extend("fiskheroes:hero_basic");
loadTextures({
    "base": "pkg:shazam_base",
    "cape": "pkg:shazam_cape.tx.json",
    "lights": "pkg:shazam_lights",
    "null": "pkg:null"
});

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

var bolt;
var bolt2;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if (!entity.isDisplayStand() && renderLayer == "CHESTPLATE") {
            var timer = entity.getInterpolatedData("pkg:dyn/shazam_timer");
            return timer == 0 ? "null" : timer < 1 ? "base" : "base";
        }
        return "base";
    });
    renderer.setLights((entity, renderLayer)=> {
        if (!entity.isDisplayStand() && renderLayer == "CHESTPLATE") {
            var timer = entity.getInterpolatedData("pkg:dyn/shazam_timer");
            return timer == 0 ? "null" : timer < 1 ? "lights" : "lights";
        }
        return "lights";
    });

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

function initEffects(renderer) {
    var physics = renderer.createResource("CAPE_PHYSICS", null);
    physics.maxFlare = 0.4;
    cape = capes.createDefault(renderer, 24, "fiskheroes:cape_default.mesh.json", physics);
    cape.effect.texture.set("cape");
    cape.effect.width = 15;

    utils.bindTrail(renderer, "pkg:shazam_transform").setCondition(entity => entity.getData("pkg:dyn/shazam_timer") < 1
        && entity.getData("pkg:dyn/shazam_timer") > 0);

    bolt = utils.createLines(renderer, "pkg:shazam", 0xD9FFFF, [
        { "start": [0.0, 25.0, 0.0], "end": [0.0, -512, 0.0], "size": [70, 70] },
    ]);
    bolt.anchor.set("body");

    bolt2 = utils.createLines(renderer, "pkg:shazam", 0xD9FFFF, [
        { "start": [0.0, 0.0, 0.0], "end": [0.0, -128, 0.0], "size": [70, 70] },
    ]);
    bolt2.anchor.set("body");

    utils.bindParticles(renderer, "pkg:shazam_smoke").setCondition(entity => entity.getInterpolatedData('pkg:dyn/shazam_timer') > 0 && entity.getInterpolatedData('pkg:dyn/shazam_timer') < 1);
    utils.bindParticles(renderer, "pkg:shazam_transform").setCondition(entity => entity.getInterpolatedData('pkg:dyn/shazam_timer') > 0 && entity.getInterpolatedData('pkg:dyn/shazam_timer') < 1);
    utils.bindParticles(renderer, "pkg:shazam_lightning").setCondition(entity => entity.getInterpolatedData('pkg:dyn/shazam_timer') > 0 && entity.getInterpolatedData('pkg:dyn/shazam_timer') < 1);

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

    // energy projection
    utils.bindBeam(renderer, "fiskheroes:energy_projection", "pkg:lightning", "body", 0xD9FFFF, [
        { "firstPerson": [0.0, 6.0, 0.0], "offset": [0.0, 5.0, -4.0], "size": [4.0, 4.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_energy_projection"));

    //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.75, 0.75] }
    ]);
    utils.bindTrail(renderer, "pkg:shazam_transform").setCondition(entity => entity.getData("pkg:dyn/shazam_timer") > 0 && entity.getData("pkg:dyn/shazam_timer") < 1);
    utils.bindTrail(renderer, "pkg:shazam_flight").setCondition(entity => entity.getData("fiskheroes:flying"));
    utils.bindTrail(renderer, "pkg:wizard_shazam");
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimation(renderer, "firestorm.FLIGHT", "fiskheroes:flight/propelled_hands.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;

    addAnimationWithData(renderer, "antimonitor.ANTIBLAST", "fiskheroes:aiming", "fiskheroes:beam_charge");
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (entity.getData("pkg:dyn/shazam") || entity.isDisplayStand()) {
        if (renderLayer == "CHESTPLATE") {

            if (!isFirstPersonArm && renderLayer == "CHESTPLATE") {
                cape.render(entity);
            }
        }
    }
}
