extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "hhp:jedi_1",
    "layer2": "hhp:jedi_2",

    "lightsaber": "hhp:lightsabers/base",
});

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

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if (renderLayer == "LEGGINGS") {
            return entity.getWornChestplate().suitType() == $SUIT_NAME ? "pants" : "layer2";
        }
        return renderLayer == "BOOTS" ? "boots" : "layer1";
    });

    renderer.showModel("CHESTPLATE", "body", "rightArm", "leftArm");
}

var lightsaberModel, lightsaberModelHip;
var lightsaberModelResource = "hhp:lightsabers/base";
var lightsaberTexture = "lightsaber";

var lightsaberColor = 0x0000FF;
var bladeOffset = {"start": [-1.2, 8.5, -3.5], "end": [-1.2, 8.5, -23.5], "size": [20.0, 20.0]}

var lightsaber;

var hasCape = false;
var cape;
function initEffects(renderer) {

    if (hasCape) {
        var physics = renderer.createResource("CAPE_PHYSICS", null);
        physics.weight = 0.9;
        physics.maxFlare = 0.5;
        cape = capes.createDefault(renderer, 24, "fiskheroes:cape_default.mesh.json", physics);
        cape.effect.texture.set("cape");
    }

    renderer.setItemIcons(["jedi_0", "jedi_1", "jedi_2", "jedi_3"]); // This isn't in init because the other suits override init

    lightsaberModel = renderer.createEffect("fiskheroes:model");
    lightsaberModel.setModel(utils.createModel(renderer, lightsaberModelResource, lightsaberTexture));
    lightsaberModel.anchor.set("rightArm");
    lightsaberModel.setScale(0.5);
    lightsaberModel.setRotation(90, 180, 0);
    lightsaberModel.setOffset(1, 9, -10);

    var lightsaberEffect = renderer.createResource("BEAM_RENDERER", "hhp:lightsaber");

    lightsaber = utils.createLines(renderer, lightsaberEffect, lightsaberColor, [
        bladeOffset,
    ]);
    lightsaber.anchor.set("rightArm");


    lightsaberModelHip = renderer.createEffect("fiskheroes:model");
    lightsaberModelHip.setModel(utils.createModel(renderer, lightsaberModelResource, lightsaberTexture));
    lightsaberModelHip.anchor.set("body");
    lightsaberModelHip.setScale(0.5);
    lightsaberModelHip.setRotation(180, 0, 0);
    lightsaberModelHip.setOffset(4.75, 22.5, 0);

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "hhp:none", "rightArm", 0xFFFFFF, [
        { "firstPerson": [-4.5, 3.75, -8.0], "offset": [-2.5, 6.5, 0.75], "size": [0.0, 0.0] }
    ])
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.AIMING");

    addAnimationWithData(renderer, "yoda.AIMTELE", "fiskheroes:aiming", "hhp:dyn/telekinesis_timer");
    addAnimationWithData(renderer, "yoda.AIMPUSH", "fiskheroes:aiming", "fiskheroes:beam_charge");
    addAnimationWithData(renderer, "yoda.LEAP", "hhp:roll", "hhp:dyn/leaping_timer");

    renderer.removeCustomAnimation("basic.BLOCKING");
    addAnimationWithData(renderer, "the.BLOCKING", "fiskheroes:blocking", "fiskheroes:shield_blocking_timer")
        .priority = -5;
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
        if (entity.getData("fiskheroes:blade")) {
            lightsaberModel.render();
            if (entity.getInterpolatedData("hhp:dyn/lightsaber_timer") > 0) {
                lightsaber.progress = entity.getInterpolatedData("hhp:dyn/lightsaber_timer");
                lightsaber.render();
            }
        } else {
            lightsaberModelHip.render();
        }
        if (cape) {
            cape.render(entity);
        }
    }
}