extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "fan:lucifer/layer1",
    "layer2": "fan:lucifer/layer2",
    "wings": "fan:sn/misc/angel_wings",
    "hat": "fan:lucifer/hat",
    "devil": "fan:lucifer/devil",
    "devil_wings": "fan:lucifer/devil_wings",
    "eyes": "fan:lucifer/eyes",
    "tentacle": "fan:tentacle",
    "eyes_lights": "fan:lucifer/eyes_lights",
    "wings": "fan:sn/misc/angel_wings",
    "blank": "fan:null"
});

var utils = implement("fiskheroes:external/utils");
var devil;
var mask;
var eyes;
var wings;
var devil_wings;
var flames = implement("fiskheroes:external/flames");
var heatR;
var heatL;
var hand_flames;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        var conditions = entity.getData("fiskheroes:mask_open_timer2") == 0 && entity.getData("fan:dyn/devil_timer") == 0 && renderLayer == "HELMET" || entity.getData("fan:dyn/devil_timer") == 0 && renderLayer != "HELMET";
        return conditions ? "blank" : "devil";
    });

}

function initEffects(renderer) {
            var ock_arm = utils.createModel(renderer, "fan:model", "tentacle");
        var ock_claw = utils.createModel(renderer, "fan:luc", "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": [0, -9.5, -2.0], "direction": [-13.0, -3.0, -9.0] }
        ]);
        tentacles.anchor.set("body");
        tentacles.setSegmentModel(ock_arm);
        tentacles.setHeadModel(ock_claw);
        tentacles.segmentLength = 1;
        tentacles.segments = 100;

        ears = renderer.createEffect("fiskheroes:model");
        ears.setModel(utils.createModel(renderer, "fan:hat", "hat"));
        ears.anchor.set("head");
        ears.setScale(1);
        ears.setOffset(4, -11, 4);
        ears.setRotation(90, 90, -90)

    var beam = renderer.createResource("BEAM_RENDERER", "fan:extremis_heat_beam");
    heatR = utils.createLines(renderer, beam, 0xF2400D, [
        { "start": [0, 0.8, 0], "end": [0.0, -1.0, 0.0], "size": [2.5, 2.5, 2.5] },
    ]);
    heatR.setOffset(1.0, 8.0, 0.0).setScale(4.3);
    heatR.anchor.set("rightArm");
    heatR.mirror = true;

    heatL = utils.createLines(renderer, beam, 0xF2400D, [
        { "start": [0, 0.8, 0], "end": [0.0, -1.0, 0.0], "size": [2.5, 2.5, 2.5] },
    ]);
    heatL.setOffset(-1.0, 8.0, 0.0).setScale(4.3);
    heatL.anchor.set("leftArm");
    heatL.mirror = true;

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:cold_beam", "head", 0xF2400D, [
        { "firstPerson": [0, 3.0, 0.0], "offset": [0, -1.1, -4.0], "size": [6.0, 5.5] },
    ])

    var fire = renderer.createResource("ICON", "fiskheroes:fire_layer_%s");
    hand_flames = flames.createHands(renderer, fire, true);

    devil = renderer.createEffect("fiskheroes:overlay");
    mask = renderer.createEffect("fiskheroes:overlay");
    eyes = renderer.createEffect("fiskheroes:overlay");
    mask.texture.set("layer1");
    eyes.texture.set("eyes", "eyes_lights");

    utils.bindParticles(renderer, "fan:lucifer_devil");

    var model_wings = renderer.createResource("MODEL", "fan:lucwings");
    model_wings.texture.set("wings");
    model_wings.bindAnimation("fan:flap").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fan:dyn/wings_flight_timer"));
        data.load(1, (entity.getInterpolatedData("fan:dyn/wings_flight") % 1));
        data.load(2, entity.getInterpolatedData("fiskheroes:shield_blocking_timer"));
    });

    wings = renderer.createEffect("fiskheroes:model").setModel(model_wings);
    wings.setOffset(0, 1, -1.6);
    wings.setScale(1.4);
    wings.anchor.set("body");

    model_wings = renderer.createResource("MODEL", "fan:devil_wings");
    model_wings.texture.set("devil_wings");
    model_wings.bindAnimation("fan:devil_flap").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fan:dyn/wings_flight_timer"));
        data.load(1, (entity.getInterpolatedData("fan:dyn/wings_flight") % 1));
        data.load(2, 1);
    });

    devil_wings = renderer.createEffect("fiskheroes:model").setModel(model_wings);
    devil_wings.setOffset(0, 1, -1.6);
    devil_wings.setScale(1.4);
    devil_wings.anchor.set("body");

    utils.bindParticles(renderer, "fan:landing").setCondition(entity => entity.getData("fiskheroes:dyn/superhero_landing_timer") > 0);

    utils.bindParticles(renderer, "fan:fire_teleport").setCondition(entity => entity.getData('fiskheroes:teleport_timer') > 0);

}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    addAnimationWithData(renderer, "lucifier.TELEKINESIS", "fiskheroes:aiming", "fan:dyn/transform_timer").priority = 10;
    addAnimation(renderer, "lucifer.SNAP", "fan:snap").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fan:dyn/transform2_timer"));
        data.load(1, entity.getInterpolatedData("fan:dyn/transform2"));
    });

    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.AIMING");
    renderer.removeCustomAnimation("basic.BLOCKING");
    renderer.removeCustomAnimation("basic.CHARGED_BEAM");

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

    addAnimationWithData(renderer, "wings.LAND", "fiskheroes:superhero_landing", "fiskheroes:dyn/superhero_landing_timer").priority = -8;

    utils.addAnimationEvent(renderer, "FLIGHT_DIVE", "fiskheroes:iron_man_dive");
    utils.addAnimationEvent(renderer, "FLIGHT_DIVE_ROLL", "fiskheroes:falcon_dive_roll");

    addAnimationWithData(renderer, "wings.ROLL", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer").priority = 10;

    addAnimation(renderer, "lucifer.HOLOSTAND", "fan:luciferHolo")
        .setData((entity, data) => {
            var f = entity.getInterpolatedData("fan:dyn/holoanim");
            data.load(f <= 1 ? f : 1);
        });
}

