var speedster_base = implement("diminpack:external/speedster_base");

function init(hero) {
    hero.setName("Savitar");
    hero.setVersion("Diminpack Enhanced Remake");
    hero.setTier(7);

    hero.setHelmet("item.superhero_armor.piece.helmet");
    hero.setChestplate("item.superhero_armor.piece.chestplate");
    hero.setLeggings("item.superhero_armor.piece.leggings");
    hero.setBoots("item.superhero_armor.piece.boots");
    hero.addEquipment("fiskheroes:flash_ring");
    hero.addPrimaryEquipment("fiskheroes:flash_ring",true);
    hero.addPowers("diminpack:speed_force", "diminpack:savitar_armor");
    hero.addAttribute("PUNCH_DAMAGE", 6.0, 0);
    hero.addAttribute("WEAPON_DAMAGE", 0.5, 0);
    hero.addAttribute("JUMP_HEIGHT", 1.0, 0);
    hero.addAttribute("FALL_RESISTANCE", 4.5, 0);
    hero.addAttribute("BASE_SPEED_LEVELS", 7.0, 0);

    hero.addKeyBind("SUPER_SPEED", "Move Fast", 1);
    hero.addKeyBind("BLADE", "And Stab things", 4);
    hero.addKeyBind("SLOW_MOTION", "key.slowMotion", 2);
    hero.addKeyBind("MINIATURIZE_SUIT", "Miniaturize Suit", 3);
    hero.addKeyBind("INTANGIBILITY", "Phase", 5);
    hero.addKeyBind("SENTRY_MODE", "key.sentryMode", 3);

    
    hero.setModifierEnabled(isModifierEnabled);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.setDefaultScale(1.25);
    hero.setHasProperty((entity, property) => property == "BREATHE_SPACE");

    hero.setAttributeProfile(entity => entity.getData("fiskheroes:blade") ? "BLADE" : null);
    hero.addAttributeProfile("BLADE", profile => {
        profile.inheritDefaults();
        profile.addAttribute("PUNCH_DAMAGE", 8.0, 0);
    });

    var speedPunch = speedster_base.createSpeedPunch(hero, {
        "types": {
            "BLUNT": 1.0,
            "SHARP": 0.2
        },
        "properties": {
            "HIT_COOLDOWN": 5
        }
    });
    hero.setDamageProfile(entity => entity.getData("fiskheroes:blade") ? "BLADE" : speedPunch.get(entity, null));
    hero.addDamageProfile("BLADE", { "types": { "SHARP": 1.0 } });

    hero.addSoundEvent("LAND", "fiskheroes:savitar_land");
    hero.addSoundEvent("STEP", ["fiskheroes:savitar_walk1", "fiskheroes:savitar_walk2", "fiskheroes:savitar_sprint"]);
    hero.addSoundOverrides("SAVITAR", speedster_base.mergeSounds("fiskheroes:speed_force", speedster_base.SOUNDS_SAVITAR));
    hero.addSoundOverrides("BARRY", speedster_base.mergeSounds("fiskheroes:speed_force", speedster_base.SOUNDS_BARRY));
    hero.addSoundOverrides("RGB", speedster_base.mergeSounds("fiskheroes:speed_force", speedster_base.SOUNDS_RGB));

    hero.setTickHandler((entity, manager) => {
        speedster_base.tick(entity, manager);
    });
}

function isKeyBindEnabled(entity, keyBind) {
    if (keyBind == "INTANGIBILITY") {
        return entity.getData("fiskheroes:speed") > 1 && entity.getData("fiskheroes:dyn/intangibility_cooldown") == 0 && entity.getData("fiskheroes:speeding") && entity.isSprinting() && !(entity.isOnGround());
    }
    switch (keyBind) {
    case "SENTRY_MODE":
        return !entity.isSneaking();
    case "MINIATURIZE_SUIT":
        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();
    default:
        return true;
    }
    return true;
}


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