import {  Effect, EffectType, EffectTypes, Entity, GameMode, ItemStack, system, World, world } from "@minecraft/server";
import { boss, dsEntity, removerPrefixo, scanerMobs } from "./communs";
import { viewLife } from "../assistant/view";
import "./hit_player";
import './projetil';
import './antafk'
import './adm'

function checkBosses() {
    dsEntity.forEach((dimension) => {
        world
            .getDimension(dimension)
            .getEntities()
            .forEach((entity) => {
                const typeId = entity.typeId;
                if (boss[removerPrefixo(typeId)]) {
                    const creature_active = entity.getProperty(
                        "ns_ab:creature_active"
                    );
                    if (boss[removerPrefixo(typeId)].spawnAnimation) {
                        let entitysForActive = scanerMobs(entity, {
                            families: ["player"],
                            maxDistance: 10,
                            location: entity.location,
                            tags:["gmsurvival"]
                        });
                        entitysForActive.forEach((victim) => {
                            if (victim.typeId == "minecraft:player") {
                                entity.setProperty("ns_ab:launch", 1);
                                if (!creature_active)
                                    entity.triggerEvent("active");
                            } else {
                                entity.setProperty("ns_ab:launch", 0);
                            }
                        });
                    }
                }
            });
    });
}

function playerGamemode() {
    world.getAllPlayers().forEach((player)=>{
        const gamemode = player.getGameMode()
        if (gamemode == GameMode.survival) {
            player.addTag('gmsurvival');
            player.removeTag('gmcreative');
        } else if (gamemode == GameMode.adventure) {
            player.addTag('gmsurvival');
            player.removeTag('gmcreative');
        } else {
            player.removeTag('gmsurvival');
            player.addTag('gmcreative');
        }
    })
}

function particleGanimedes() {
    dsEntity.forEach((dimension) => {
        world.getDimension(dimension).getEntities().forEach((target) => {
            if (target.typeId == 'ns_ab:ganimedes') {
                try {
                    if (target.getProperty('ns_ab:invisibility') == 1) target.dimension.spawnParticle('ns_ab:foot_mark',target.location)
                } catch (error) {
                    return 
                }
            }
        })
    })
}

function rpPlayer() {
    world.getAllPlayers().forEach((player) => {
        if (player.hasTag("adm")) viewLife(player);
    });
}

/**
 * 
 * @param {Entity} source 
 */
function randomDropMinions (source) {
    let potion_regeneration = new ItemStack('ns_ab:regeneration_potion',1)
    let potion_instant_health = new ItemStack('ns_ab:instant_health_potion',1)
    let golden_apple = new ItemStack('minecraft:golden_apple',1)
    const itens = [potion_instant_health,potion_regeneration,golden_apple]
    let value = Math.round(Math.random()*100)
    if (value >= 40) {
        source.dimension.spawnItem(itens[Math.round(Math.random()*2)],source.location)
    }
}

system.runInterval(() => {
    checkBosses();
    playerGamemode();
    rpPlayer();
    particleGanimedes()
});

// Estou com pressa para receber, mas proxima atualização fazer o codigo decente 

world.afterEvents.entityDie.subscribe((event) => {
    const {deadEntity,damageSource} = event;
    if (
        deadEntity != undefined
    ) {
        if (deadEntity.typeId == 'ns_ab:drop_minions') {
            randomDropMinions(deadEntity)
        }
    }
})