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

function init(hero) {
    hero.setName("Steel");
    hero.setTier(10);
    hero.addKeyBindFunc("TENTACLES", StartClimb, "Wall Run", 4);
    hero.hide();

    hero.setChestplate("Ultralink");

    hero.addPowers("pkg:turbo_energy");
    hero.addAttribute("PUNCH_DAMAGE", 6.0, 0);
    hero.addAttribute("WEAPON_DAMAGE", 2.0, 0);
    hero.addAttribute("FALL_RESISTANCE", 100000, 1);
    hero.addAttribute("BASE_SPEED_LEVELS", 7.0, 0);

    //TRANSFORM

    hero.addKeyBind("TURBO", "TURBO!", 1);
    hero.addKeyBind("FLIGHT", "TURBO FLIGHT MOD!", 2);
    hero.addKeyBind("STEALTH", "TURBO STEALTH MOD!", 3);
    hero.addKeyBind("STRENGHT", "TURBO STRENGHT MOD!", 4);
    hero.addKeyBind("MORE", "TURBO MODS", 5);

    hero.addKeyBind("SPEED", "TURBO SPEED MOD!", 2);
    hero.addKeyBind("SCUBA", "TURBO SCUBA MOD!", 3);
    hero.addKeyBind("SUPER", "TURBO SUPER MOD!", 4);

    // BASE
    hero.addKeyBind("LASH", "TURBO LASH", 2)
    hero.addKeyBind("WEB_ZIP", "Lash Forward", 4);
    hero.addKeyBindFunc("func_WEB_SWINGING", webSwingingKey, "Lashing", 5);

    //STRENGHT
    hero.addKeyBind("CHARGED_PUNCH", "Super Punch", 2)
    hero.addKeyBind("EARTHQUAKE", "key.earthquake", 3);

    //STEALTH
    hero.addKeyBind("INVISIBILITY", "key.invisibility", 2);

    //SPEED
    hero.addKeyBind("SUPER_SPEED", "Super speed", 3);

    //SUPER
    hero.addKeyBind("HEAT_VISION", "Turbo Vision", 2);
    hero.addKeyBind("GROUND_SMASH", "Turbo Smash", 1);

    //FLĘGHT
    hero.addKeyBind("TELEPORT", "Go to the Moon(You need to look to the moon)", 3);

    hero.setModifierEnabled(isModifierEnabled);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.setDefaultScale(defaultScale);
    hero.setTierOverride(getTierOverride);
    hero.addAttributeProfile("TURBO", turboProfile);
    hero.addAttributeProfile("FLIGHT", flightProfile);
    hero.addAttributeProfile("STRENGHT", strenghtProfile);
    hero.addAttributeProfile("SPEED", speedProfile);
    hero.addAttributeProfile("SCUBA", scubaProfile);
    hero.addAttributeProfile("STEALTH", stealthProfile);
    hero.addAttributeProfile("SUPER", superProfile);
    hero.addAttributeProfile("PUNCH", punchProfile);
    hero.addAttributeProfile("BLUNT", bluntProfile);
    hero.setAttributeProfile(getAttributeProfile);
    hero.setTickHandler((entity, manager) => {
        utils.flightOnIntangibility(entity, manager);
        landing.tick(entity, manager);
        var data1 = entity.getData("pkg:dyn/phase_timer")
        var _data2 = "pkg:dyn/phase"
        var data2 = entity.getData(_data2)
        if (data1 == 1 && !data2) {
            manager.setData(entity, _data2, true)
        }
        if (entity.isOnGround() && data2) {
            manager.setData(entity, _data2, false)
        }
        var value = entity.getData("pkg:dyn/climb");
        if (entity.getData('fiskheroes:tentacle_lift')) {
            if (entity.motionY() > 0.05 || entity.isSneaking() && entity.world().getBlock(entity.pos().add(0, -1, 0)) == 'minecraft:air') {
                manager.setData(entity, "pkg:dyn/climb", value + 0.1);
            }
        } else if (!entity.getData('fiskheroes:tentacle_lift') && value != 0) {
            manager.setData(entity, "pkg:dyn/climb", 0);
            manager.setData(entity, "pkg:dyn/climb_bool", false);
        }
        if (entity.getData("fiskheroes:tentacles") == null && entity.getData('fiskheroes:tentacle_lift')) {
            manager.setData(entity, "fiskheroes:tentacle_lift", false);
        } else if (entity.getData("fiskheroes:tentacles") != null && !entity.getData('fiskheroes:tentacle_lift')) {
            manager.setData(entity, "fiskheroes:tentacle_lift", true);
        }

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

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

function bluntProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 20, 0);
}
function getAttributeProfile(entity) {
    return entity.getData("fiskheroes:punchmode") ? "PUNCH" : "BLUNT";
}

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 turboProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 6, 0);
    profile.addAttribute("SPRINT_SPEED", 0.6, 1);
    profile.addAttribute("REACH_DISTANCE", 0.8, 1);
    profile.addAttribute("MAX_HEALTH", 20.0, 0);
}

function flightProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 6, 0);
    profile.addAttribute("SPRINT_SPEED", 0.6, 1);
    profile.addAttribute("REACH_DISTANCE", 0.8, 1);
    profile.addAttribute("MAX_HEALTH", 20.0, 0);
}

function superProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 25, 0);
    profile.addAttribute("SPRINT_SPEED", 0.6, 1);
    profile.addAttribute("REACH_DISTANCE", 3.8, 1);
    profile.addAttribute("MAX_HEALTH", 25.0, 0);
}

function stealthProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 7.6, 0);
    profile.addAttribute("SPRINT_SPEED", 0.4, 1);
    profile.addAttribute("REACH_DISTANCE", 0.8, 1);
    profile.addAttribute("MAX_HEALTH", 20.0, 0);
}

function strenghtProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 35, 0);
    profile.addAttribute("SPRINT_SPEED", 0.6, 1);
    profile.addAttribute("REACH_DISTANCE", 2.8, 1);
    profile.addAttribute("MAX_HEALTH", 26.0, 0);
}

function scubaProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 6, 0);
    profile.addAttribute("SPRINT_SPEED", 0.6, 1);
    profile.addAttribute("REACH_DISTANCE", 2.8, 1);
    profile.addAttribute("MAX_HEALTH", 26.0, 0);
}

function speedProfile(profile) {
    profile.addAttribute("FALL_RESISTANCE", 100000, 1);
    profile.addAttribute("PUNCH_DAMAGE", 6, 0);
    profile.addAttribute("SPRINT_SPEED", 0.6, 1);
    profile.addAttribute("REACH_DISTANCE", 2.8, 1);
    profile.addAttribute("MAX_HEALTH", 26.0, 0);
}

function getTierOverride(entity) {
    if (entity.getData("pkg:dyn/turbo")) {
        return 7;
    }
    if (entity.getData("pkg:dyn/flight")) {
        return 5;
    }
    if (entity.getData("pkg:dyn/strength")) {
        return 4;
    }
    if (entity.getData("pkg:dyn/flight")) {
        return 1;
    }
    if (entity.getData("pkg:dyn/turbo")) {
        return 6;
    }
    if (entity.getData("pkg:dyn/super")) {
        return 6;
    }
    if (entity.getData("fiskheroes:punchmode")) {
        return 4;
    }

    return 2;
}

function webSwingingKey(player, manager) {
    var flag = player.getData("fiskheroes:web_swinging");

    if (!flag) {
        manager.setDataWithNotify(player, "fiskheroes:prev_utility_belt_type", player.getData("fiskheroes:utility_belt_type"));
        manager.setDataWithNotify(player, "fiskheroes:utility_belt_type", -1);
    }

    manager.setDataWithNotify(player, "fiskheroes:web_swinging", !flag);
    return true;
}