function render(entity, renderLayer, isFirstPersonArm) {
    var isHolo = entity.is("DISPLAY") && entity.as("DISPLAY").getDisplayType() == "HOLOGRAM";
    devil.opacity = 1 - entity.getInterpolatedData("fan:dyn/devil_timer");
    if (renderLayer == "HELMET") {
        mask.opacity = Math.min(devil.opacity, 1 - entity.getInterpolatedData("fiskheroes:mask_open_timer2"));
        mask.render();

        var fire = [entity.getData("fan:dyn/fire"), entity.getInterpolatedData("fan:dyn/fire_timer")]
        eyes.opacity = Math.max(fire[1] * 2, Number(isHolo));
        eyes.render();
    } else {
        devil.texture.set(renderLayer == "LEGGINGS" ? "layer2" : "layer1");
        devil.render();
    }
        if (renderLayer == "CHESTPLATE") {
        heatR.progress = entity.getInterpolatedData("fiskheroes:blade");
        heatR.render();
        heatL.progress = entity.getInterpolatedData("fiskheroes:blade");
        heatL.render();
        hand_flames.render(entity.getInterpolatedData("fiskheroes:blade"));
        hand_flames.render();
    }
    var timer = Math.min(entity.getInterpolatedData("fan:dyn/wings_timer") * 2, 1);
    devil.opacity = Math.max(0, devil.opacity - 0.5) * 2
        if ((!isFirstPersonArm || entity.getData("fiskheroes:shield_blocking")) && renderLayer == "CHESTPLATE") {
            wings.anchor.ignoreAnchor(isFirstPersonArm && entity.getData("fiskheroes:shield_blocking"));
            wings.opacity = timer * devil.opacity || Number(isHolo);
            wings.setOffset(0, 1, -1.6 + 5.0 * isFirstPersonArm);
            wings.setRotation(isFirstPersonArm ? -Math.max(-40, Math.min(entity.rotPitch(), 60)) * 0.8 : 0, 0, 0);
            wings.setScale(1.4 * timer || 1.4 * Number(isHolo));
            wings.render();

            devil_wings.opacity = timer * Math.max(0, entity.getInterpolatedData("fan:dyn/devil_timer") - 0.5) * 2;
            devil_wings.setOffset(0, 1, -1.6);
            devil_wings.setScale(1.4 * timer);
            devil_wings.render();
        }
         ears.render();
}
