extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "fan:marvel/electro/electro_layer1",
    "layer2": "fan:marvel/electro/electro_layer2",
    "head": "fan:marvel/electro/ears",
    "stat": "fan:marvel/electro/stat",
    "lights_layer1": "fan:marvel/electro/electro_lights_layer1",
    "lights_layer2": "fan:marvel/electro/electro_lights_layer2",
    "tentacle": "fan:tentacle",
    "helmet": "fan:marvel/electro/galan_helmet"
});

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

var overlay;

var glow;

var stat;

var aura_head;
var aura_head2;
var aura_body;
var aura_arms;
var aura_arms2;
var aura_legs;
var aura_legs2;


function init(renderer) {
    parent.init(renderer);
    renderer.setLights((entity, renderLayer) => {
        return renderLayer == "HELMET" ? "head" : renderLayer == "LEGGINGS" ? "lights_layer2" : "lights_layer1";
    });
    renderer.setTexture((entity, renderLayer) => renderLayer == "HELMET" ? "head": renderLayer == "LEGGINGS" ? "layer2" : "layer1");

    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm");
    renderer.fixHatLayer("HELMET", "CHESTPLATE");
}

function initEffects(renderer) {
    utils.bindTrail(renderer, "fan:electro_no_flicker").setCondition(entity => entity.getData("fiskheroes:shadowform"));
    utils.bindTrail(renderer, "fan:electro").setCondition(entity => entity.getData("fiskheroes:teleport_timer") > 0);

            var ock_arm = utils.createModel(renderer, "fan:model", "tentacle");
        var ock_claw = utils.createModel(renderer, "fan:vox", "tentacle");
        ock_claw.bindAnimation("fiskheroes:ock_claw").setData((entity, data) => {
            var t = entity.as("TENTACLE");
            data.load(0, 1 - Math.min(t.getCaster().getInterpolatedData("fiskheroes:tentacle_extend_timer") * 2, 1));
            data.load(1, t.getIndex());
            data.load(2, t.getGrabTimer());
            data.load(3, t.getStrikeTimer());
            
        });
        
        utils.setOpacityWithData(renderer, 0.0, 1.0, "fiskheroes:shadowform_timer");
        utils.bindCloud(renderer, "fiskheroes:particle_cloud", "fiskheroes:shadow_smoke").setCondition(entity => entity.getData("fiskheroes:shadowform"));
        
        var tentacles = renderer.bindProperty("fiskheroes:tentacles").setTentacles([
            { "offset": [2.0, -4.5, -2.0], "direction": [13.0, 10.0, -10.0] },
            { "offset": [-2.0, -4.5, -2.0], "direction": [-13.0, 10.0, -10.0] },
            { "offset": [2.0, -3.5, -2.0], "direction": [14.0, -5.0, -10.0] },
            { "offset": [-2.0, -3.5, -2.0], "direction": [-14.0, -5.0, -10.0] },
            { "offset": [2.0, -7.5, -2.0], "direction": [13.0, -10.0, -10.0] },
            { "offset": [-2.0, -7.5, -2.0], "direction": [-13.0, -10.0, -10.0] }
        ]);
        tentacles.anchor.set("body");
        tentacles.setSegmentModel(ock_arm);
        tentacles.setHeadModel(ock_claw);
        tentacles.segmentLength = 1;
        tentacles.segments = 25;
 
    var model_galan_helmet = renderer.createResource("MODEL", "fan:galan_helmet");
    model_galan_helmet.texture.set("helmet");
    model_galan_helmet.generateMirror();
    galan_helmet = renderer.createEffect("fiskheroes:model").setModel(model_galan_helmet);
    galan_helmet.anchor.set("head");
    galan_helmet.setOffset(0.0, -21.0, 0.0);

    var model_stat = renderer.createResource("MODEL", "fan:galan_helmet");
    model_stat.texture.set("stat", "head");

    stat = renderer.createEffect("fiskheroes:model").setModel(model_stat);
    stat.setOffset(0.0, 0.0, 0.0).setRotation(0.0, 0.0, 0.0).setScale(1.0);
    stat.anchor.set("head");

    glow = renderer.createEffect("fiskheroes:glowerlay");
    glow.includeEffects(stat);
    glow.color.set(0x2EBAEC);

    utils.setOpacityWithData(renderer, 0.0, 1.0, "fiskheroes:shadowform_timer");

    //Aura Head
    aura_head = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [8.0, 8.0]}
    ]);
    aura_head.anchor.set("head");
    aura_head.setOffset(0.0, 0.0, 0.0).setRotation(0, 0, 0.0).setScale(16.0, 8.0, 16.0);
    aura_head.mirror = false;

    aura_head2 = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [8.0, 8.0]}
    ]);
    aura_head2.anchor.set("head");
    aura_head2.setOffset(0.0, -4.0, 4.0).setRotation(90, 0, 0.0).setScale(16.0, 8.0, 16.0);
    aura_head2.mirror = false;

    //Aura Body
    aura_body = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [4.0, 8.0]}
    ]);
    aura_body.anchor.set("body");
    aura_body.setOffset(0.0, 12.0, 0.0).setRotation(0, 0, 0.0).setScale(16.0, 12.0, 16.0);
    aura_body.mirror = false;

    //Aura Arms
    aura_arms = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [4.0, 4.0]}
    ]);
    aura_arms.anchor.set("rightArm");
    aura_arms.setOffset(1.0, 10.0, 0.0).setRotation(0, 0, 0.0).setScale(16.0, 12.0, 16.0);
    aura_arms.mirror = true;

    aura_arms2 = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [12.0, 4.0]}
    ]);
    aura_arms2.anchor.set("rightArm");
    aura_arms2.setOffset(1.0, 4.0, 2.0).setRotation(90, 0, 0.0).setScale(16.0, 4.0, 16.0);
    aura_arms2.mirror = true;

    //Aura Legs
    aura_legs = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [4.0, 4.0]}
    ]);
    aura_legs.anchor.set("rightLeg");
    aura_legs.setOffset(0.0, 12.0, 0.0).setRotation(0, 0, 0.0).setScale(16.0, 12.0, 16.0);
    aura_legs.mirror = true;

    aura_legs2 = utils.createLines(renderer, "fan:aura_beam", 0x1B36A6, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -1.0, 0.0], "size": [12.0, 4.0]}
    ]);
    aura_legs2.anchor.set("rightLeg");
    aura_legs2.setOffset(0.0, 6.0, 2.0).setRotation(90, 0, 0.0).setScale(16.0, 4.0, 16.0);
    aura_legs2.mirror = true;

    //Electro Beam
    utils.bindBeam(renderer, "fiskheroes:energy_projection", "fan:electro_energy_projection", "rightArm", 0x2EBAEC, [
        { "firstPerson": [-5.5, 4.75, -10.0], "offset": [-0.5, 9.0, 0.0], "size": [3.0, 3.0, -3.0] },
        { "firstPerson": [5.5, 4.75, -10.0], "offset": [0.5, 9.0, 0.0], "size": [3.0, 3.0, -3.0], "anchor": "leftArm" }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fan:electro_energy_projection_impact"));

    utils.bindBeam(renderer, "fiskheroes:lightning_cast", "fan:electro_lightning_cast", "rightArm", 0x2EBAEC, [
        { "firstPerson": [-5.5, 4.75, -10.0], "offset": [-0.5, 9.0, 0.0], "size": [0.5, 0.5, 0.5] }
    ]);
}

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

    addAnimation(renderer, "basic.AIMING", "fiskheroes:dual_aiming").setData((entity, data) => {
        data.load(Math.max(entity.getInterpolatedData("fiskheroes:aiming_timer")));
    });

    utils.addFlightAnimation(renderer, "vision.FLIGHT", "fiskheroes:flight/default.anim.json");
    utils.addHoverAnimation(renderer, "vision.HOVER", "fiskheroes:flight/idle/neutral");

    renderer.reprioritizeDefaultAnimation("PUNCH", -9);
    renderer.reprioritizeDefaultAnimation("AIM_BOW", -9);
}

