extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "gowhp:atreus/atreus_ragnarok_layer1",
    "layer2": "gowhp:atreus/atreus_ragnarok_layer2",
    "bow": "gowhp:atreus/atreus_talon_bow",
    "arrow": "gowhp:atreus/atreus_arrow",
    "quiver": "gowhp:atreus/atreus_quiver",
    "runic_wolves": "gowhp:atreus/runic_wolves_ragnarok",
    "eyes": "gowhp:atreus/atreus_ragnarok_eyes",
    "wolfform": "gowhp:atreus/wolfform",
    "wolfform_lights": "gowhp:atreus/wolfform_lights",
});

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

var glow;

function init(renderer) {
    parent.init(renderer);
    renderer.setLights((entity) => entity.getInterpolatedData("gowhp:dyn/wolfform_timer") ? "eyes" : null);
    initEffects(renderer);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    addAnimation(renderer, "atreus.WOLFFORM", "gowhp:wolfform")
        .setData((entity, data) => data.load(1.0))
        .setCondition((entity) => entity.getData("gowhp:dyn/wolfform_timer") == 1)
        .priority = -10;
    addAnimation(renderer, "atreus.SPRINT", "gowhp:sprint_atreus")
        .setData((entity, data) => data.load(1.0))
        .setCondition(entity => (entity.isSprinting() && !entity.getData("gowhp:dyn/wolfform")))
        .priority = 21;
    renderer.removeCustomAnimation("basic.BLOCKING");
    addAnimationWithData(renderer, "atreus.BLOCKING", "fiskheroes:dual_aiming", "fiskheroes:shield_blocking_timer")
        .priority = -5;
    addAnimationWithData(renderer, "atreus.WOLFFORMTRANSFORMATIONCHARGE", "gowhp:wolfformtransformationcharge", "gowhp:dyn/wolfform_timer").setCondition(entity => (entity.getInterpolatedData("gowhp:dyn/wolfform_timer") > 0.0 && entity.getInterpolatedData("gowhp:dyn/wolfform_timer") < 0.5));
addAnimationWithData(renderer, "atreus.WOLFFORMTRANSFORMATION", "gowhp:wolfformtransformation", "gowhp:dyn/wolfform_timer").setCondition(entity => (entity.getInterpolatedData("gowhp:dyn/wolfform_timer") > 0.5 && entity.getInterpolatedData("gowhp:dyn/wolfform_timer") < 1));
}

