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

function init(hero) {
    hero.setName("Blue Beetle");
    hero.setVersion("item.superhero_armor.version.dceu");
    hero.setTier(8);

    hero.setChestplate("Scarab");

    hero.addPowers("skarlet:scarab");
    hero.addAttribute("PUNCH_DAMAGE", 8.5, 0);
    hero.addAttribute("WEAPON_DAMAGE", 2, 0);
    hero.addAttribute("SPRINT_SPEED", 0.25, 1);
    hero.addAttribute("JUMP_HEIGHT", 1, 0);
    hero.addAttribute("FALL_RESISTANCE", 1.0, 1);

    hero.addKeyBind("AIM", "key.aim", 1);
    hero.addKeyBind("BLADE", "Toggle Blades", 2);
    hero.addKeyBind("BUSTER_SWORD_TRANSFORM", "Toggle Buster Sword", 2);
    hero.addKeyBind("SHIELD", "key.wingShield", 3);
    hero.addKeyBind("SCARAB_TRANSFORM", "Scarab Transformation", 5);
	
	hero.setModifierEnabled(isModifierEnabled);
    hero.setHasProperty(hasProperty);
    hero.addAttributeProfile("SHIELD", shieldProfile);
    hero.addAttributeProfile("BLADE", bladeProfile);
    hero.addAttributeProfile("BUSTER_SWORD", busterSwordProfile);
    hero.addAttributeProfile("INACTIVE", inactiveProfile);
    hero.supplyFunction("canAim", canAim);
    hero.setAttributeProfile(getAttributeProfile);
    hero.setTierOverride(getTierOverride);
	hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.addSoundOverrides("creetle", {
		"powers": {
			"skarlet:scarab": {
				"fiskheroes:controlled_flight": {
					"ENABLE": ["fiskheroes:falcon_flight_enable", "fiskheroes:cap_sam_flight_loop", "fiskheroes:cap_sam_flight_ambient_loop", "skarlet:beetle_flight_loop", "skarlet:beetle_flight_start", "skarlet:creetle_flight_loop"]
				}
			}
		}
    });
    hero.addDamageProfile("BLADE", {
        "types": {
            "SHARP": 1.0,
            "ELECTRICITY": 0.5
        }
    });
    hero.addDamageProfile("BUSTER_SWORD", {
        "types": {
            "SHARP": 1.0,
            "BLUNT": 1.0,
            "ELECTRICITY": 0.5
        }
    });

    
    hero.addSoundEvent("AIM_START", ["skarlet:beetle_aim_start", "skarlet:beetle_aim_crackle"]);
    hero.addSoundEvent("AIM_STOP", "skarlet:beetle_aim_crackle");
    hero.addSoundEvent("MASK_OPEN", "skarlet:beetle_mask");
    hero.addSoundEvent("MASK_CLOSE", "skarlet:beetle_mask");
	
    hero.setTickHandler((entity, manager) => {
        if (!entity.isSneaking() && !entity.isOnGround() && entity.motionY() < -0.8 && !entity.getData("skarlet:dyn/scarab")) {
            manager.setData(entity, "skarlet:dyn/scarab", true);
            manager.setData(entity, "fiskheroes:flying", true);
			entity.playSound("skarlet:scarab.enable", 1, 1);
        }
		
		if (!entity.getData("skarlet:dyn/scarab_save") && entity.getHealth() < 5) {
            manager.setData(entity, "skarlet:dyn/scarab_save", true);
            manager.setData(entity, "skarlet:dyn/scarab", true);
			entity.playSound("skarlet:scarab.enable", 1, 1);
        }
        if (entity.getData("fiskheroes:time_since_damaged") < 5 && entity.getHealth() < 6 && entity.getData("skarlet:dyn/scarab_save")) {
            manager.setData(entity, "skarlet:dyn/scarab_save", false);
        }
        
        if (entity.getHealth() > 6 && entity.getData("skarlet:dyn/scarab_save")) {
            manager.setData(entity, "skarlet:dyn/scarab_save", false);
        }
		
        var flying = !entity.isSprinting() && entity.getData("fiskheroes:flying");
        manager.incrementData(entity, "fiskheroes:dyn/wing_timer", 6, 7, flying);

        if (typeof tickHandler !== "undefined") {
            tickHandler(entity, manager);
        }
		if (entity.getData("skarlet:dyn/scarab_timer") != 1 || !entity.getHeldItem().isEmpty() || entity.getData("fiskheroes:aiming")) {
			manager.setData(entity, "skarlet:dyn/buster_sword", false);
		}
    });
}

