extend("gowhp:kratos");
loadTextures({
    "bladesofchaos": "gowhp:kratos/bladesofchaos",
    "bladesofchaos_lights": "gowhp:kratos/bladesofchaos_lights"
});

var bladeflames = implement("gowhp:external/bladesofchaos");
var bladechargeflames = implement("gowhp:external/bladesofchaoscharge");
var bladeflamesdisplay = implement("gowhp:external/bladesofchaosdisplay");

var bladesofchaos;
var blades_flame;
var bladescharge_flame;
var blades_flame_display;

function init(renderer) {
    parent.init(renderer);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    addAnimation(renderer, "kratos.DISPLAY", "gowhp:kratos_upgraded_display")
        .setData((entity, data) => data.load(1.0))
        .setCondition(entity => (entity.as("DISPLAY").getDisplayType() == "HOLOGRAM" || entity.as("DISPLAY").getDisplayType() == "DATABASE_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "FABRICATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "ITERATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "BOOK_PREVIEW"))
        .priority = 19;
    addAnimationWithData(renderer, "kratos.CYCLONEOFCHAOSCHARGE", "gowhp:cycloneofchaoscharge", "fiskheroes:beam_charge").setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0));
    addAnimationWithData(renderer, "kratos.CYCLONEOFCHAOSSPIN", "gowhp:cycloneofchaosspin", "fiskheroes:beam_shooting").setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0));
    addAnimationWithData(renderer, "kratos.FIREOFARESCHARGE", "gowhp:fireofarescharge", "fiskheroes:beam_charge").setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1 && entity.getData("fiskheroes:beam_charging") && !entity.getData("fiskheroes:beam_shooting")));
    addAnimationWithData(renderer, "kratos.FIREOFARES", "gowhp:fireofares", "fiskheroes:beam_charge").setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1 && entity.getData("fiskheroes:beam_shooting")));
    addAnimationWithData(renderer, "kratos.FIREOFARESDONE", "gowhp:fireofares", "fiskheroes:beam_charge").setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1 && !entity.getData("fiskheroes:beam_charging")));
    addAnimationWithData(renderer, "kratos.BLADESOFCHAOSUNHOLSTER", "gowhp:bladesofchaosunholster", "gowhp:dyn/bladesofchaos_timer");
    addAnimationWithData(renderer, "kratos.BLADESOFCHAOSDUALWIELD", "gowhp:dualwield", "gowhp:dyn/bladesofchaos").setCondition(entity => entity.isPunching() &&  !entity.getInterpolatedData("fiskheroes:shield_blocking_timer"));
    addAnimationWithData(renderer, "kratos.BLADESOFCHAOSBLOCKING", "gowhp:weaponfirstperson", "fiskheroes:shield_blocking_timer").setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos")));
}

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

    bladeofchaos_right = renderer.createEffect("fiskheroes:model");
    bladeofchaos_right.setModel(utils.createModel(renderer, "gowhp:bladesofchaos", "bladesofchaos", "bladesofchaos_lights"));
    bladeofchaos_right.anchor.set("rightArm");
    bladeofchaos_right.setScale(0.7);
    bladeofchaos_right.setRotation(90.0, -90.0, 0.0);
    bladeofchaos_right.setOffset(1.0, 9.0, -20.0);

    bladeofchaos_left = renderer.createEffect("fiskheroes:model");
    bladeofchaos_left.setModel(utils.createModel(renderer, "gowhp:bladesofchaos", "bladesofchaos", "bladesofchaos_lights"));
    bladeofchaos_left.anchor.set("leftArm");
    bladeofchaos_left.setScale(0.7);
    bladeofchaos_left.setRotation(90.0, -90.0, 0.0);
    bladeofchaos_left.setOffset(-1.0, 9.0, -20.0);

    bladeofchaos_holsterright = renderer.createEffect("fiskheroes:model");
    bladeofchaos_holsterright.setModel(utils.createModel(renderer, "gowhp:bladesofchaos", "bladesofchaos", "bladesofchaos_lights"));
    bladeofchaos_holsterright.anchor.set("body");
    bladeofchaos_holsterright.setScale(0.7);
    bladeofchaos_holsterright.setRotation(0.0, 180.0, -45.0);
    bladeofchaos_holsterright.setOffset(-11.0, -5.0, 3.0);

    bladeofchaos_holsterleft = renderer.createEffect("fiskheroes:model");
    bladeofchaos_holsterleft.setModel(utils.createModel(renderer, "gowhp:bladesofchaos", "bladesofchaos", "bladesofchaos_lights"));
    bladeofchaos_holsterleft.anchor.set("body");
    bladeofchaos_holsterleft.setScale(0.7);
    bladeofchaos_holsterleft.setRotation(0.0, 180.0, 135.0);
    bladeofchaos_holsterleft.setOffset(10.0, 15.0, 4.0);

    var fire = renderer.createResource("ICON", "fiskheroes:fire_layer_%s");
    blades_flame = bladeflames.createHands(renderer, fire, true);
    bladescharge_flame = bladechargeflames.createHands(renderer, fire, true);
    blades_flame_display = bladeflamesdisplay.createHands(renderer, fire, true);

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "gowhp:invisible", "rightArm", 0xe25822, [
        { "firstPerson": [-4.75, 3.0, -13.0], "offset": [-0.6, 19.0, -1.5], "size": [2.0, 2.0] }
    ]).setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0));

    utils.bindParticles(renderer, "gowhp:cycloneofchaos").setCondition(entity => entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0 && entity.getData("fiskheroes:beam_charging") && entity.getInterpolatedData("fiskheroes:beam_shooting_timer") > 0);

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "gowhp:invisible", "rightArm", 0xe25822, [
        { "firstPerson": [-4.75, 3.0, -13.0], "offset": [-0.6, 19.0, -1.5], "size": [2.0, 2.0] }
    ]).setCondition(entity => (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1)).setParticles(renderer.createResource("PARTICLE_EMITTER", "gowhp:fireofares"));;

    utils.bindParticles(renderer, "gowhp:fireofaresground").setCondition(entity => entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1 && entity.getInterpolatedData("fiskheroes:beam_shooting_timer") > 0);

    var bladesofchaosrunicchargeshake = renderer.bindProperty("fiskheroes:camera_shake").setCondition(entity => ((entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0 || entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1) && !entity.getData("gowhp:dyn/leviathanaxe_runic_cycle") == 0 && entity.getData("fiskheroes:beam_charging") && !entity.getData("fiskheroes:beam_shooting")));
    bladesofchaosrunicchargeshake.factor = 0.05;

    var bladesofchaosrunicshake = renderer.bindProperty("fiskheroes:camera_shake").setCondition(entity => ((entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0 || entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1) && !entity.getData("gowhp:dyn/leviathanaxe_runic_cycle") == 0 && entity.getData("fiskheroes:beam_charging") && entity.getInterpolatedData("fiskheroes:beam_shooting_timer") > 0));
    bladesofchaosrunicshake.factor = 0.1;

    var bladesofchaoscharge_color = 0xe25822;
    var bladesofchaoscharge_effect = renderer.createResource("BEAM_RENDERER", "gowhp:bladesofchaoscharge");

    bladesofchaoscharge = utils.createLines(renderer, bladesofchaoscharge_effect, bladesofchaoscharge_color, [
        {"start": [-1.0, 7.0, -5.15], "end": [-1.0, 12.0, -10.15], "size": [30.0, 20.0]},
        {"start": [-1.0, 12.0, -10.15], "end": [-1.0, 7.5, -15.15], "size": [30.0, 20.0]},
    ]);
    bladesofchaoscharge.anchor.set("rightArm");
    bladesofchaoscharge.setScale(1.0);
    bladesofchaoscharge.mirror = true;
}

