var utils = implement("fiskheroes:external/utils");
function init(hero) {
    hero.setName("Alastor");
    hero.setVersion("New");
    hero.setTier(8);
    
    hero.setChestplate("item.superhero_armor.piece.chestpiece");
    hero.addPrimaryEquipment("fisktag:weapon{WeaponType:fan:microphone}", true, item => item.nbt().getString("WeaponType") == 'fan:microphone');
    hero.addPrimaryEquipment("fisktag:weapon{WeaponType:fan:plush}", true, item => item.nbt().getString("WeaponType") == 'fan:plush');

    
    hero.addPowers("fan:radio_demon_physiology");
    hero.addAttribute("FALL_RESISTANCE", 1, 1);
    hero.addAttribute("PUNCH_DAMAGE", 11.0, 0);
    hero.addAttribute("MAX_HEALTH", 1.0, 0);
    hero.addAttribute("SPRINT_SPEED", 0.60, 1)
    hero.addAttribute("STEP_HEIGHT", 0.5, 0); 
    hero.addAttribute("JUMP_HEIGHT", 3.0, 0)
    
    hero.addKeyBind("TENTACLES", "Toggle Tentacles", 1);
    hero.addKeyBind("SHADOWFORM", "key.shadowForm", 2);
    hero.addKeyBind("SHADOWDOME", "key.shadowDome", 3);
    hero.addKeyBindFunc("func_GIANT_MODE", giantModeKey, "key.giantMode", 4);
    hero.addKeyBind("TENTACLE_JAB", "key.tentacleJab", 2);
    hero.addKeyBind("TENTACLE_GRAB", "key.tentacleGrab", 3);
    hero.addKeyBind("TENTACLE_STRIKE", "key.tentacleStrike", 4);
    hero.addKeyBind("SHIELD", "key.forcefield", 5);

	
    hero.setDefaultScale(1.0);
	hero.setTickHandler(tickHandler);
	hero.setKeyBindEnabled(isKeyBindEnabled);
	hero.setHasPermission(hasPermission);
	hero.supplyFunction("canAim", canAim);
	hero.setModifierEnabled(isModifierEnabled);
	
	hero.addSoundEvent("STEP", "fan:muds");
	hero.addAttributeProfile("STAFF", staffProfile);
    hero.setAttributeProfile(getProfile);
    hero.setDamageProfile(getProfile);
    hero.addDamageProfile("STAFF", {
            "types": {
            "SHARP": 0.25,
            "BLUNT": 0.75
        }     
    });
	
	function staffProfile(profile) {
        profile.inheritDefaults();
        profile.addAttribute("PUNCH_DAMAGE", 9, 0);
    }
    
    function getProfile(entity) {
        return entity.getData("fiskheroes:blade") ? "STAFF" : null;
    }
}

function isModifierEnabled(entity, modifier) {
	switch (modifier.name()) {
        case "fiskheroes:energy_projection":
            return entity.getData("fiskheroes:moving"); 
        default:
            return true;
        }
}

function ShapeShiftReset(entity, manager) {
    manager.setDataWithNotify(entity, "fiskheroes:shape_shift_timer", 1);
    manager.setDataWithNotify(entity, "fiskheroes:shape_shifting_to", null);
    return true;
}

function hasPermission(entity, permission) {
    return permission == "USE_GUN" && entity.getData("fan:dyn/joker_form") || entity.getData("fan:dyn/face_form");
}

function canAim(entity) {
    return entity.getData("fan:dyn/joker_form") || entity.getData("fan:dyn/face_form");
} 

