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

function init(hero) {
    hero.setName("Edge Runner");
    hero.setTier(5);
    
    hero.setHelmet("item.superhero_armor.piece.cowl");
    hero.setChestplate("item.superhero_armor.piece.chestpiece");
    hero.setLeggings("item.superhero_armor.piece.pants");
    hero.setBoots("item.superhero_armor.piece.boots");
    hero.addEquipment("fiskheroes:flash_ring");
    hero.addPrimaryEquipment("fiskheroes:beretta_93r{Dual:1}", true, item => item.nbt().getBoolean("Dual"));
    hero.addPrimaryEquipment("fiskheroes:flash_ring",true);
    hero.addPowers("diminpack:edge_suit");
    hero.addAttribute("PUNCH_DAMAGE", 5.1, 0);
    hero.addAttribute("JUMP_HEIGHT", 1.0, 0);
    hero.addAttribute("FALL_RESISTANCE", 1.0, 1);
    hero.addAttribute("BASE_SPEED_LEVELS", 6.0, 0);

    hero.addKeyBind("SUPER_SPEED", "key.superSpeed", 1);
    hero.addKeyBind("SLOW_MOTION", "key.slowMotion", 2);
    hero.addKeyBind("INTANGIBILITY", "Phase", 4);
    hero.addKeyBind("INVISIBILITY", "Engage Retrodisplay Panels", 3);
    hero.addKeyBind("SHAPE_SHIFT", "Display Alternate Identity", 5);
    hero.addKeyBind("AIM", "key.aim", -1);
    hero.addKeyBind("GUN_RELOAD", "key.reload", 4);
    hero.addKeyBind("MINIATURIZE_SUIT", "Miniaturize Suit", 5);
    
    hero.setHasPermission((entity, permission) => permission == "USE_GUN");
    hero.supplyFunction("canAim", canAim);

    hero.addAttributeProfile("SPEED", speedProfile);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.setModifierEnabled(isModifierEnabled);
    hero.setDamageProfile(getDamageProfile);
    hero.setAttributeProfile(getProfile);
    hero.addDamageProfile("SPEED_PUNCH", {
        "types": {
            "BLUNT": 1.0
        },
        "properties": {
            "HIT_COOLDOWN": 5
        }
    });
    hero.setTickHandler((entity, manager) => {
        manager.incrementData(entity, "fiskheroes:dyn/speed_sprint_timer", 4, entity.isSprinting() && entity.getData("fiskheroes:speed_sprinting") && entity.getData("fiskheroes:speed") > 0.5);
        if (entity.getData("fiskheroes:speed_sprinting") != entity.getData("fiskheroes:energy_charging")) {
            manager.setData(entity, "fiskheroes:energy_charging", entity.getData("fiskheroes:speed_sprinting"));
        }
    });
    hero.setTickHandler((entity, manager) => {
        utils.flightOnIntangibility(entity, manager);
    });
}

function getDamageProfile(entity) {
    return entity.getData("fiskheroes:speeding") ? "SPEED_PUNCH" : null;
}

function getProfile(entity) {
    if (entity.getData("fiskheroes:intangible")) {
        return "SPEED";
    }
}

function speedProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("STEP_HEIGHT", 1.0, 0);
}

function isModifierEnabled(entity, modifier) {
    if (modifier.name() == "fiskheroes:speed_disintegration") {
        return entity.getData("fiskheroes:speeding") && entity.getData("fiskheroes:speed") > 4;
    }
    if (modifier.name() == "fiskheroes:lightning_cast") {
        return entity.getData("fiskheroes:speed") > 1 && entity.getData("fiskheroes:speeding") && entity.isSprinting();
    }
    else if (modifier.name() == "fiskheroes:flight") {
        return entity.getData("fiskheroes:intangible");
    }
    return true;
}

function isKeyBindEnabled(entity, keyBind) {
    switch (keyBind) {
    case "MINIATURIZE_SUIT":
        return entity.isSneaking();
    case "SHAPE_SHIFT":
        return !entity.isSneaking();
    case "INTANGIBILITY":
        return entity.getData("fiskheroes:speed") > 1 && entity.getData("fiskheroes:dyn/intangibility_cooldown") == 0 && entity.getData("fiskheroes:speeding") && entity.isSprinting() && !entity.isOnGround();
    case "GUN_RELOAD":
        return entity.getHeldItem().isGun() && !entity.getData("fiskheroes:aiming");
    default:
        return true;
    }
}
function canAim(entity) {
    return entity.getHeldItem().isGun() && !(entity.isSprinting() && entity.getData("fiskheroes:flying"));
}
