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("skarredheroes: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("BLADE", "Toggle Blades", 1);
    hero.addKeyBind("BUSTER_SWORD_TRANSFORM", "Toggle Buster Sword", 1);
    hero.addKeyBind("SHIELD", "key.wingShield", 2);
    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.setAttributeProfile(getAttributeProfile);
    hero.setTierOverride(getTierOverride);
	hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.addSoundOverrides("creetle", {
		"powers": {
			"skarredheroes:scarab": {
				"fiskheroes:controlled_flight": {
					"ENABLE": ["fiskheroes:falcon_flight_enable", "fiskheroes:cap_sam_flight_loop", "fiskheroes:cap_sam_flight_ambient_loop", "skarredheroes:beetle_flight_loop", "skarredheroes:beetle_flight_start", "skarredheroes:creetle_flight_loop"]
				}
			}
		}
    });
    hero.addDamageProfile("BLADE", {
        "types": {
            "SHARP": 1.0,
            "ELECTRICITY": 0.7
        }
    });
    hero.addDamageProfile("BUSTER_SWORD", {
        "types": {
            "SHARP": 1.0,
            "BLUNT": 1.0,
            "ELECTRICITY": 0.7
        }
    });
	
    hero.setTickHandler((entity, manager) => {
        if (!entity.isSneaking() && !entity.isOnGround() && entity.motionY() < -0.8 && !entity.getData("skarredheroes:dyn/scarab")) {
            manager.setData(entity, "skarredheroes:dyn/scarab", true);
            manager.setData(entity, "fiskheroes:flying", true);
			entity.playSound("skarredheroes:scarab.enable", 1, 1);
        }
		
		if (!entity.getData("skarredheroes:dyn/scarab_save") && entity.getHealth() < 5) {
            manager.setData(entity, "skarredheroes:dyn/scarab_save", true);
            manager.setData(entity, "skarredheroes:dyn/scarab", true);
			entity.playSound("skarredheroes:scarab.enable", 1, 1);
        }
        if (entity.getData("fiskheroes:time_since_damaged") < 5 && entity.getHealth() < 6 && entity.getData("skarredheroes:dyn/scarab_save")) {
            manager.setData(entity, "skarredheroes:dyn/scarab_save", false);
        }
        
        if (entity.getHealth() > 6 && entity.getData("skarredheroes:dyn/scarab_save")) {
            manager.setData(entity, "skarredheroes: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("skarredheroes:dyn/scarab_timer") != 1 || !entity.getHeldItem().isEmpty()) {
			manager.setData(entity, "skarredheroes:dyn/buster_sword", false);
		}
    });
}

function getTierOverride(entity) {
    return entity.getData("skarredheroes: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("skarredheroes:dyn/scarab") && !entity.getData("fiskheroes:flying") && entity.isOnGround() && !entity.getData("fiskheroes:blade") && !entity.getData("skarredheroes:dyn/buster_sword");
		case "BLADE":
            return (entity.getData("skarredheroes:dyn/buster_sword_timer") == 0 || entity.isBookPlayer()) && entity.getData("skarredheroes:dyn/scarab");
		case "BUSTER_SWORD_TRANSFORM":
            return (entity.getData("fiskheroes:blade_timer") > 0 || entity.getData("skarredheroes:dyn/buster_sword_timer") > 0 || entity.isBookPlayer()) && entity.getData("skarredheroes:dyn/scarab");
		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("skarredheroes:dyn/scarab")) {
        return "INACTIVE";
    }
	
    if (entity.getData("skarredheroes: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 "BREATHE_SPACE":
            return entity.getData("skarredheroes:dyn/scarab");
        default:
            return false;
    }
}

function isModifierEnabled(entity, modifier) {
	switch (modifier.name()) {
		case "fiskheroes:blade":
			return entity.getData("skarredheroes:dyn/scarab");
		case "fiskheroes:controlled_flight":
			return entity.getData("skarredheroes:dyn/scarab");
        case "fiskheroes:water_breathing":
            return entity.getData("skarredheroes:dyn/scarab");
		case "fiskheroes:fire_immunity":
            return entity.getData("skarredheroes:dyn/scarab");
		case "fiskheroes:projectile_immunity":
            return entity.getData("skarredheroes:dyn/scarab");
		case "fiskheroes:regeneration":
            return entity.getData("skarredheroes:dyn/scarab");
		default:
			return true;
	}
}
