import { RefreshType, socketEvent } from '../../systemRegistration/socket.mjs';

export default class DhSceneConfigSettings extends foundry.applications.sheets.SceneConfig {
    constructor(options) {
        super(options);

        Hooks.on(socketEvent.Refresh, ({ refreshType }) => {
            if (refreshType === RefreshType.Scene) this.render();
        });
    }

    static DEFAULT_OPTIONS = {
        ...super.DEFAULT_OPTIONS,
        actions: {
            ...super.DEFAULT_OPTIONS.actions,
            removeSceneEnvironment: DhSceneConfigSettings.#removeSceneEnvironment
        }
    };

    static buildParts() {
        const { footer, tabs, ...parts } = super.PARTS;
        const tmpParts = {
            tabs: { template: 'systems/daggerheart/templates/scene/tabs.hbs' },
            ...parts,
            dh: { template: 'systems/daggerheart/templates/scene/dh-config.hbs' },
            footer
        };
        return tmpParts;
    }

    static PARTS = DhSceneConfigSettings.buildParts();

    static buildTabs() {
        super.TABS.sheet.tabs.push({ id: 'dh', src: 'systems/daggerheart/assets/logos/FoundryBorneLogoWhite.svg' });
        return super.TABS;
    }

    static TABS = DhSceneConfigSettings.buildTabs();

    async _preRender(context, options) {
        await super._preFirstRender(context, options);

        if (!options.internalRefresh)
            this.daggerheartFlag = new game.system.api.data.scenes.DHScene(this.document.flags.daggerheart);
    }

    _attachPartListeners(partId, htmlElement, options) {
        super._attachPartListeners(partId, htmlElement, options);

        switch (partId) {
            case 'dh':
                htmlElement.querySelector('#rangeMeasurementSetting')?.addEventListener('change', async event => {
                    this.daggerheartFlag.updateSource({ rangeMeasurement: { setting: event.target.value } });
                    this.render({ internalRefresh: true });
                });

                const dragArea = htmlElement.querySelector('.scene-environments');
                if (dragArea) dragArea.ondrop = this._onDrop.bind(this);

                break;
        }
    }

    async _onDrop(event) {
        event.stopPropagation();
        const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
        if (data.type === 'Level') {
            const level = await foundry.documents.Level.fromDropData(data);
            if (level?.parent === this.document) return this._onSortLevel(event, level);

            return;
        }

        const item = await foundry.utils.fromUuid(data.uuid);
        if (item instanceof game.system.api.documents.DhpActor && item.type === 'environment') {
            let sceneUuid = data.uuid;
            if (item.pack) {
                const inWorldActor = await game.system.api.documents.DhpActor.create([item.toObject()]);
                if (!inWorldActor.length) return;
                sceneUuid = inWorldActor[0].uuid;
            }

            await this.daggerheartFlag.updateSource({
                sceneEnvironments: [...this.daggerheartFlag.sceneEnvironments, sceneUuid]
            });
            this.render({ internalRefresh: true });
        }
    }

    /** @inheritDoc */
    async _preparePartContext(partId, context, options) {
        context = await super._preparePartContext(partId, context, options);
        switch (partId) {
            case 'dh':
                context.data = this.daggerheartFlag;
                context.variantRules = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.variantRules);
                break;
        }

        return context;
    }

    static async #removeSceneEnvironment(_event, button) {
        await this.daggerheartFlag.updateSource({
            sceneEnvironments: this.daggerheartFlag.sceneEnvironments.filter(
                (_, index) => index !== Number.parseInt(button.dataset.index)
            )
        });
        this.render({ internalRefresh: true });
    }

    /** @override */
    async _processSubmitData(event, form, submitData, options) {
        if (!submitData.flags) submitData.flags = {};
        submitData.flags.daggerheart = foundry.utils.mergeObject(
            this.daggerheartFlag.toObject(),
            submitData.flags.daggerheart
        );
        submitData.flags.daggerheart.sceneEnvironments = submitData.flags.daggerheart.sceneEnvironments.filter(x =>
            foundry.utils.fromUuidSync(x)
        );

        super._processSubmitData(event, form, submitData, options);
    }
}