function render(entity, renderLayer, isFirstPersonArm) {
    var teleport = entity.getInterpolatedData("fiskheroes:teleport_timer");
    var shadowform = entity.getInterpolatedData("fiskheroes:shadowform_timer");
    var aiming = entity.getInterpolatedData("fiskheroes:aiming_timer");

    glow.opacity = teleport || shadowform;
    glow.render();

    if (renderLayer == "HELMET") {
        galan_helmet.render();
        aura_head.opacity = teleport || shadowform;
        aura_head.render();
        aura_head2.opacity = teleport || shadowform;
        aura_head2.render();

        if (entity.getWornChestplate().nbt().getTagList("Equipment").tagCount() > 0) {
            stat.render();
        }

    }
    if (renderLayer == "CHESTPLATE") {
        aura_body.opacity = teleport || shadowform;
        aura_body.render();

        aura_arms.opacity = teleport || shadowform;
        aura_arms.render();
        aura_arms2.opacity = teleport || shadowform;
        aura_arms2.render();
    }
    if (renderLayer == "LEGGINGS") {
        aura_legs.opacity = teleport || shadowform;
        aura_legs.render();
        aura_legs2.opacity = teleport || shadowform;
        aura_legs2.render();
    }
}

function getEquipmentFromIndex(entity, index) {
    var nbt = entity.getWornChestplate().nbt();
    var equipment = nbt.getTagList("Equipment");
    for (var i=0; i<equipment.tagCount(); i++) {
        if (equipment.getCompoundTag(i).getByte("Index") == index) {
            return equipment.getCompoundTag(i).getCompoundTag("Item");
        }
    }
    return null;
}
