import { world, system, BlockPermutation } from "@minecraft/server";
import { ModalFormData, ActionFormData } from "@minecraft/server-ui"



export class ChannelKey {
    constructor(block, targetBlock) {
        this.block = block
        this.targetBlock = targetBlock
    }

    getKey(block) {
        return world.getDynamicProperty(`pb_red:channel_key:x_${block.location.x}:y_${block.location.y}:z_${block.location.z}:dim_${block.dimension.id.replace("minecraft:", "")}`) || null;
    }

    setKey(block, key) {
        world.setDynamicProperty(`pb_red:channel_key:x_${block.location.x}:y_${block.location.y}:z_${block.location.z}:dim_${block.dimension.id.replace("minecraft:", "")}`, key);
    }

    removeBlockKey() {
        this.setKey(this.block, "pb_red:null")
    }

    static constructBlock(location, dimension) {
        return { location: location, dimension: { id: dimension } }
    }

    channelConfirmed() {
        const key = this.getKey(this.block)
        const targetKey = this.getKey(this.targetBlock)

        if ((key == null || key == "pb_red:null") && (targetKey == null || targetKey == "pb_red:null")) {
            return true;
        }

        if (targetKey == key && key !== "pb_red:null") return true;

        return false;
    }

    openUI(player) {

        const key = this.getKey(this.block)

        let boddySTR = `\n§lCurrent Channel Key: §r${key}\n\n\n`

        if (key == null || key == "pb_red:null") {
            boddySTR = `\nThere's currently no Channel Key on this block\n\n\n`
        }

        const channelKeyGUI = new ActionFormData()
            .title("Channel Key Manager")
            .body(boddySTR)
            .button("Change Key")
            .button("Remove Key")
            .show(player).then((selec) => {
                if (selec.canceled) {
                    return;
                }

                switch (selec.selection) {
                    case 0:
                        const changeKey = new ModalFormData()
                            .title("Change Key")
                            .textField("Type the key: ", "e.g. 'Farm System123'", (key == null || key == "pb_red:null") ? "" : key)
                            .show(player).then((selec) => {
                                if (selec.canceled) {
                                    return;
                                }
                                let newKey = selec.formValues[0];
                                this.setKey(this.block, newKey)
                            });

                        break;
                    case 1:
                        this.removeBlockKey()
                        break;

                }
            })

    }

    openControllerUI(player, item) {

        const key = item.getDynamicProperty("pb_red:channel_key") || null

        let boddySTR = `\n§lCurrent Channel Key: §r${key}\n\n\n`

        if (key == null || key == "pb_red:null") {
            boddySTR = `\nThere's currently no Channel Key on this controller\n\n\n`
        }

        const channelKeyGUI = new ActionFormData()
            .title("Channel Key Manager")
            .body(boddySTR)
            .button("Change Key")
            .button("Remove Key")
            .show(player).then((selec) => {
                if (selec.canceled) {
                    return;
                }

                switch (selec.selection) {
                    case 0:
                        const changeKey = new ModalFormData()
                            .title("Change Key")
                            .textField("Type the key: ", "e.g. 'Farm System123'", (key == null || key == "pb_red:null") ? "" : key)
                            .show(player).then((selec) => {
                                if (selec.canceled) {
                                    return;
                                }
                                let newKey = selec.formValues[0];
                                item.setDynamicProperty("pb_red:channel_key", newKey)

                                const inv = player.getComponent('inventory').container;
                                inv.setItem(player.selectedSlotIndex, item);

                            });

                        break;
                    case 1:
                        item.setDynamicProperty("pb_red:channel_key", "pb_red:null")

                        const inv = player.getComponent('inventory').container;
                        inv.setItem(player.selectedSlotIndex, item);
                        break;

                }
            })

    }


}


