import * as mc from "@minecraft/server"
import * as ui from '@minecraft/server-ui'
import * as dragon_horn_menu from './dragon_horn_menu';
import * as dragon_attacks from './dragon_attacks';
import * as weapon_effects from './weapon_effects';
import * as world_events from './world_events';
import * as effects from './effects';

dragon_horn_menu.main();
world_events.main();
scriptEventListener();

const hurt_enemy_types= ["pb:skeleton_soldier_0","pb:skeleton_soldier_1","pb:skeleton_soldier_2","pb:skeleton_soldier_3","pb:fight_dummy"]

mc.world.afterEvents.entityHurt.subscribe((eventData) => {
    if (hurt_enemy_types.includes(eventData.hurtEntity.typeId) && eventData.damage >= 2) {
        eventData.hurtEntity.runCommandAsync(`playanimation @s hurt`)
    }
})

const forbidden_enemy_types= ["minecraft:piglin","minecraft:zombie_pigman","minecraft:zombified_piglin","minecraft:piglin_brute","minecraft:ghast","minecraft:hoglin"]

mc.world.afterEvents.entitySpawn.subscribe((eventData) => {
    if (forbidden_enemy_types.includes(eventData.entity.typeId)) 
    {
        if(eventData.entity.dimension.id == "overworld")
        {
            eventData.entity.remove();
        }
    }
})

function scriptEventListener() {
    mc.system.afterEvents.scriptEventReceive.subscribe((eventData) => {
        const{id,sourceEntity,message} = eventData;
        if(sourceEntity == null) return;

        if(id == "pb:dragon_attack" && message == "breath") dragon_attacks.breath(sourceEntity);
        if(id == "pb:dragon_attack" && message == "fireball") dragon_attacks.fireball(sourceEntity);
        if(id == "pb:dragon_attack" && message == "nova") dragon_attacks.nova(sourceEntity);
        if(id == "pb:dragon" && message == "register") dragon_horn_menu.dragonRegister(sourceEntity);
        if(id == "pb:dragon" && message == "remove") dragon_horn_menu.dragonRemove(sourceEntity);

        if(id == "pb:weapon_attack" && message == "trident") weapon_effects.tridentAttack(sourceEntity);
        if(id == "pb:weapon_attack" && message == "hammer") weapon_effects.hammerReject(sourceEntity);
        if(id == "pb:weapon_attack" && message == "spear") weapon_effects.spearAttack(sourceEntity);
        if(id == "pb:weapon_attack" && message == "spear_spin") weapon_effects.spearSpin(sourceEntity);

        if(id == "pb:weapon_attack" && message == "skull") weapon_effects.skullimpact(sourceEntity);
        if(id == "pb:weapon_attack" && message == "skull_boss") weapon_effects.skullimpactBoss(sourceEntity);

        if(id == "pb:effect" && message == "lazer") effects.lazer(sourceEntity);
        if(id == "pb:effect" && message == "chain") effects.lazerToNearby(sourceEntity, 5);

        if(id == "pb:dragon" && message == "freed") world_events.tutorialDragonFreed(sourceEntity.location);
        if(id == "pb:dragon" && message == "firstride") world_events.tutorialDragonRide(sourceEntity);

    })
}

mc.world.afterEvents.itemUse.subscribe((eventData) => {
    let itemUse = eventData.itemStack;
    if(itemUse.typeId == 'minecraft:debug_stick') debugStickInteract(eventData);
})

function debugStickInteract(eventData)
{
    let player = eventData.source;
    let itemUse = eventData.itemStack;
    let target = mc.world.getDimension('overworld').getEntities({ location: player.location,  closest: 1, families: ["dungeon_room_controller"], maxDistance: 3})[0];
    if(target == undefined) return;

    let target_scoreboard_id = target.scoreboardIdentity;
    
    let score_room_controller = mc.world.scoreboard.getObjective("room_controller").getScore(target_scoreboard_id);
    let score_room_population = mc.world.scoreboard.getObjective("room_population").getScore(target_scoreboard_id);

    let rc_init_count = mc.world.scoreboard.getObjective("rc_init_count").getScore(target_scoreboard_id);
    let rc_init_start = mc.world.scoreboard.getObjective("rc_init_start").getScore(target_scoreboard_id);
    let rc_init_max = mc.world.scoreboard.getObjective("rc_init_max").getScore(target_scoreboard_id);
    let rc_init_set = mc.world.scoreboard.getObjective("rc_init_set").getScore(target_scoreboard_id);
    let rc_init_delay = mc.world.scoreboard.getObjective("rc_init_delay").getScore(target_scoreboard_id);

    //mc.world.sendMessage("score_room_controller: " + score_room_controller);
    //mc.world.sendMessage("score_room_population: " + score_room_population);

    let enemySetList = [ "Skeleton_0", "Skeleton_1", "Skeleton_2", "Skeleton_3", "Ork_0" ]

    let form = new ui.ModalFormData()
    form.title(target.typeId);
    form.dropdown("enemy_type", enemySetList, rc_init_set);
    form.slider("total_enemy_count", 1, 50, 1, rc_init_count);
    form.slider("spawn_enemies_at_start", 1, 10, 1, rc_init_start);
    form.slider("max_enemies_in_room", 1, 20, 1, rc_init_max);
    form.slider("enemy_spawn_delay", 1, 5, 1, rc_init_delay);

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

        //mc.world.sendMessage(`results: ${JSON.stringify(result.formValues, undefined, 2)}`);
        //mc.world.sendMessage("score_room_controller: " + result_room_controller);

        mc.world.scoreboard.getObjective("rc_init_set").setScore(target_scoreboard_id, result.formValues[0])
        mc.world.scoreboard.getObjective("rc_init_count").setScore(target_scoreboard_id, result.formValues[1])
        mc.world.scoreboard.getObjective("rc_init_start").setScore(target_scoreboard_id, result.formValues[2])
        mc.world.scoreboard.getObjective("rc_init_max").setScore(target_scoreboard_id, result.formValues[3])
        mc.world.scoreboard.getObjective("rc_init_delay").setScore(target_scoreboard_id, result.formValues[4])
    });
}