var landing = implement("fiskheroes:external/superhero_landing");

function init(hero) {
    hero.setName("Iron Man");
    hero.setVersion("Mk86");
    hero.setTier(10);

    hero.setChestplate("Watchstrap");

    hero.addPowers("diminpack:mk86_watch");
    hero.addPowers("fiskheroes:shield_throwing");
    hero.addAttribute("PUNCH_DAMAGE", 8.5, 0);
    hero.addAttribute("WEAPON_DAMAGE", 1.5, 0);
    hero.addAttribute("SPRINT_SPEED", 0.5, 1);
    hero.addAttribute("JUMP_HEIGHT", 0.9, 0);
    hero.addAttribute("FALL_RESISTANCE", 0.95, 1);

    hero.addKeyBind("ENERGY_PROJECTION", "Repulsor Ray", -1);
    hero.addKeyBind("AIM", "Crabcannon", 1);
    hero.addKeyBind("CHARGED_BEAM", "Repulsor Beams", 2);
    hero.addKeyBind("BLADE", "key.blade", 3);
    hero.addKeyBind("SHIELD", "Engage Deflector Shield", 3);
    /*hero.addKeyBind("SLOW_MOTION", "Time Warp", 4);
    hero.addKeyBind("SUPER_SPEED", "Time Warp", 4);*/
    hero.addKeyBind("SHIELD_THROW", "Why Not?", 4);
    hero.addKeyBind("NANITE_TRANSFORM", "Time For Some Fun", 5);

    hero.setModifierEnabled(isModifierEnabled);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.setHasProperty(hasProperty);
    hero.setTierOverride(getTierOverride);
    hero.supplyFunction("canAim", canAim);

    hero.addAttributeProfile("INACTIVE", inactiveProfile);
    hero.addAttributeProfile("BLADE", bladeProfile);
    hero.setAttributeProfile(getProfile);
    hero.setDamageProfile(getProfile);
    hero.addDamageProfile("BLADE", {
        "types": {
            "SHARP": 1.0,
            "ENERGY": 0.7
        }
    });

    hero.addSoundEvent("MASK_OPEN", "fiskheroes:mk50_mask_open");
    hero.addSoundEvent("MASK_CLOSE", "fiskheroes:mk50_mask_close");
    hero.addSoundEvent("AIM_START", ["fiskheroes:mk50_cannon_aim", "fiskheroes:mk50_cannon_static"]);
    hero.addSoundEvent("AIM_STOP", "fiskheroes:mk50_cannon_retract");
    hero.addSoundEvent("STEP", "fiskheroes:mk50_walk");
    hero.setHasPermission(hasPermission);
    hero.setTickHandler((entity, manager) => {
        if (entity.getData("fiskheroes:speeding")){
            manager.setData(entity, "fiskheroes:slow_motion", true);
        }

        var flying = entity.getData("fiskheroes:flying");
        manager.incrementData(entity, "fiskheroes:dyn/booster_timer", 2, flying);

        var item = entity.getHeldItem();
        flying &= !entity.as("PLAYER").isUsingItem();
        manager.incrementData(entity, "fiskheroes:dyn/booster_r_timer", 2, flying && item.isEmpty() && !entity.isPunching() && entity.getData("fiskheroes:aiming_timer") == 0 && entity.getData("fiskheroes:blade_timer") == 0);
        manager.incrementData(entity, "fiskheroes:dyn/booster_l_timer", 2, flying && !item.doesNeedTwoHands() && entity.getData("fiskheroes:blade_timer") == 0);

        landing.tick(entity, manager);
    });
}

function getTierOverride(entity) {
    return entity.getData("fiskheroes:dyn/nanites") ? entity.getData("fiskheroes:shield_blocking") ? 9 : 10 : 0;
}

function inactiveProfile(profile) {
    profile.addAttribute("PUNCH_DAMAGE", 0.0, 0);
    profile.addAttribute("JUMP_HEIGHT", 0.0, 0);
    profile.addAttribute("WEAPON_DAMAGE", 0.0, 0);
    profile.addAttribute("SPRINT_SPEED", 0.0, 0);
    profile.addAttribute("FALL_RESISTANCE", 0.0, 0);
}

