extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "skarredheroes:silk/silk_layer1",
    "layer2": "skarredheroes:silk/silk_layer2",
    "mask_layer1": "skarredheroes:silk/silk_mask_layer1",
    "claws": "skarredheroes:silk/silk_claws"
});

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

var chest;
var claws;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if ((renderLayer == "HELMET" || renderLayer == "CHESTPLATE") && (entity.isDisplayStand() || entity.isBookPlayer() ? entity.getData("fiskheroes:mask_open") : entity.getData("fiskheroes:mask_open_timer2") > 0.35)) {
            return "mask_layer1";
        }
        return renderLayer == "LEGGINGS" ? "layer2" : "layer1";
    });
}

function initEffects(renderer) {
    var model_claws = renderer.createResource("MODEL", "skarredheroes:claws");
    model_claws.texture.set("claws");

    claws = renderer.createEffect("fiskheroes:model").setModel(model_claws);
    claws.anchor.set("rightArm");

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

    renderer.bindProperty("fiskheroes:equipment_wheel").color.set(0xA50414);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);

    addAnimation(renderer, "spiderman.MASK", "skarredheroes:remove_mask")
        .setData((entity, data) => {
            var f = entity.getInterpolatedData("fiskheroes:mask_open_timer2");
            data.load(f < 1 ? f : 0);
        });

    renderer.removeCustomAnimation("basic.AIMING");

    addAnimationWithData(renderer, "spiderman.AIMING", "fiskheroes:web_aim_right", "fiskheroes:web_aim_right_timer")
        .priority = 2;

    addAnimationWithData(renderer, "spiderman.AIMING_LEFT", "fiskheroes:web_aim_left", "fiskheroes:web_aim_left_timer")
        .priority = 2;

    addAnimationWithData(renderer, "spiderman.WEB_RAPPEL", "fiskheroes:web_rappel", "fiskheroes:web_rappel_timer")
        .priority = 5;

    utils.addAnimationEvent(renderer, "WEBSWING_DEFAULT", ["fiskheroes:swing_default", "fiskheroes:swing_default2"]);
    utils.addAnimationEvent(renderer, "WEBSWING_RIGHT", "fiskheroes:swing_right");
    utils.addAnimationEvent(renderer, "WEBSWING_LEFT", "fiskheroes:swing_left");
    utils.addAnimationEvent(renderer, "WEBSWING_TRICK_DEFAULT", [
        "skarredheroes:swinging/front_flip2",
        "skarredheroes:swinging/front_flip3",
        "fiskheroes:swing_roll",
        "fiskheroes:swing_roll2",
        "fiskheroes:swing_roll5"
    ]);
    utils.addAnimationEvent(renderer, "WEBSWING_TRICK_RIGHT", ["fiskheroes:swing_rotate_right"]);
    utils.addAnimationEvent(renderer, "WEBSWING_TRICK_LEFT", ["fiskheroes:swing_rotate_left"]);
    utils.addAnimationEvent(renderer, "WEBSWING_ZIP", "fiskheroes:swing_zip");
    utils.addAnimationEvent(renderer, "WEBSWING_DIVE", ["skarredheroes:swinging/dive2", "fiskheroes:swing_dive", "fiskheroes:swing_dive2"]);
    utils.addAnimationEvent(renderer, "WEBSWING_LEAP", "fiskheroes:swing_springboard");
    utils.addAnimationEvent(renderer, "WEBSWING_SHOOT_RIGHT", "fiskheroes:web_swing_shoot_right");
    utils.addAnimationEvent(renderer, "WEBSWING_SHOOT_LEFT", "fiskheroes:web_swing_shoot_left");
    utils.addAnimationEvent(renderer, "WEBSHOOTER_SHOOT_RIGHT", "fiskheroes:web_shoot_right");
    utils.addAnimationEvent(renderer, "WEBSHOOTER_SHOOT_LEFT", "fiskheroes:web_shoot_left");
    utils.addAnimationEvent(renderer, "CEILING_CRAWL", "fiskheroes:crawl_ceiling");
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
        if (!isFirstPersonArm) {
            chest.render();
        }
        var unfold = entity.getInterpolatedData("fiskheroes:blade_timer");
        claws.opacity = unfold;
        claws.setOffset(1.5, -12.0 + 1.35 * Math.min(unfold * 5, 3.5), -1.0);
        claws.render();
    }
}