import * as mc from "@minecraft/server"
import * as ui from '@minecraft/server-ui'
import { tutorialDragonHornCollected } from "./world_events";

const SOUND_FAIL = "note.didgeridoo";
const SOUND_OPTIONS = { pitch: 1.0, volume: 1.0, };

var fire_dragon = false;
var earth_dragon = false;
var jungle_dragon = false;
var skull_dragon = false;
var water_dragon = false;

export function main() {
    interactController();
}

function interactController() {
    mc.world.afterEvents.itemUse.subscribe((eventData) => {
        let player = eventData.source;
        let itemUse = eventData.itemStack;

        if (itemUse.typeId != 'pb:dragon_horn') return;

        if(mc.world.scoreboard.getObjective("dragon_unlock").getScore("any") != 2)
        {
            tutorialDragonHornCollected(player.location, player.dimension);
            return;
        }


        if (mc.world.scoreboard.getObjective("dragon_unlock").getScore("fire") == 3) fire_dragon = true; else fire_dragon = false;
        if (mc.world.scoreboard.getObjective("dragon_unlock").getScore("earth") == 3) earth_dragon = true; else earth_dragon = false;
        if (mc.world.scoreboard.getObjective("dragon_unlock").getScore("jungle") == 3) jungle_dragon = true; else jungle_dragon = false;
        if (mc.world.scoreboard.getObjective("dragon_unlock").getScore("dark") == 3) skull_dragon = true; else skull_dragon = false;
        if (mc.world.scoreboard.getObjective("dragon_unlock").getScore("water") == 3) water_dragon = true; else water_dragon = false;

        let form = new ui.ActionFormData();
        form.title("§lDragoncaller Horn");
        if (fire_dragon) form.button("Call Fire Dragon", "textures/items/portrait/dragon_fire_portrait"); else form.button("", "textures/items/portrait/dragon_fire_portrait_l");
        if (earth_dragon) form.button("Call Earth Dragon", "textures/items/portrait/earth_dragon_portrait"); else form.button("", "textures/items/portrait/earth_dragon_portrait_l");
        if (jungle_dragon) form.button("Call Jungle Dragon", "textures/items/portrait/jungle_dragon_portrait"); else form.button("", "textures/items/portrait/jungle_dragon_portrait_l");
        if (skull_dragon) form.button("Call Skull Dragon", "textures/items/portrait/skull_dragon_portrait"); else form.button("", "textures/items/portrait/skull_dragon_portrait_l");
        if (water_dragon) form.button("Call Water Dragon", "textures/items/portrait/water_dragon_portrait"); else form.button("", "textures/items/portrait/water_dragon_portrait_l");
        form.button("Dismiss Dragon","textures/items/portrait/dismiss");

        form.show(player).then(result => {
            if (result.canceled) return;

            let response = result.selection;
            switch (response) {
                case 0:
                    if (fire_dragon) {
                        dragonRemove(player);
                        player.triggerEvent("evt:summon_tamed_fire_dragon");
                    }
                    else player.playSound(SOUND_FAIL, SOUND_OPTIONS);
                    break;
                case 1:
                    if (earth_dragon) {
                        dragonRemove(player);
                        player.triggerEvent("evt:summon_tamed_earth_dragon");
                    }
                    else player.playSound(SOUND_FAIL, SOUND_OPTIONS);
                    break;
                case 2:
                    if (jungle_dragon) {
                        dragonRemove(player);
                        player.triggerEvent("evt:summon_tamed_jungle_dragon");
                    }
                    else player.playSound(SOUND_FAIL, SOUND_OPTIONS);
                    break;
                case 3:
                    if (skull_dragon) {
                        dragonRemove(player);
                        player.triggerEvent("evt:summon_tamed_skull_dragon");
                    }
                    else player.playSound(SOUND_FAIL, SOUND_OPTIONS);
                    break;
                case 4:
                    if (water_dragon) {
                        dragonRemove(player);
                        player.triggerEvent("evt:summon_tamed_water_dragon");
                    }
                    else player.playSound(SOUND_FAIL, SOUND_OPTIONS);
                    break;
                default: dragonRemove(player);
            }
        });
    })
}

export function dragonRegister(sourceEntity) {

    let evt_location = sourceEntity.location;
    let dimension = sourceEntity.dimension.id;

    let player = mc.world.getDimension(dimension).getEntities({ location: evt_location, families: ["player"], closest: 1 })[0];
    sourceEntity.addTag(player.id);
}

export function dragonRemove(player) {

    let evt_location = player.location;
    let dimension = player.dimension.id;

    for (let entity of mc.world.getDimension(dimension).getEntities({ location: evt_location, families: ["dragon"], tags: [player.id] })) {
        entity.remove();
    }
}