function isModifierEnabled(entity, modifier) {
        switch (modifier.name()) {
            case "fiskheroes:web_swinging":
                return entity.getData("pkg:dyn/turbo"); entity.getHeldItem().isEmpty() && entity.getData("fiskheroes:utility_belt_type") == -1;
            case "fiskheroes:leaping":
                return entity.getData("pkg:dyn/strength")
            case "fiskheroes:invisibility":
                return entity.getData("pkg:dyn/stealth");
            case "fiskheroes:arrow_catching":
                return entity.getData("pkg:dyn/strength")
            case "fiskheroes:water_breathing":
                return entity.getData("pkg:dyn/scuba");
            case "fiskheroes:controlled_flight":
                return entity.getData("pkg:dyn/flight");
            case "fiskheroes:flight":
                return entity.getData("pkg:dyn/super");
            case "fiskheroes:heat_vision":
                return entity.getData("pkg:dyn/super");
            default:
                return true;
        }
        var data1 = entity.getData("pkg:dyn/phase_timer");
        var data2 = (entity.getData("fiskheroes:speeding") || entity.isSprinting())
        var syaw = entity.getData("pkg:dyn/startedyaw");
        var yaw = entity.rotYaw();
        var pitch = entity.rotPitch();
        var Jump = entity.getData("pkg:dyn/jump");
        var YDif = Math.round(entity.posY()) - entity.posY();
        switch (modifier.name()) {

            case "fiskheroes:flight":
                return entity.getData("fiskheroes:intangible") && data1 > 0 && data1 < 0.15 && data2;
            case "fiskheroes:intangibility":
                return ((data1 > 0 && data1 < 1 || !entity.isOnGround()) && data2 || entity.isSneaking());
            case "fiskheroes:charged_beam":
                switch (modifier.id()) {
                    case "speed_lightning":
                        return false;
                }
            case "fiskheroes:tentacles":
                if (entity.world().getBlock(entity.pos().add(0, YDif, 0.5)) == 'minecraft:air'
                    && entity.world().getBlock(entity.pos().add(0, YDif, -0.5)) == 'minecraft:air'
                    && entity.world().getBlock(entity.pos().add(0.5, YDif, 0)) == 'minecraft:air'
                    && entity.world().getBlock(entity.pos().add(-0.5, YDif, 0)) == 'minecraft:air' ||

                    Jump == true && entity.world().getBlock(entity.pos().add(0, YDif + 0, 1.5)) == 'minecraft:air'
                    && entity.world().getBlock(entity.pos().add(0, YDif + 0, -2.0)) == 'minecraft:air'
                    && entity.world().getBlock(entity.pos().add(2.0, YDif + 0, 0)) == 'minecraft:air'
                    && entity.world().getBlock(entity.pos().add(-2.0, YDif + 0, 0)) == 'minecraft:air'

                    || entity.isInWater() || pitch > 30 || yaw < syaw - 30 || yaw > syaw + 30 || !entity.getData("fiskheroes:speeding")) {
                    return false
                };

        }

        return true;
    }

    function isKeyBindEnabled(entity, keyBind) {
        switch (keyBind) {
            case "EARTHQUAKE":
                return !entity.getData("pkg:dyn/super") && entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/scuba")
            case "CHARGED_PUNCH":
                return entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/scuba")
            case "SUPER_SPEED":
                return !entity.getData("pkg:dyn/super") && entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/strength")
            case "INVISIBILITY":
                return !entity.getData("pkg:dyn/super") && entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/strength")
            case "LASH":
                return entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/strength")
            case "func_WEB_SWINGING":
                return !entity.getData("pkg:dyn/super") && entity.getData("pkg:dyn/lash") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/strength")
            case "WEB_ZIP":
                return !entity.getData("pkg:dyn/super") && entity.getData("pkg:dyn/lash") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/strength")
            case "TELEPORT":
                return entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/stealth")
            case "HEAT_VISION":
                return !entity.getData("pkg:dyn/stealth") && entity.getData("pkg:dyn/super") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/strength")
            case "GROUND_SMASH":
                return !entity.getData("pkg:dyn/stealth") && entity.getData("pkg:dyn/super") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/strength")

            case "TURBO":
                return !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/flight")
            case "FLIGHT":
                return !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/turbo")
            case "STRENGHT":
                return !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo")
            case "STEALTH":
                return !entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo")

            case "MORE":
                return !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/super") && !entity.getData("pkg:dyn/speed")

            case "TENTACLES":
                return entity.getHeldItem().isEmpty() && entity.getData("pkg:dyn/speed");
            case "SCUBA":
                return entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/speed") && !entity.getData("pkg:dyn/super")
            case "SPEED":
                return entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/scuba") && !entity.getData("pkg:dyn/super")
            case "SUPER":
                return entity.getData("pkg:dyn/more") && !entity.getData("pkg:dyn/stealth") && !entity.getData("pkg:dyn/turbo") && !entity.getData("pkg:dyn/flight") && !entity.getData("pkg:dyn/strength") && !entity.getData("pkg:dyn/scuba")

            default:
                return true;
        }

    }
    function defaultScale(entity) {
        if (entity.getData("pkg:dyn/turbo")) {
            return 1.0;
        }
        if (entity.getData("pkg:dyn/stealth")) {
            return 1.0;
        }
        if (entity.getData("pkg:dyn/speed")) {
            return 1.1;
        }
        if (entity.getData("pkg:dyn/scuba")) {
            return 1.1;
        }
        if (entity.getData("pkg:dyn/strength")) {
            return 1.5;
        }
        if (entity.getData("pkg:dyn/flight")) {
            return 1.1;
        }
        return 1.0;
    }

    function StartClimb(entity, manager) {
        manager.setData(entity, "pkg:dyn/startedclimb", true);
        manager.setData(entity, "pkg:dyn/startedyaw", entity.rotYaw());
        var syaw = entity.getData("pkg:dyn/startedyaw");
        return true;
    }

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

        if (entity.getData("pkg:dyn/turbo")) {
            return "TURBO";
        }
        if (entity.getData("pkg:dyn/stealth")) {
            return "STEALTH";
        }
        if (entity.getData("pkg:dyn/speed")) {
            return "SPEED";
        }
        if (entity.getData("pkg:dyn/scuba")) {
            return "SCUBA";
        }
        if (entity.getData("pkg:dyn/super")) {
            return "SUPER";
        }
        else if (entity.getData("fiskheroes:punchmode")) {
            return "STRENGHT";
        }
        else if (entity.getData("pkg:dyn/flight")) {
            return "FLIGHT";
        }
        return true;
    }
