extend("fiskheroes:hero_basic");
loadTextures({
    "chainsaw": "skarlet:ash_williams/chainsaw",
    "chainsaw2": "skarlet:ash_williams/chainsaw2",
	"boomstick": "skarlet:ash_williams/boomstick"
});

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

var chainsaw;
var chainsaw_back;
var model_chainsaw;
 
var boomstick_hand;
var boomstick_hand_fp;

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 "layer2";
        }
        return "layer1";
    });
}

function initEffects(renderer) {
    model_chainsaw = renderer.createResource("MODEL", "skarlet:chainsaw");
    model_chainsaw.texture.set("chainsaw");
	
    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);
	
    var model_boomstick = renderer.createResource("MODEL", "skarlet:boomstick");
    model_boomstick.texture.set("boomstick");
	
    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);
	
    var model_boomstick_fp = renderer.createResource("MODEL", "skarlet:boomstick");
    model_boomstick_fp.texture.set("boomstick");
    model_boomstick_fp.bindAnimation("skarlet:shotgun").setData((entity, data) => data.load(entity.getInterpolatedData("fiskheroes:aiming_timer")));
	
    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);
	
	utils.bindParticles(renderer, "skarlet:chainsaw").setCondition(entity => entity.getData('fiskheroes:blade_timer') >= 0.3);
    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "skarlet:bullet", "leftArm", 0xB97317, [
        { "firstPerson": [5.0, 3.75, -15.0], "offset": [0.0, 18.8, -3.0], "size": [2.0, 2.0] }
    ]);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.AIMING");
    addAnimationWithData(renderer, "ash.AIMING", "skarlet:aiming_left", "fiskheroes:aiming_timer");
    addAnimationWithData(renderer, "ash.BELT", "skarlet:belt", "fiskheroes:blade_timer");
    addAnimationWithData(renderer, "ash.CHAINSAW", "skarlet: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) {
			if (entity.ticksExisted() % 5 >= 0 && entity.ticksExisted() % 5 < 2.5) {
				model_chainsaw.texture.set("chainsaw2");
			} else {
				model_chainsaw.texture.set("chainsaw");
			}
			chainsaw.render();
		} else {
			model_chainsaw.texture.set("chainsaw");
			chainsaw_back.render();
		}
	}
}
