extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "skarredheroes:ash_williams/ash_knight_layer1",
    "layer2": "skarredheroes:ash_williams/ash_knight_layer2",
    "chest": "skarredheroes:ash_williams/ash_knight_chest",
    "pants": "skarredheroes:ash_williams/ash_knight_pants",
    "shoes": "skarredheroes:ash_williams/ash_knight_shoes",
    "shoes_pants": "skarredheroes:ash_williams/ash_knight_shoes_pants",
    "chainsaw": "skarredheroes:ash_williams/chainsaw",
    "chainsaw2": "skarredheroes:ash_williams/chainsaw2",
    "chainsaw_layer1": "skarredheroes:ash_williams/chainsaw_knight_layer1",
	"cape": "skarredheroes:ash_williams/ash_knight_cape",
	"holster": "skarredheroes:ash_williams/knight_holster",
	"boomstick": "skarredheroes:ash_williams/boomstick"
});

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

var chainsaw;
var chainsaw_back;
var model_chainsaw;
 
var boomstick_hand;
var boomstick_hand_fp;
var cape;
var boomstick_back;

var holster;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if (renderLayer == "CHESTPLATE") {
			if (entity.getData('fiskheroes:blade_timer') >= 0.3) {
				return "chainsaw_layer1";
			} else {
            return entity.getWornLeggings().suitType() == $SUIT_NAME ? "layer1" : "chest";
			}
        }
        else if (renderLayer == "LEGGINGS") {
            return entity.getWornChestplate().suitType() == $SUIT_NAME ? "layer2" : "pants";
        }
		else if (renderLayer == "BOOTS") {
			return entity.getWornLeggings().suitType() == $SUIT_NAME ? "shoes_pants" : "shoes";
		}
		return "layer1";
    });
    renderer.showModel("CHESTPLATE", "body", "rightArm", "leftArm");
}

function initEffects(renderer) {
    var physics = renderer.createResource("CAPE_PHYSICS", null);
    physics.maxFlare = 0.3;
    physics.flareDegree = 1.5;
    physics.flareFactor = 1.2;
    physics.flareElasticity = 5;
    cape = capes.createDefault(renderer, 20, "fiskheroes:cape_default.mesh.json", physics);
    cape.effect.texture.set("cape");
    cape.effect.width = 14;
	
    model_chainsaw = renderer.createResource("MODEL", "skarredheroes:chainsaw");
    model_chainsaw.texture.set("chainsaw");
	
    var model_boomstick = renderer.createResource("MODEL", "skarredheroes:boomstick");
    model_boomstick.texture.set("boomstick");
	
    var model_boomstick_fp = renderer.createResource("MODEL", "skarredheroes:boomstick");
    model_boomstick_fp.texture.set("boomstick");
    model_boomstick_fp.bindAnimation("skarredheroes:shotgun").setData((entity, data) => data.load(entity.getInterpolatedData("fiskheroes:aiming_timer")));
	
    var model_holster = renderer.createResource("MODEL", "skarredheroes:holster");
    model_holster.texture.set("holster");
	
    var model_boomstick_back = renderer.createResource("MODEL", "skarredheroes:back_boomstick");
    model_boomstick_back.texture.set("boomstick");
	
    chainsaw_back = renderer.createEffect("fiskheroes:model").setModel(model_chainsaw);
    chainsaw_back.anchor.set("body");
    chainsaw_back.setOffset(17.0, 8.0, 10.0);
	chainsaw_back.setRotation(-90.0, -15.0, -90.0);
	
    chainsaw = renderer.createEffect("fiskheroes:model").setModel(model_chainsaw);
    chainsaw.anchor.set("rightArm");
    chainsaw.setOffset(-5.0, -2.0, 0.0);
	chainsaw.setRotation(0.0, 0.0, 0.0);
	
    boomstick_hand = renderer.createEffect("fiskheroes:model").setModel(model_boomstick);
    boomstick_hand.anchor.set("leftArm");
    boomstick_hand.setOffset(-0.8, 10.0, -24.5);
	boomstick_hand.setRotation(90.0, 0.0, 0.0);
	boomstick_hand.setScale(0.9);
	
    boomstick_hand_fp = renderer.createEffect("fiskheroes:model").setModel(model_boomstick_fp);
    boomstick_hand_fp.anchor.set("leftArm");
    boomstick_hand_fp.setOffset(-5.0, 5.0, -1.0);
	boomstick_hand_fp.setRotation(0.0, 0.0, 0.0);
	boomstick_hand_fp.setScale(0.9);
	
    holster = renderer.createEffect("fiskheroes:model").setModel(model_holster);
    holster.anchor.set("body");
    holster.setOffset(-0.6, 0.0, 0.0);
	
    boomstick_back = renderer.createEffect("fiskheroes:model").setModel(model_boomstick_back);
    boomstick_back.anchor.set("body");
    boomstick_back.setOffset(-0.4, 0.0, 0.4);
	boomstick_back.setScale(0.9);
	
	utils.bindParticles(renderer, "skarredheroes:chainsaw").setCondition(entity => entity.getData('fiskheroes:blade_timer') >= 0.3);
    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "fiskheroes:repulsor_blast", "leftArm", 0xFF7700, [
        { "firstPerson": [5.0, 3.75, -15.0], "offset": [0.0, 18.8, -3.0], "size": [1.0, 1.0] }
    ]);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.AIMING");
    addAnimationWithData(renderer, "ash.AIMING", "skarredheroes:aiming_left", "fiskheroes:aiming_timer");
    addAnimationWithData(renderer, "ash.BELT", "skarredheroes:belt", "fiskheroes:blade_timer");
    addAnimationWithData(renderer, "ash.CHAINSAW", "skarredheroes:sword_pose", "fiskheroes:blade_timer");
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
		if (isFirstPersonArm) {
			boomstick_hand_fp.anchor.ignoreAnchor(true);
            boomstick_hand_fp.render();
        } else {
			boomstick_hand_fp.anchor.ignoreAnchor(false);
        }
		if (entity.getData("fiskheroes:aiming")) {
			if (!isFirstPersonArm) {
				boomstick_hand.render();
			}
		}
		if (entity.getData('fiskheroes:blade_timer') >= 0.3) {
			chainsaw.render();
			if (entity.ticksExisted() % 5 >= 0 && entity.ticksExisted() % 5 < 2.5) {
				model_chainsaw.texture.set("chainsaw2");
			} else {
				model_chainsaw.texture.set("chainsaw");
			}
		} else {
			chainsaw_back.render();
			model_chainsaw.texture.set("chainsaw");
		}
	}
    if (renderLayer == "CHESTPLATE") {
		if (!isFirstPersonArm) {
			holster.render();
			cape.render(entity);
			if (!entity.getData("fiskheroes:aiming")) {
				boomstick_back.render();
			}
		}
	}
}
