extend("fiskheroes:hero_basic");
loadTextures({
    "base": "skarlet:wolverine/x23/base",
    "damage": "skarlet:wolverine/x23/damage.tx.json",
    "layer1": "skarlet:wolverine/x23/wolverine_layer1",
    "layer2": "skarlet:wolverine/x23/wolverine_layer2",
	"chestplate": "skarlet:wolverine/x23/wolverine_chestplate",
	"chest_damage": "skarlet:wolverine/x23/chest.tx.json",
    "chest": "skarlet:wolverine/x23/wolverine_chest",
	"mask": "skarlet:wolverine/x23/wolverine_mask",
    "null": "skarlet:null"
});

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

var head_model;
var body_model;
var rarm_model;
var larm_model;
var rleg_model;
var lleg_model;

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

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if (!entity.isWearingFullSuit() || (entity.is("DISPLAY") && entity.as("DISPLAY").getDisplayType() == "DISPLAY_STAND")) {
            if (renderLayer == "CHESTPLATE") {
                return entity.getWornHelmet().suitType() == $SUIT_NAME ? "layer1" : "chestplate";
            }
            return renderLayer == "HELMET" ? "mask" : renderLayer == "LEGGINGS" ? "layer2" : "layer1";
        } else if (entity.getHealth() < 12) {
            return "chest_damage";
        } else {
            return "chest";
        }
    });
    renderer.showModel("HELMET", "head", "headwear", "body");
}

function initEffects(renderer) {
    renderer.bindProperty("fiskheroes:opacity").setOpacity((entity, renderLayer) => {
        return entity.isWearingFullSuit() ? 0.999 : 1;
    });

    chest = renderer.createEffect("fiskheroes:chest");
    chest.setExtrude(1.2).setYOffset(1);

    //ears
    ears_model = renderer.createResource("MODEL", "skarlet:wolverine/ears");
    ears_model.texture.set("layer1");

    ears = renderer.createEffect("fiskheroes:model").setModel(ears_model);
    ears.anchor.set("head");

    //head
    head_model = renderer.createResource("MODEL", "skarlet:wolverine/head");
    head_model.texture.set("base");

    head = renderer.createEffect("fiskheroes:model").setModel(head_model);
    head.anchor.set("head");

    //body
    body_model = renderer.createResource("MODEL", "skarlet:wolverine/body");
    body_model.texture.set("base");

    body = renderer.createEffect("fiskheroes:model").setModel(body_model);
    body.anchor.set("body");

    //right arm
	rarm_model = renderer.createResource("MODEL", "skarlet:wolverine/x23/right_arm");
    rarm_model.bindAnimation("skarlet:wolverine_claws").setData((entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:blade_timer") * 2);
    });
    rarm_model.texture.set("base");

    rightarm = renderer.createEffect("fiskheroes:model").setModel(rarm_model);
    rightarm.anchor.set("rightArm");

    //left arm
	larm_model = renderer.createResource("MODEL", "skarlet:wolverine/x23/left_arm");
    larm_model.bindAnimation("skarlet:wolverine_claws").setData((entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:blade_timer") * 2);
    });
    larm_model.texture.set("base");

    leftarm = renderer.createEffect("fiskheroes:model").setModel(larm_model);
    leftarm.anchor.set("leftArm");

    //right leg
    rleg_model = renderer.createResource("MODEL", "skarlet:wolverine/x23/right_leg");
    rleg_model.bindAnimation("skarlet:wolverine_claws").setData((entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:blade_timer") * 2);
    });
    rleg_model.texture.set("base");

    rightleg = renderer.createEffect("fiskheroes:model").setModel(rleg_model);
    rightleg.anchor.set("rightLeg");

    //left leg
    lleg_model = renderer.createResource("MODEL", "skarlet:wolverine/x23/left_leg");
    lleg_model.bindAnimation("skarlet:wolverine_claws").setData((entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:blade_timer") * 2);
    });
    lleg_model.texture.set("base");

    leftleg = renderer.createEffect("fiskheroes:model").setModel(lleg_model);
    leftleg.anchor.set("leftLeg");

    utils.bindBeam(renderer, "fiskheroes:heat_vision", "skarlet:clear", "body", 0xFFFFFF, []);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
 
    addAnimation(renderer, "wolverine.PUNCH", "skarlet:double_punch")
        .setData((entity, data) => {
            data.load(entity.getPunchTimerInterpolated());
	}).setCondition(entity => entity.getData("fiskheroes:blade")).priority = -9;

    renderer.removeCustomAnimation("basic.HEAT_VISION");

	addAnimation(renderer, "wolverine.LUNGE", "skarlet:wolverine_lunge") 
		.setData((entity, data) => {
		data.load(0, entity.getInterpolatedData("skarlet:dyn/lunge_timer") * (1 - entity.getInterpolatedData("skarlet:dyn/landing_timer")));
        data.load(1, entity.getInterpolatedData("fiskheroes:heat_vision_timer"));
    });

    addAnimation(renderer, "wolverine.LAND", "skarlet:wolverine_lunge_land") 
		.setData((entity, data) => {
		data.load(entity.getInterpolatedData("skarlet:dyn/landing_timer"));
    }).priority = -8;
}

function render(entity, renderLayer, isFirstPersonArm) {
    var suit = entity.isWearingFullSuit() && !(entity.is("DISPLAY") && entity.as("DISPLAY").getDisplayType() == "DISPLAY_STAND");
    if (entity.getData("skarlet:dyn/damage_lock")) {
        head_model.texture.set("damage");
        body_model.texture.set("damage");
        if (entity.getData("skarlet:dyn/rarm_damage")) {
            rarm_model.texture.set("damage");
        }
        if (entity.getData("skarlet:dyn/larm_damage")) {
            larm_model.texture.set("damage");
        }
        if (entity.getData("skarlet:dyn/rleg_damage")) {
            rleg_model.texture.set("damage");
        }
        if (entity.getData("skarlet:dyn/lleg_damage")) {
            lleg_model.texture.set("damage");
        }
        if (entity.getHealth() < 1) {
            rarm_model.texture.set("damage");
            larm_model.texture.set("damage");
            rleg_model.texture.set("damage");
            lleg_model.texture.set("damage");
        }
    } else {
        head_model.texture.set("base");
        body_model.texture.set("base");
        rarm_model.texture.set("base");
        larm_model.texture.set("base");
        rleg_model.texture.set("base");
        lleg_model.texture.set("base");
    }
    
    if (suit && !isFirstPersonArm) {
        head.render();
        body.render();
        leftarm.render();
        rightleg.render();
        leftleg.render();
    }

    if (suit) {  
        rightarm.render();
    }

    if (!suit && !isFirstPersonArm) {
        ears.render();
    }

    if (renderLayer == "CHESTPLATE") {
        chest.render();
    }
}