function tickHandler(entity, manager) {
	
		if (entity.getData("fiskheroes:speeding")) {
            manager.setData(entity, "fiskheroes:energy_projection", true);
        }
		 if (!entity.getData("fiskheroes:speeding")) {
            manager.setData(entity, "fiskheroes:energy_projection", false);
        }
		
	
		if (entity.getData("fiskheroes:shape_shifting_to") == "Joker") {
            manager.setData(entity, "fan:dyn/joker_form", true);
        } 
        if (entity.getData("fiskheroes:shape_shifting_to") != "Joker") {
            manager.setData(entity, "fan:dyn/joker_form", false);
        }
		
		if (entity.getData("fiskheroes:shape_shifting_to") == "The Batman") {
            manager.setData(entity, "fan:dyn/batman_form", true);
       } 
       if (entity.getData("fiskheroes:shape_shifting_to") != "The Batman") {
            manager.setData(entity, "fan:dyn/batman_form", false);
        }
		
		if (entity.getData("fiskheroes:shape_shifting_to") == "Two-Face") {
            manager.setData(entity, "fan:dyn/face_form", true);
        } 
        if (entity.getData("fiskheroes:shape_shifting_to") != "Two-Face") {
            manager.setData(entity, "fan:dyn/face_form", false);
        }
		
		if (entity.getData("fiskheroes:shape_shifting_to") == "The Flash") {
            manager.setData(entity, "fan:dyn/flash_form", true);
        } 
        if (entity.getData("fiskheroes:shape_shifting_to") != "The Flash") {
            manager.setData(entity, "fan:dyn/flash_form", false);
        }
		
		if (!entity.getData("fiskheroes:dyn/giant_mode")) {
            manager.setData(entity, "fiskheroes:blade", false);
        }
	
		manager.incrementData(entity, "fan:dyn/giant_timer", 20, 25, entity.getData("fiskheroes:dyn/giant_mode"))
		
		manager.incrementData(entity, "fan:dyn/change_form_timer", 20, 25, entity.getData("fiskheroes:speeding"))

        // if (entity.getData("fan:dyn/change_form_timer") > 0.1) {
        //     manager.setData(entity, "fiskheroes:scale", 1 + (entity.getData("fan:dyn/change_form_timer") / 2));
        // } else {
        //     manager.setData(entity, "fiskheroes:scale", 1);
        // }

		if (entity.getData("fan:dyn/giant_timer") > 0.1 || entity.getData("fan:dyn/change_form_timer") > 0.1) {
            manager.setData(entity, "fiskheroes:scale", 1 + ((entity.getData("fan:dyn/giant_timer") + entity.getData("fan:dyn/change_form_timer")) / 2));
        } else {
            manager.setData(entity, "fiskheroes:scale", 1);
        }
		
		 if (entity.getData("fan:dyn/giant_timer") == false) {
            manager.setData(entity, "fan:dyn/change_form", false);
        }
		
		
	
		manager.incrementData(entity, "fan:dyn/punch_int", 1, 8, (entity.getData("fiskheroes:blade") && entity.isPunching() && entity.getData('fan:dyn/punch_int') < 1 && entity.getHeldItem().isEmpty()
			|| entity.getData("fiskheroes:blade") && entity.isPunching() && entity.getData('fan:dyn/punch_int') < 1 && entity.getHeldItem().isEmpty()));	

		
        
		manager.incrementData(entity, "fan:dyn/move_timer2", 1, 1, entity.getData("fiskheroes:moving"));


        var move_timer = entity.getData("fan:dyn/move_timer")
        if (entity.getData("fan:dyn/move_timer2") == 1) {
            manager.setData(entity, "fan:dyn/move_timer", move_timer + 0.02);
            manager.setData(entity, "fan:dyn/move_timer2", 0);

        }

        
        if (!entity.getData("fiskheroes:speeding")) {
            manager.setData(entity, "fan:dyn/move_timer", 0);
        }





        }


function giantModeKey(player, manager) {
    var flag = player.getData("fiskheroes:dyn/giant_mode");
    manager.setData(player, "fiskheroes:dyn/giant_mode", !flag);
    manager.setData(player, "fiskheroes:size_state", flag ? -1 : 1);
    return true;
}

function isKeyBindEnabled(entity, keyBind) {
    if (keyBind == "TENTACLE_JAB" || keyBind == "TENTACLE_GRAB" || keyBind == "TENTACLE_STRIKE") {
        return entity.getData("fiskheroes:tentacles") != null;
        return keyBind == "func_GIANT_MODE";
    }
    
    if (keyBind == "TENTACLE_JAB" || keyBind == "TENTACLE_GRAB" || keyBind == "TENTACLE_STRIKE") {
        return entity.getData("fiskheroes:tentacles") != null;
    }
    if (entity.getData("fiskheroes:tentacles")) {
        return keyBind == "TENTACLES"
    }
    if (entity.getData("fiskheroes:shadowform")) {
        return keyBind == "SHADOWFORM";
    }
    else if (entity.getData("fiskheroes:lightsout")) {
        return keyBind == "SHADOWDOME";
        
    }
    return true;
}