extend("fiskheroes:hero_basic");
loadTextures({
    "null": "pkg:null",
    //Daylight 
    "amulet": "pkg:amulet",
    "daylight": "pkg:daylight_suit",
    "shield": "pkg:trollhunters_shield",
    "glaives": "pkg:glaives",
    "sword": "pkg:trollhunters_sword",
    "mask": "pkg:daylight_suit_helmet",
    "helmet": "pkg:daylight_suit_helmet_open",
    //Eclipse
    "eclipse_amulet": "pkg:eclipse_amulet",
    "eclipse": "pkg:eclipse_suit",
    "shield_eclipse": "pkg:trollhunters_shield_eclipse",
    "eclipse_sword": "pkg:trollhunters_sword_eclipse",
    "eclipse_glaives": "pkg:trollhunters_sword_eclipse",
    "eclipse_helmet": "pkg:eclipse_suit_helmet",
    "eclipse_mask": "pkg:eclipse_suit_helmet",
});
var utils = implement("fiskheroes:external/utils");

var daylight

var eclipse
var shield_eclipse
var eclipse_glaives
var eclipse_sword

var glow1
var glow

var glaives;
var sword;

function init(renderer) {
    parent.init(renderer);
    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm", "rightLeg", "leftLeg");
    renderer.fixHatLayer("CHESTPLATE");
    renderer.setLights((entity, renderLayer) => {
        if (!entity.isDisplayStand() && renderLayer == "CHESTPLATE") {
            var timer = entity.getInterpolatedData("pkg:dyn/eclipse_timer");
            return timer == 0 ? "amulet" : timer < 1 ? "amulet" : "amulet";
        }
        return "amulet";
    });
    renderer.setTexture((entity, renderLayer) => {
        if (entity.getData("fiskheroes:mask_open_timer2") > 0) {
            return "mask";
        }
        if (!entity.isDisplayStand() && renderLayer == "CHESTPLATE") {
            var timer = entity.getInterpolatedData("pkg:dyn/daylight_timer");
            return timer == 0 ? "amulet" : timer < 1 ? "daylight" : "daylight";
        }
        return "daylight";
    });
}