function render(entity, renderLayer, isFirstPersonArm) {
    parent.render(entity, renderLayer, isFirstPersonArm);
    if ((entity.getInterpolatedData("gowhp:dyn/bladesofchaos_timer") > 0.5) && (entity.getHeldItem().isEmpty()) || ((entity.as("DISPLAY").getDisplayType() == "HOLOGRAM" || entity.as("DISPLAY").getDisplayType() == "DATABASE_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "FABRICATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "ITERATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "BOOK_PREVIEW") && entity.getWornChestplate().suitType().startsWith("gowhp:kratos_upgraded"))) {
        bladeofchaos_left.render();
        bladeofchaos_right.render();
    }
    if ((renderLayer == "CHESTPLATE" && entity.getInterpolatedData("gowhp:dyn/bladesofchaos_timer") < 0.5) && !((entity.as("DISPLAY").getDisplayType() == "HOLOGRAM" || entity.as("DISPLAY").getDisplayType() == "DATABASE_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "FABRICATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "ITERATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "BOOK_PREVIEW") && entity.getWornChestplate().suitType().startsWith("gowhp:kratos_upgraded"))) {
        bladeofchaos_holsterright.render();
    }
    if ((renderLayer == "CHESTPLATE" && entity.getInterpolatedData("gowhp:dyn/bladesofchaos_timer") < 0.5) && !(((entity.as("DISPLAY").getDisplayType() == "HOLOGRAM" || entity.as("DISPLAY").getDisplayType() == "DATABASE_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "FABRICATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "ITERATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "BOOK_PREVIEW") && (entity.getWornChestplate().suitType().startsWith("gowhp:kratos_upgraded") || entity.getWornChestplate().suitType().startsWith("gowhp:kratos_allfather"))))) {
        bladeofchaos_holsterleft.render();
    }
    if ((entity.getInterpolatedData("gowhp:dyn/bladesofchaos_timer") > 0.5)) {
        blades_flame.render(entity);
    }
    if (((entity.as("DISPLAY").getDisplayType() == "HOLOGRAM" || entity.as("DISPLAY").getDisplayType() == "DATABASE_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "FABRICATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "ITERATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "BOOK_PREVIEW") && entity.getWornChestplate().suitType().startsWith("gowhp:kratos_upgraded"))) {
        blades_flame_display.render(entity);
    }
    if (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("fiskheroes:beam_charge") && (entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0 || entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1)){
        bladescharge_flame.render(entity);
    }
    if (entity.getData("gowhp:dyn/bladesofchaos") && entity.getData("fiskheroes:beam_charge") && (entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 0 || entity.getData("gowhp:dyn/bladesofchaos_runic_cycle") == 1)){
        bladesofchaoscharge.progress = Math.max(1 - (1 - entity.getInterpolatedData('fiskheroes:beam_charge')) * 1, 0);
        bladesofchaoscharge.opacity = Math.max(1 - (1 - entity.getInterpolatedData('fiskheroes:beam_charge')) * 1, 0);
        bladesofchaoscharge.render();
    }
    if (((entity.as("DISPLAY").getDisplayType() == "HOLOGRAM" || entity.as("DISPLAY").getDisplayType() == "DATABASE_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "FABRICATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "ITERATOR_PREVIEW" || entity.as("DISPLAY").getDisplayType() == "BOOK_PREVIEW") && entity.getWornChestplate().suitType().startsWith("gowhp:kratos_upgraded"))){
        bladesofchaoscharge.render();
    }
}
