extend("fiskheroes:hero_basic");
loadTextures({
    "eyes": "pkg:lightning_eye",
    "stormbreaker": "pkg:stormbreaker",
    "layer1": "pkg:null"
});

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


var overlay;
var bolt;
var bolt2;
var glow;
var glyph;

var stormbreaker;
var stormbreaker_back;


function init(renderer) {
    parent.init(renderer);
    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm");
    renderer.fixHatLayer("CHESTPLATE");
}

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

    stormbreaker = renderer.createEffect("fiskheroes:model");
    stormbreaker.setModel(utils.createModel(renderer, "pkg:stormbreaker", "stormbreaker", null));
    stormbreaker.anchor.set("rightArm");
    stormbreaker.mirror = false;

    stormbreaker_back = renderer.createEffect("fiskheroes:model");
    stormbreaker_back.setModel(utils.createModel(renderer, "pkg:stormbreaker", "stormbreaker", null));
    stormbreaker_back.anchor.set("rightArm");
    stormbreaker_back.mirror = false;

    overlay = renderer.createEffect("fiskheroes:overlay");
    overlay.texture.set(null, "eyes");

    utils.bindParticles(renderer, "pkg:bifrost").setCondition(entity => entity.getInterpolatedData('fiskheroes:teleport_timer') > 0 && entity.getInterpolatedData('fiskheroes:teleport_timer') < 1);
    utils.bindParticles(renderer, "pkg:bifrost_smoke").setCondition(entity => entity.getInterpolatedData('fiskheroes:teleport_timer') > 0 && entity.getInterpolatedData('fiskheroes:teleport_timer') < 1);
    utils.bindTrail(renderer, "pkg:shazam_flight").setCondition(entity => entity.getData("fiskheroes:flying"));
    utils.bindTrail(renderer, "pkg:wizard_shazam");
    utils.bindTrail(renderer, "pkg:lightning_beam");
    utils.bindTrail(renderer, "pkg:asgardian_trails").setCondition(entity => entity.getInterpolatedData('fiskheroes:dyn/steel_timer') > 0 && entity.getInterpolatedData('fiskheroes:dyn/steel_timer') < 610);

    overlay = renderer.createEffect("fiskheroes:overlay");
    overlay.texture.set(null, "eyes");

    // charged beam
    utils.bindBeam(renderer, "fiskheroes:charged_beam", "pkg:lightning", "rightArm", 0xD9FFFF, [
        { "firstPerson": [0.80, 0.80, -1.0], "offset": [0.0, 4.0, -8.0], "size": [4.0, 4.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_antimatter"));

    // energy manipulation
    utils.bindBeam(renderer, "fiskheroes:energy_manipulation", "pkg:lightning", "rightArm", 0xD9FFFF, [
        { "firstPerson": [-2.5, 0.0, -7.0], "offset": [-0.5, 19.0, -12.0], "size": [1.5, 1.5] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_antimatter"));

    //lightning cast
    utils.bindBeam(renderer, "fiskheroes:lightning_cast", "fiskheroes:lightning_cast", "rightArm", 0xD9FFFF, [
        { "firstPerson": [-8.0, 4.5, -10.0], "offset": [-0.5, 9.0, 0.0], "size": [0.85, 0.85] }
    ]);
}


function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimation(renderer, "firestorm.FLIGHT", "fiskheroes:flight/default_arms_forward.anim.json");
    utils.addHoverAnimation(renderer, "firestorm.HOVER", "fiskheroes:flight/idle/propelled_hands");
    addAnimationWithData(renderer, "iron_man.LAND", "fiskheroes:superhero_landing", "fiskheroes:dyn/superhero_landing_timer")
        .priority = -8;

    renderer.removeCustomAnimation("basic.AIMING");
    renderer.removeCustomAnimation("basic.ENERGY_PROJ");
    renderer.removeCustomAnimation("basic.CHARGED_BEAM");
    addAnimation(renderer, "basic.AIMING", "fiskheroes:aiming").setData((entity, data) => {
        var charge = entity.getInterpolatedData("fiskheroes:beam_charge");
        data.load(Math.max(entity.getInterpolatedData("fiskheroes:aiming_timer"), entity.getData("fiskheroes:beam_charging") ? Math.min(charge * 3, 1) : Math.max(charge * 5 - 4, 0)));
    });
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
        var charge = entity.getInterpolatedData("fiskheroes:beam_charge");
        //glyph.render(entity.getData("fiskheroes:beam_charging") ? Math.min(charge * 2, 1) : Math.max(charge * 3 - 2, 0));
        if (entity.getData("fiskheroes:teleport_timer") < 1 && entity.getData("fiskheroes:teleport_timer") > 0) {
            //glow.opacity = 1 - entity.getInterpolatedData("fiskheroes:teleport_timer");
            //glow.render();
            //bolt.render();
            //bolt2.render();
        }
        stormbreaker.opacity = !entity.getInterpolatedData("pkg:dyn/stormbreaker");
        stormbreaker.render();
    }
    if (renderLayer == "CHESTPLATE") {
        var back_anchor = stormbreaker.anchor.set("body");
        var back_offSet = stormbreaker.setOffset(-9.0, 8, 3.4);
        var back_rot = stormbreaker.setRotation(90, 35, 90);

        stormbreaker_back.anchor = back_anchor, back_offSet, back_rot;
        stormbreaker_back.opacity = entity.getInterpolatedData("pkg:dyn/stormbreaker");
        stormbreaker_back.render();
    }
    overlay.opacity = entity.getInterpolatedData("fiskheroes:dyn/steeled");
    overlay.render();
    if (renderLayer == "CHESTPLATE") {
        overlay.opacity = entity.getInterpolatedData("fiskheroes:beam_charge");
        overlay.render();
    }
    overlay.opacity = entity.getInterpolatedData("fiskheroes:dyn/steeled");
    overlay.render();

    if (renderLayer == "CHESTPLATE") {
        overlay.opacity = entity.getInterpolatedData("fiskheroes:dyn/steeled");
        overlay.render();
    }
    overlay.opacity = entity.getInterpolatedData("fiskheroes:beam_charge");
    overlay.render();

    if (renderLayer == "CHESTPLATE") {
        overlay.opacity = entity.getInterpolatedData("fiskheroes:beam_charge");
        overlay.render();
    }
}