function initEffects(renderer) {
    parent.initEffects(renderer);
    utils.bindParticles(renderer, "pkg:turbo_energy_blue").setCondition(entity => entity.getInterpolatedData('pkg:dyn/daylight_timer') > 0 && entity.getInterpolatedData('pkg:dyn/daylight_timer') < 1);
    utils.bindParticles(renderer, "pkg:turbo_smoke").setCondition(entity => entity.getInterpolatedData('pkg:dyn/daylight_timer') > 0 && entity.getInterpolatedData('pkg:dyn/daylight_timer') < 1);
    utils.bindParticles(renderer, "pkg:turbo_smoke").setCondition(entity => entity.getInterpolatedData('pkg:dyn/daylight_timer') > 0 && entity.getInterpolatedData('pkg:dyn/daylight_timer') < 1);

    shield_eclipse = renderer.createEffect("fiskheroes:shield");
    shield_eclipse.texture.set("shield_eclipse");
    shield_eclipse.anchor.set("rightArm");
    shield_eclipse.setCurve(25.0, 35.0);

    eclipse_sword = renderer.createEffect("fiskheroes:model");
    eclipse_sword.setModel(utils.createModel(renderer, "pkg:trollhuntersswordeclipse", "eclipse_sword"));
    eclipse_sword.anchor.set("rightArm");
    eclipse_sword.setOffset(2, 8, -1.55)
    eclipse_sword.setScale(1.0)
    eclipse_sword.setRotation(90, 90, 0)

    utils.bindParticles(renderer, "pkg:eclipse").setCondition(entity => entity.getInterpolatedData('pkg:dyn/eclipse_timer') > 0.125 && entity.getInterpolatedData('pkg:dyn/eclipse_timer') < 1);
    utils.bindParticles(renderer, "pkg:eclipse_red").setCondition(entity => entity.getInterpolatedData('pkg:dyn/eclipse_timer') > 0.125 && entity.getInterpolatedData('pkg:dyn/eclipse_timer') < 1);

    eclipse = renderer.createEffect("fiskheroes:overlay");
    eclipse.texture.set("eclipse", "eclipse_amulet");

    daylight = renderer.createEffect("fiskheroes:overlay");
    daylight.texture.set("daylight", "amulet");

    glow = renderer.createEffect("fiskheroes:glowerlay");
    glow.color.set(0xFA0D02);

    //glaives
    utils.bindBeam(renderer, "fiskheroes:lightning_cast", "pkg:glaives", "rightArm", 0x00FFFF, [
        { "firstPerson": [-8.0, 4.5, -10.0], "offset": [-0.5, 9.0, 0.0], "size": [0.75, 0.75] }
    ]);

    shield = renderer.createEffect("fiskheroes:shield");
    shield.texture.set("shield");
    shield.anchor.set("rightArm");
    shield.setCurve(25.0, 35.0);

    sword = renderer.createEffect("fiskheroes:model");
    sword.setModel(utils.createModel(renderer, "pkg:trollhunterssword", "sword"));
    sword.anchor.set("rightArm");
    sword.setOffset(2, 8, -1.55)
    sword.setScale(1.0)
    sword.setRotation(90, 90, 0)

    var model_glaives = renderer.createResource("MODEL", "pkg:glaives");
    model_glaives.texture.set("glaives");
    glaives = renderer.createEffect("fiskheroes:model").setModel(model_glaives);
    glaives.anchor.set("rightArm");
    glaives.setScale(0.80);
    model_glaives.mirror = true;

    glow1 = renderer.createEffect("fiskheroes:glowerlay");
    glow1.color.set(0x00FFFF);
}

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 (renderLayer == "CHESTPLATE") {
        shield.unfold = (entity.getData("fiskheroes:shield_timer") > 0.5 && entity.getInterpolatedData("pkg:dyn/daylight_timer")); {
            shield.setOffset(2.9 + 1.35 * Math.min(shield.unfold * 5, 1), 5.0, 0.0);
            shield.render();
        }
        if (renderLayer == "CHESTPLATE" && (entity.getData("fiskheroes:blade_timer") > 0.5 && entity.getInterpolatedData("pkg:dyn/daylight_timer"))) {
            sword.render();
        }
        if (entity.getData("fiskheroes:dyn/nanite_timer") > 0.4) {
            var glaives_anchor = glaives.anchor.set("rightArm");
            var glaives_offSet = glaives.setOffset(2, 8, -4.0);
            var glaives_rot = glaives.setRotation(90, 135, 0);

            glaives.anchor = glaives_anchor, glaives_offSet, glaives_rot;
            glaives.render();
        }
        if (renderLayer == "CHESTPLATE") {
            daylight.opacity = entity.getInterpolatedData("pkg:dyn/daylight_timer");
            daylight.render();
        }
        if (renderLayer == "CHESTPLATE") {
            eclipse.opacity = entity.getInterpolatedData("pkg:dyn/eclipse_timer");
            eclipse.render();
        }
        if (renderLayer == "CHESTPLATE" && (entity.getData("fiskheroes:blade_timer") > 0.5 && entity.getInterpolatedData("pkg:dyn/eclipse_timer"))) {
            eclipse_sword.render();
        }
        if (renderLayer == "CHESTPLATE") {
            shield_eclipse.unfold = (entity.getData("fiskheroes:shield_timer") > 0.5 && entity.getInterpolatedData("pkg:dyn/eclipse_timer")); {
                shield_eclipse.setOffset(2.9 + 1.35 * Math.min(shield.unfold * 5, 1), 5.0, 0.0);
                shield_eclipse.render();
            }
            if (entity.getData("pkg:dyn/daylight_timer") || entity.isDisplayStand()) {
                if (renderLayer == "CHESTPLATE") {

                }
            }
            glow1.opacity = entity.getData('pkg:dyn/daylight_timer') > 0 && entity.getData('pkg:dyn/daylight_timer') < 1;
            glow1.render();

            glow.opacity = entity.getData('pkg:dyn/eclipse_timer') > 0 && entity.getData('pkg:dyn/eclipse_timer') < 1;
            glow.render();

        }
    }
}