function initEffects(renderer) {
    
    utils.addLivery(renderer, "COMPOUND_BOW", "bow");
    utils.addLivery(renderer, "ARROW", "arrow");
    utils.addLivery(renderer, "QUIVER", "quiver");

    var color = 0xff9a00;
    var tao_mandala = renderer.createResource("SHAPE", "fiskheroes:tao_mandala");
    var beam = renderer.createResource("BEAM_RENDERER", "fiskheroes:line");
    
    shield_idle = renderer.createEffect("fiskheroes:lines").setShape(tao_mandala).setRenderer(beam);
    shield_idle.setOffset(1.0, 11.0, 0.0).setRotation(0.0, 0.0, 10.0).setScale(4.8);
    shield_idle.color.set(color);
    shield_idle.anchor.set("rightArm");
    shield_idle.mirror = false;

    shield_big = renderer.createEffect("fiskheroes:lines").setShape(tao_mandala).setRenderer(beam);
    shield_big.color.set(color);
    shield_big.anchor.set("head");

    var runicsummonenergy_color = 0x9acd32;
    var runicsummonenergy_beam = renderer.createResource("BEAM_RENDERER", "gowhp:runicsummonenergy");

    runicsummonenergyarms = utils.createLines(renderer, runicsummonenergy_beam, runicsummonenergy_color, [
        {"start": [-0.3, -1.0, -1.1], "end": [-1.7, 4.5, -1.1], "size": [10.0, 10.0]},
        {"start": [-1.7, -1.0, -1.1], "end": [-0.3, 4.5, -1.1], "size": [10.0, 10.0]},
        {"start": [-1.7, 4.0, -1.1], "end": [-0.3, 6.5, -1.1], "size": [10.0, 10.0]},
        {"start": [-0.3, 4.0, -1.1], "end": [-1.7, 6.5, -1.1], "size": [10.0, 10.0]},

        {"start": [-0.3, -1.0, 1.1], "end": [-1.7, 4.5, 1.1], "size": [10.0, 10.0]},
        {"start": [-1.7, -1.0, 1.1], "end": [-0.3, 4.5, 1.1], "size": [10.0, 10.0]},
        {"start": [-1.7, 4.0, 1.1], "end": [-0.3, 6.5, 1.1], "size": [10.0, 10.0]},
        {"start": [-0.3, 4.0, 1.1], "end": [-1.7, 6.5, 1.1], "size": [10.0, 10.0]},

        {"start": [-1.8, -1.0, 1.0], "end": [-1.8, 4.5, -1.0], "size": [10.0, 10.0]},
        {"start": [-1.8, -1.0, -1.0], "end": [-1.8, 4.5, 1.0], "size": [10.0, 10.0]},
        {"start": [-1.8, 4.0, -1.0], "end": [-1.8, 6.5, 1.0], "size": [10.0, 10.0]},
        {"start": [-1.8, 4.0, 1.0], "end": [-1.8, 6.5, -1.0], "size": [10.0, 10.0]},
    ]);
    runicsummonenergyarms.anchor.set("rightArm");
    runicsummonenergyarms.setScale(1.5);
    runicsummonenergyarms.mirror = true;

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "gowhp:wrathofthewolf", "head", 0x9acd32, [
        { "offset": [12.0, 10.0, 3.0], "size": [6.0, 6.0] },
        { "offset": [-12.0, 10.0, 3.0], "size": [6.0, 6.0] }
    ]);

    var runic_wolves_walk = renderer.createResource("MODEL", "gowhp:runic_wolves_ragnarok");
    runic_wolves_walk.texture.set("runic_wolves");
    runic_wolves_walk.bindAnimation("gowhp:runicwolfwalk").setData((entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:beam_shooting_timer"));
    });
    runic_wolves_walk.bindAnimation("gowhp:runicwolfmove").setData((entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:beam_shooting_timer"));
    });
    runic_wolves = renderer.createEffect("fiskheroes:model").setModel(runic_wolves_walk);
    runic_wolves.anchor.set("head");

    var transformationsurge_color = 0xff9a00;
    var transformationsurge_beam = renderer.createResource("BEAM_RENDERER", "gowhp:transformationsurge");

    transformationsurgearms = utils.createLines(renderer, transformationsurge_beam, transformationsurge_color, [
        {"start": [-1.3, -1.0, -1.1], "end": [1.7, 6.5, -1.1], "size": [10.0, 10.0]},
        {"start": [1.7, -1.0, -1.1], "end": [0.3, 6.5, -1.1], "size": [10.0, 10.0]},

        {"start": [-1.3, -1.0, 1.1], "end": [1.7, 6.5, 1.1], "size": [10.0, 10.0]},
        {"start": [1.7, -1.0, 1.1], "end": [0.3, 6.5, 1.1], "size": [10.0, 10.0]},

        {"start": [1.8, -1.0, 1.0], "end": [1.8, 6.5, -1.0], "size": [10.0, 10.0]},
        {"start": [1.8, -1.0, -1.0], "end": [1.8, 6.5, 1.0], "size": [10.0, 10.0]},

        {"start": [-0.4, -1.0, 1.0], "end": [-0.4, 6.5, -1.0], "size": [10.0, 10.0]},
        {"start": [-0.4, -1.0, -1.0], "end": [-0.4, 6.5, 1.0], "size": [10.0, 10.0]},
    ]);
    transformationsurgearms.anchor.set("leftArm");
    transformationsurgearms.setScale(1.5);
    transformationsurgearms.mirror = true;

    transformationsurgelegs = utils.createLines(renderer, transformationsurge_beam, transformationsurge_color, [
        {"start": [-1.0, 0.0, -1.1], "end": [1.2, 7.5, -1.1], "size": [10.0, 10.0]},
        {"start": [1.2, 0.0, -1.1], "end": [-1.0, 7.5, -1.1], "size": [10.0, 10.0]},

        {"start": [-1.0, 0.0, 1.1], "end": [1.2, 7.5, 1.1], "size": [10.0, 10.0]},
        {"start": [1.2, 0.0, 1.1], "end": [-1.0, 7.5, 1.1], "size": [10.0, 10.0]},

        {"start": [-1.2, 0.0, 1.0], "end": [-1.2, 7.5, -1.0], "size": [10.0, 10.0]},
        {"start": [-1.2, 0.0, -1.0], "end": [-1.2, 7.5, 1.0], "size": [10.0, 10.0]},

        {"start": [1.2, 0.0, 1.0], "end": [1.2, 7.5, -1.0], "size": [10.0, 10.0]},
        {"start": [1.2, 0.0, -1.0], "end": [1.2, 7.5, 1.0], "size": [10.0, 10.0]},
    ]);
    transformationsurgelegs.anchor.set("rightLeg");
    transformationsurgelegs.setScale(1.5);
    transformationsurgelegs.mirror = true;

    transformationsurgebody = utils.createLines(renderer, transformationsurge_beam, transformationsurge_color, [
        {"start": [3.5, 0.0, -1.4], "end": [-3.5, 7.5, -1.4], "size": [10.0, 10.0]},
        {"start": [-3.5, 0.0, -1.4], "end": [3.5, 7.5, -1.4], "size": [10.0, 10.0]},

        {"start": [3.5, 0.0, 1.4], "end": [-3.5, 7.5, 1.4], "size": [10.0, 10.0]},
        {"start": [-3.5, 0.0, 1.4], "end": [3.5, 7.5, 1.4], "size": [10.0, 10.0]},
    ]);
    transformationsurgebody.anchor.set("body");
    transformationsurgebody.setScale(1.5);
    transformationsurgebody.mirror = false;

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

    var wolf_model = renderer.createResource("MODEL", "gowhp:wolfform");
    wolf_model.texture.set("wolfform", "wolfform_lights");
    wolf_model.bindAnimation("gowhp:wolf").setData((entity, data) => {
        data.load(0, entity.loop(40) * 8 * Math.PI);
        data.load(1, 1.0);
        data.load(2, entity.isSneaking());
    });
    wolf = renderer.createEffect("fiskheroes:model").setModel(wolf_model);
    wolf.anchor.set("body");
    wolf.setScale(2.5);
    wolf.setRotation(-90.0, 0.0, 0.0);
    wolf.setOffset(0.0, 0.0, 35.0);

    utils.bindParticles(renderer, "gowhp:wolfformglow");
    utils.bindParticles(renderer, "gowhp:wolfformtransformation").setCondition(entity => entity.getInterpolatedData("gowhp:dyn/wolfform_timer") > 0.0 && entity.getInterpolatedData("gowhp:dyn/wolfform_timer") < 1.0);
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
      var shield_timer = entity.getInterpolatedData("fiskheroes:shield_timer");
      var blocking_timer = entity.getInterpolatedData("fiskheroes:shield_blocking_timer");
      shield_idle.progress = shield_timer * (1 - blocking_timer);
      shield_big.progress = shield_timer * blocking_timer;

      if (isFirstPersonArm) {
        shield_big.setOffset(0, 7, -14);
        shield_big.setScale(16).setRotation(80, 0, 0);
        shield_big.anchor.ignoreAnchor(true);
      }
      else {
        var pitch = entity.rotPitch() * Math.PI / 180;
        shield_big.setOffset(0, 2 * Math.cos(pitch), -15 - 12 * Math.pow(Math.sin(Math.max(pitch, 0)), 2));
        shield_big.setScale(12).setRotation(90, 0, 0);
        shield_big.anchor.ignoreAnchor(false);
      }

      shield_idle.render();
      shield_big.render();
    }
    if (entity.getData("fiskheroes:beam_charge")){
        runicsummonenergyarms.progress = Math.max(1 - (1 - entity.getInterpolatedData('fiskheroes:beam_charge')) * 1, 0);
        runicsummonenergyarms.opacity = Math.max(1 - (1 - entity.getInterpolatedData('fiskheroes:beam_charge')) * 1, 0);
        runicsummonenergyarms.render();
    }
    if (entity.getData("fiskheroes:beam_shooting_timer")){
        runic_wolves.render();
    }
    if (entity.getInterpolatedData("gowhp:dyn/wolfform_timer") > 0.0  && entity.getInterpolatedData("gowhp:dyn/wolfform_timer") < 1.0){
        transformationsurgearms.progress = Math.max(1 - (1 - entity.getInterpolatedData('gowhp:dyn/wolfform_timer')) * 1, 0);
        transformationsurgearms.opacity = Math.max(1 - (1 - entity.getInterpolatedData('gowhp:dyn/wolfform_timer')) * 1, 0);
        transformationsurgearms.render();
        transformationsurgebody.progress = Math.max(1 - (1 - entity.getInterpolatedData('gowhp:dyn/wolfform_timer')) * 1, 0);
        transformationsurgebody.opacity = Math.max(1 - (1 - entity.getInterpolatedData('gowhp:dyn/wolfform_timer')) * 1, 0);
        transformationsurgebody.render();
        transformationsurgelegs.progress = Math.max(1 - (1 - entity.getInterpolatedData('gowhp:dyn/wolfform_timer')) * 1, 0);
        transformationsurgelegs.opacity = Math.max(1 - (1 - entity.getInterpolatedData('gowhp:dyn/wolfform_timer')) * 1, 0);
        transformationsurgelegs.render();
    }
    if (entity.getInterpolatedData("gowhp:dyn/wolfform_timer") > 0.8  && entity.getInterpolatedData("gowhp:dyn/wolfform_timer") < 1.0) {
        glow.opacity = true;
        glow.render();
    }
    if (entity.getInterpolatedData("gowhp:dyn/wolfform_timer") > 0.9) {
        wolf.render();
    }
}