var landing = implement("pkg:external/superhero_landing_1");
var utils = implement("fiskheroes:external/utils");

function init(hero) {
    hero.setName("Anodite");
    hero.setTier(10);
    hero.setHelmet("Mask");
    hero.setChestplate("Chestplate");
    hero.setLeggings("Leggings");
    hero.setBoots("Boots");
    
    hero.addPowers("pkg:anodite_physiology", "pkg:mana", "pkg:mana_physiology");
    hero.addAttribute("FALL_RESISTANCE", 1, 1);
    hero.addAttribute("PUNCH_DAMAGE", 20.0, 0);
    hero.addAttribute("SPRINT_SPEED", 0.5, 1);
    hero.addAttribute("STEP_HEIGHT", 0.5, 0);
    hero.addAttribute("IMPACT_DAMAGE", 0.5, 1);
    hero.addAttribute("BASE_SPEED_LEVELS", 1.0, 0);

    hero.setDefaultScale(0.9);

    hero.addKeyBind("ENERGY_PROJECTION", "Mana Laser", 1);
    hero.addKeyBind("CHARGED_BEAM", "Mana Blast", 1);
    hero.addKeyBind("SPELL_MENU", "Mana Menu", 2);
    hero.addKeyBind("CHARGED_PUNCH", "Mana Punch", 3);
    hero.addKeyBind("SHIELD", "Mana Shield", 4);
    hero.addKeyBindFunc("func_GIANT_MODE", giantModeKey, "key.giantMode", 5);
    hero.addKeyBind("TELEPORT", "Go to the Moon(You need to look to the moon)", 5);

    hero.addAttributeProfile("SHIELD", shieldProfile);
    hero.setAttributeProfile(getAttributeProfile);
    hero.setModifierEnabled(isModifierEnabled);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.setHasProperty(hasProperty);
    hero.setTickHandler((entity, manager) => {
        utils.flightOnIntangibility(entity, manager);
        landing.tick(entity, manager);
    });

    hero.addSoundEvent("LAND", "fiskheroes:anti_land");
    hero.addSoundEvent("PUNCH", "fiskheroes:anti_punch");
    hero.addSoundEvent("STEP", "fiskheroes:anti_walk");

    hero.addAttributeProfile("PUNCH", punchProfile);
    hero.addAttributeProfile("BLUNT", bluntProfile);
    hero.setAttributeProfile(getAttributeProfile);
}

function punchProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 25, 0);
    profile.addAttribute("KNOCKBACK", 30.0, 0);
}

function bluntProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 20, 0);
}

function getAttributeProfile(entity) {
    return entity.getData("fiskheroes:punchmode") ? "PUNCH" : "BLUNT";
}

function giantModeKey(player, manager) {
    var flag = player.getData("fiskheroes:dyn/giant_mode");
    manager.setData(player, "fiskheroes:dyn/giant_mode", !flag);
    manager.setData(player, "fiskheroes:size_state", flag ? -1 : 1);
    return true;
}

function shieldProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("BASE_SPEED", -0.75, 1);
    profile.addAttribute("JUMP_HEIGHT", -2.0, 1);
}

function getAttributeProfile(entity) {
    return entity.getData("fiskheroes:shield_blocking") ? "SHIELD" : null;
}

function isModifierEnabled(entity, modifier) {
    switch (modifier.name()) {
        case "fiskheroes:arrow_catching":
            return entity.getData("fiskheroes:scale") <= 1.11 && !entity.getData("fiskheroes:shield_blocking");
        case "fiskheroes:energy_projection":
            return entity.getData("fiskheroes:scale") <= 1.11;
        case "fiskheroes:charged_beam":
            return entity.getData("fiskheroes:scale") > 1.11;
        case "fiskheroes:flight":
            return entity.getData("fiskheroes:shadowform");
        case "fiskheroes:leaping":
            return !entity.getData("fiskheroes:shadowform") && !entity.getData("fiskheroes:shield_blocking");
        default:
            return true;
    }
}

function isKeyBindEnabled(entity, keyBind) {
    if (keyBind != "SHADOWFORM" && entity.getData("fiskheroes:shadowform") || keyBind != "SHIELD" && entity.getData("fiskheroes:shield_blocking")) {
        return false;
    }

    switch (keyBind) {
        case "func_GIANT_MODE":
            var timer = entity.getData("fiskheroes:dyn/giant_mode_timer");
            return timer == 0 || timer == 1;
        case "CHARGED_BEAM":
            return entity.getHeldItem().isEmpty() && entity.getData("fiskheroes:scale") > 1.11 && entity.getData("fiskheroes:size_state") == 0;
        case "SHIELD":
            return entity.getHeldItem().isEmpty() && entity.getData("fiskheroes:scale") <= 1.11;
        case "TELEPORT":
            return entity.getData("fiskheroes:flying");
        case "SPELL_MENU":
            return entity.getHeldItem().isEmpty() && entity.getData("fiskheroes:scale") <= 1.11;
        default:
            return entity.getData("fiskheroes:scale") <= 1.11;
    }
}

function punchProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 25, 0);
    profile.addAttribute("KNOCKBACK", 30.0, 0);
}

function bluntProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 20, 0);
}

function getAttributeProfile(entity) {
    return entity.getData("fiskheroes:punchmode") ? "PUNCH" : "BLUNT";
}

function hasProperty(entity, property) {
    return property == "BREATHE_SPACE";
}