export const RomPatcher = function( initialPatches = {} ) {

    let patchesArray = [];
    if (initialPatches) {
        patchesArray = Array.isArray(initialPatches) ? initialPatches : [initialPatches];
    }
    patchesArray = patchesArray.filter(p => p && p.file);

    return {

        /**
         * @type {string}
         */
        romFileName: '',

        /**
         * @type {string}
         */
        patchFileName: '',

        /**
         * @type {boolean}
         */
        isRomDragOver: false,

        /**
         * @type {boolean}
         */
        isPatchDragOver: false,

        /**
         * @type {boolean}
         */
        showStatusBox: false,

        /**
         * @type {object}
         */
        patchesData: patchesArray,
        hasEmbedded: patchesArray.length > 0,

        init( config = {language: 'en', requireValidation: false} ) {

            const CONFIG = config;

            if (!RomPatcherWeb.isInitialized()){
                if (this.hasEmbedded) {
                    RomPatcherWeb.initialize(CONFIG, ...this.patchesData);
                } else {
                    RomPatcherWeb.initialize(CONFIG);
                }
            }

            const STAT_BOX = document.getElementById('patcher-status');
            const OBSERVER = new MutationObserver(() => {
                const CRC = document.getElementById('rom-patcher-span-crc32').textContent;
                const DESC = document.getElementById('rom-patcher-patch-description').textContent;
                const PATCH = document.getElementById('rom-patcher-patch-requirements-value').textContent;

                this.showStatusBox = CRC.trim().length > 0 || DESC.trim().length > 0 || PATCH.trim().length > 0;
            });

            OBSERVER.observe(STAT_BOX, { childList: true, subtree: true, characterData: true });
        },

        /**
         *
         * @param {string} id
         */
        triggerFileInput(id){
            const I = document.getElementById(id);
            if( !I.disabled )
                I.click();
        },

        /**
         *
         * @param {Event} e
         * @param {string} type
         */
        handleInputChange(e, type){
            const file = e.target.files[0];
            if(file){
                if( type === 'rom' ) this.romFileName = file.name;
                if( type === 'patch' ) this.patchFileName = file.name;
            }
        },

        /**
         *
         * @param {Event} e
         * @param {string} type
         */
        handleDrop(e, type ){

            const file = e.dataTransfer.files[0];
            if( !file )
                return;

            const ID = type === 'rom' ? 'rom-patcher-input-file-rom' : 'rom-patcher-input-file-patch';
            const I = document.getElementById(ID);

            if( I.disabled )
                return;

            I.files = e.dataTransfer.files;

            if( type === 'rom' ) this.romFileName = file.name;
            if( type === 'patch' ) this.patchFileName = file.name;

            I.dispatchEvent(new Event('change', { bubbles: true }));
        }
    }
}

window.RomPatcher = RomPatcher;