function getTierOverride(entity) {
    return entity.getData("skarlet:dyn/scarab") ? 8 : 0;
}

function inactiveProfile(profile) {
    profile.revokeAugments();
}

function bladeProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 10.5, 0);
}

function busterSwordProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 14.5, 0);
    profile.addAttribute("BASE_SPEED", -0.25, 1);
    profile.addAttribute("SPRINT_SPEED", -0.35, 1);
    profile.addAttribute("JUMP_HEIGHT", -1.0, 1);
}

function isKeyBindEnabled(entity, keyBind) {
	switch (keyBind) {
		case "SHIELD":
			return entity.getData("skarlet:dyn/scarab") && !entity.getData("fiskheroes:flying") && entity.isOnGround() && !entity.getData("fiskheroes:blade") && !entity.getData("skarlet:dyn/buster_sword") && !entity.getData("fiskheroes:aiming");
		case "BLADE":
            return (entity.getData("skarlet:dyn/buster_sword_timer") == 0 || entity.isBookPlayer()) && entity.getData("skarlet:dyn/scarab") && !entity.getData("fiskheroes:aiming");
		case "BUSTER_SWORD_TRANSFORM":
            return (entity.getData("fiskheroes:blade_timer") > 0 || entity.getData("skarlet:dyn/buster_sword_timer") > 0 || entity.isBookPlayer()) && entity.getData("skarlet:dyn/scarab") && !entity.getData("fiskheroes:aiming");
        case "AIM":
            return entity.getData("skarlet:dyn/scarab") && !(entity.getData("fiskheroes:flying") && entity.isSprinting() && entity.getData("skarlet:dyn/buster_sword_timer") == 0 && entity.getData("fiskheroes:blade_timer") == 0)
		default:
			return true;
	}
}

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

function getAttributeProfile(entity) {
    if (!entity.getData("skarlet:dyn/scarab")) {
        return "INACTIVE";
    }
	
    if (entity.getData("skarlet:dyn/buster_sword")) {
        return "BUSTER_SWORD";
    }
	
    if (entity.getData("fiskheroes:blade")) {
        return "BLADE";
    }
	
    return entity.getData("fiskheroes:shield_blocking") ? "SHIELD" : null;
}

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

function isModifierEnabled(entity, modifier) {
	switch (modifier.name()) {
        case "fiskheroes:repulsor_blast":
            return entity.getData("fiskheroes:aimed_timer") == 1;
		case "fiskheroes:blade":
			return entity.getData("skarlet:dyn/scarab") && !entity.getData("fiskheroes:aiming");
		case "fiskheroes:controlled_flight":
			return entity.getData("skarlet:dyn/scarab");
        case "fiskheroes:water_breathing":
            return entity.getData("skarlet:dyn/scarab");
		case "fiskheroes:fire_immunity":
            return entity.getData("skarlet:dyn/scarab");
		case "fiskheroes:projectile_immunity":
            return entity.getData("skarlet:dyn/scarab");
		case "fiskheroes:regeneration":
            return entity.getData("skarlet:dyn/scarab");
		default:
			return true;
	}
}

function canAim(entity) {
    return entity.getHeldItem().isEmpty();
}