function bladeProfile(profile) {
    profile.addAttribute("PUNCH_DAMAGE", 19.5, 0);
    profile.addAttribute("JUMP_HEIGHT", 1.5, 0);
    profile.addAttribute("WEAPON_DAMAGE", 1.5, 0);
    profile.addAttribute("SPRINT_SPEED", 0.5, 1);
    profile.addAttribute("FALL_RESISTANCE", 0.95, 1);
}

/*function getProfile(entity) {
    if (!entity.getData("fiskheroes:dyn/nanites")) {
        return "INACTIVE";
    }
    return entity.getData("fiskheroes:blade") ? "BLADE" : null;
}*/
function getProfile(entity) {
    if  (entity.getData("fiskheroes:blade")){
        return "BLADE";
    }
    return !entity.getData("fiskheroes:dyn/nanites") ? "INACTIVE" : null;
}

function isModifierEnabled(entity, modifier) {
    if (modifier.name() == "fiskheroes:transformation"){
        return true;
    }

    switch (modifier.name()) {
        /*case "fiskheroes:super_speed":
            return entity.getData("fiskheroes:slow_motion");*/
        case "fiskheroes:energy_bolt":
            return entity.getData("fiskheroes:aimed_timer") >= 1;
        case "fiskheroes:charged_beam":
            return !entity.getData("fiskheroes:shield_blocking") && entity.getData("fiskheroes:dyn/nanites");
        case "fiskheroes:energy_projection":
            return entity.getData("fiskheroes:aimed_timer") >= 1;
        case "fiskheroes:blade":
            return !entity.getData("fiskheroes:aiming") && !entity.getData("fiskheroes:shield") && !(entity.isSprinting() && entity.getData("fiskheroes:flying"));
        case "fiskheroes:shield":
            return !entity.getData("fiskheroes:aiming") && !entity.getData("fiskheroes:flying");
        case "fiskheroes:regeneration":
            return !entity.getData("fiskheroes:blade");
        case "fiskheroes:water_breathing":
            return !entity.getData("fiskheroes:mask_open") && entity.getData("fiskheroes:dyn/nanites");
        case "fiskheroes:slow_motion":
            return true;
        default:
            return entity.getData("fiskheroes:dyn/nanites");
    }
}

function isKeyBindEnabled(entity, keyBind) {
    if (keyBind == "NANITE_TRANSFORM") {
        return true;
    }


    switch (keyBind) {
        case "SLOW_MOTION":
            return entity.isSneaking();
        case "SUPER_SPEED":
            return !entity.isSneaking();
        case "ENERGY_PROJECTION":
            return entity.getData("fiskheroes:aiming_timer") == 1;
        case "CHARGED_BEAM":
            return !(entity.isSprinting() && entity.getData("fiskheroes:flying")) && (entity.getData("fiskheroes:dyn/nanite_timer") == 1);
        case "SHIELD":
            return (entity.getData("fiskheroes:blade_timer") == 0 || entity.isBookPlayer()) && !entity.getData("fiskheroes:flying");
        case "BLADE":
            return entity.getData("fiskheroes:shield_timer") > 0 && !(entity.isSprinting() && entity.getData("fiskheroes:flying")) || entity.getData("fiskheroes:blade_timer") > 0 && !(entity.isSprinting() && entity.getData("fiskheroes:flying")) || entity.isBookPlayer() && !(entity.isSprinting() && entity.getData("fiskheroes:flying")) || entity.getData("fiskheroes:flying")&& !(entity.isSprinting() && entity.getData("fiskheroes:flying")) ;
        case "AIM":
            return entity.getData("fiskheroes:beam_charge") == 0 && !entity.getData("fiskheroes:telekinesis") && !entity.getData("fiskheroes:shield") && !entity.getData("fiskheroes:blade");
        case "SLOW_MOTION":
            return true;
        default:
            return (entity.getData("fiskheroes:dyn/nanite_timer") == 1);
    }
}

function hasProperty(entity, property) {
    switch (property) {
        case "MASK_TOGGLE":
            return entity.getData("fiskheroes:dyn/nanite_timer") == 1;
        case "BREATHE_SPACE":
            return !entity.getData("fiskheroes:mask_open") && entity.getData("fiskheroes:dyn/nanites");
        default:
            return false;
    }
}

function canAim(entity) {
    return entity.getHeldItem().isEmpty() && !entity.getData("fiskheroes:shield");
}
function hasPermission(entity, permission) {
    return permission == "USE_SHIELD";
}

