extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "fiskheroes:atom_layer1",
    "layer2": "fiskheroes:atom_layer2",
    "wings": "diminpack:wing",
    "lights_layer1": "fiskheroes:atom_lights_layer1",
    "lights_layer2": "fiskheroes:atom_lights_layer2",
    "visor": "fiskheroes:atom_visor.tx.json",
    "segment": "fiskheroes:doctor_octopus_arm",
    "claw": "fiskheroes:doctor_octopus_claw",
    "claw_lights": "fiskheroes:doctor_octopus_claw_lights"

});

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

var helmet;
var chest;
var wings;
var wingsrender;
var boosters = implement("fiskheroes:external/bloom_booster");
var body_lines = implement("fiskheroes:external/body_lines");

var visor;
var booster_boots;
var booster_back;

var shrink_lights;
function init(renderer) {
    parent.init(renderer);
    renderer.setLights((entity, renderLayer) => renderLayer == "LEGGINGS" ? "lights_layer2" : renderLayer != "HELMET" ? "lights_layer1" : null);
}


function initEffects(renderer) {
    var ock_arm = utils.createModel(renderer, "fiskheroes:ock_arm", "segment");
    var ock_claw = utils.createModel(renderer, "fiskheroes:ock_claw", "claw", "claw_lights");
    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());
    });

    var tentacles = renderer.bindProperty("fiskheroes:tentacles").setTentacles([
        { "offset": [2.0, -4.5, -2.0], "direction": [13.0, 10.0, -10.0] },
        { "offset": [-2.0, -4.5, -2.0], "direction": [-13.0, 10.0, -10.0] },
        { "offset": [2.0, -7.5, -2.0], "direction": [13.0, -10.0, -10.0] },
        { "offset": [-2.0, -7.5, -2.0], "direction": [-13.0, -10.0, -10.0] }
    ]);
    tentacles.anchor.set("body");
    tentacles.setSegmentModel(ock_arm);
    tentacles.setHeadModel(ock_claw);
    tentacles.segmentLength = 1.8;
    tentacles.segments = 16;
    visor = renderer.createEffect("fiskheroes:overlay");
    visor.texture.set("visor");
    visor.opacity = 0.85;

    shrink_lights = body_lines.create(renderer, "fiskheroes:atom_shrink", 0x005EFF, [
        { anchor: "body", renderLayer: "CHESTPLATE", mirror: true, entries: [
            { "start": [3.5, 0.0, -1.5], "end": [7.5, -6.0, -3.5], "size": [0.5, 0.5] },
            { "start": [4.0, 5.5, -2.0], "end": [8.0, 4.5, -6.0], "size": [0.5, 0.5] },
            { "start": [3.5, 9.5, -1.5], "end": [7.5, 12.5, -4.5], "size": [0.5, 0.5] }
        ]},
        { anchor: "rightArm", renderLayer: "CHESTPLATE", mirror: true, entries: [
            { "start": [-3.5, 1.0, 0.5], "end": [-9.0, -1.0, 1.5], "size": [0.5, 0.5] },
            { "start": [-3.0, 4.5, 0.5], "end": [-8.0, 5.5, 1.5], "size": [0.5, 0.5] }
        ]},
        { anchor: "rightLeg", renderLayer: "LEGGINGS", mirror: true, entries: [
            { "start": [-2.0, 2.2, 2.0], "end": [-4.0, 5.2, 4.0], "size": [0.5, 0.5] }
        ]}
    ]);

    chest = renderer.createEffect("fiskheroes:chest");
    chest.setExtrude(1.2).setYOffset(1);


    var color = 0xFFFF00;
    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "diminpack:buzz_stinger", "rightArm", color, [{
        "firstPerson": [-4.5, 3.75, -7.0],
        "offset": [-4, 9.0, 0.0],
        "size": [1, 1]
    }
]).setCondition(entity => entity.getData("fiskheroes:scale") == 1.0);
    /*utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "diminpack:buzz_stinger", "rightArm", color, [{
                "firstPerson": [-4.5, 3.75, -7.0],
                "offset": [-4, 9.0, 0.0],
                "size": [1, 1]
            }
        ]).setCondition(entity => ((entity.getData("fiskheroes:scale") == 1.0) && (entity.getData("diminpack:sinister") == true)));
    
    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "diminpack:buzz_stinger", "leftArm", color, [
        { "firstPerson": [-4.5, 3.75, -7.0], "offset": [-4, 9.0, 0.0], "size": [1, 1] }
    ]).setCondition(entity => ((entity.getData("fiskheroes:scale") == 1.0) && (entity.getData("diminpack:sinister") == false)));*/
    var beam = renderer.createResource("BEAM_RENDERER", "fiskheroes:atom_booster");
    var blue_fire = renderer.createResource("ICON", "fiskheroes:deep_blue_fire_layer_%s");
    var color = 0x0033FF;
    booster_boots = boosters.create(renderer, "rightLeg", true, color, blue_fire, beam).setOffset(0.0, 7.0, 2.0).setSize(1.5, 3.0);
    booster_back = boosters.create(renderer, "body", true, color, blue_fire, beam).setOffset(1.5, 6.0, 1.75).setRotation(15.0, 0.0, 5.0);

    utils.addCameraShake(renderer, 0.015, 1.5, "fiskheroes:flight_boost_timer");
    var wings = renderer.createResource("MODEL", "diminpack:Wings");
    wings.bindAnimation("diminpack:wings_flapping").setData((entity, data) => {
        //i put this the same as the hover animation just in case you want it like this
        data.load(0, (entity.getData('fiskheroes:flight_timer') > 0.5? entity.getInterpolatedData("diminpack:dyn/wasp_flap") * 2 : 0 || entity.getData('fiskheroes:flight_timer') <= 0.5 ? 0.5 : 0));
        data.load(1, 1 - (Math.min(entity.getInterpolatedData('fiskheroes:flight_timer') * 2, 1)));
    });
    wings.texture.set("wings");

    wingsrender = renderer.createEffect("fiskheroes:model").setModel(wings);
    wingsrender.anchor.set("body");

}


function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addHoverAnimation(renderer, "atom.HOVER", "fiskheroes:flight/idle/default_back");
    utils.addFlightAnimation(renderer, "atom.FLIGHT", "fiskheroes:flight/propelled.anim.json", (entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_boost_timer"));
        data.load(2, entity.getInterpolatedData("fiskheroes:scale"));
    });

    renderer.reprioritizeDefaultAnimation("PUNCH", -9);
    renderer.reprioritizeDefaultAnimation("AIM_BOW", -9);
    addAnimationWithData(renderer, "barrel.ROLL", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer")
    .priority = 10;
    utils.addAnimationEvent(renderer, "FLIGHT_DIVE", "fiskheroes:iron_man_dive");
    utils.addAnimationEvent(renderer, "FLIGHT_DIVE_ROLL", "fiskheroes:falcon_dive_roll");
    addAnimationWithData(renderer, "basic.AIMING", "fiskheroes:dual_aiming", "fiskheroes:aiming_timer")
        .priority = 10;
    addAnimationWithData(renderer, "spiderman.AIMING", "fiskheroes:web_aim_right", "fiskheroes:web_aim_right_timer")
        .priority = 2;

    addAnimationWithData(renderer, "spiderman.AIMING_LEFT", "fiskheroes:web_aim_left", "fiskheroes:web_aim_left_timer")
        .priority = 2;

    addAnimationWithData(renderer, "spiderman.WEB_RAPPEL", "fiskheroes:web_rappel", "fiskheroes:web_rappel_timer")
        .priority = 5;

    utils.addAnimationEvent(renderer, "WEBSWING_DEFAULT", "fiskheroes:swing_default");
    utils.addAnimationEvent(renderer, "WEBSWING_RIGHT", "fiskheroes:swing_right");
    utils.addAnimationEvent(renderer, "WEBSWING_LEFT", "fiskheroes:swing_left");
    utils.addAnimationEvent(renderer, "WEBSWING_TRICK_DEFAULT", [
        "fiskheroes:swing_roll",
        "fiskheroes:swing_roll2",
        "fiskheroes:swing_roll5"
    ]);
    utils.addAnimationEvent(renderer, "WEBSWING_TRICK_RIGHT", "fiskheroes:swing_rotate_right");
    utils.addAnimationEvent(renderer, "WEBSWING_TRICK_LEFT", "fiskheroes:swing_rotate_left");
    utils.addAnimationEvent(renderer, "WEBSWING_ZIP", "fiskheroes:swing_zip");
    utils.addAnimationEvent(renderer, "WEBSWING_DIVE", [
        "fiskheroes:swing_dive",
        "fiskheroes:swing_dive2"
    ]);
    utils.addAnimationEvent(renderer, "WEBSWING_LEAP", "fiskheroes:swing_springboard");
    utils.addAnimationEvent(renderer, "WEBSWING_SHOOT_RIGHT", "fiskheroes:web_swing_shoot_right");
    utils.addAnimationEvent(renderer, "WEBSWING_SHOOT_LEFT", "fiskheroes:web_swing_shoot_left");
    utils.addAnimationEvent(renderer, "WEBSHOOTER_SHOOT_RIGHT", "fiskheroes:web_shoot_right");
    utils.addAnimationEvent(renderer, "WEBSHOOTER_SHOOT_LEFT", "fiskheroes:web_shoot_left");
    utils.addAnimationEvent(renderer, "CEILING_CRAWL", "fiskheroes:crawl_ceiling");
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (!isFirstPersonArm) {
        if (renderLayer == "CHESTPLATE" || renderLayer == "LEGGINGS") {
            shrink_lights.opacity = shrink_lights.progress = entity.getInterpolatedData("fiskheroes:dyn/shrink_timer");
            shrink_lights.progress /= Math.sqrt(entity.getInterpolatedData("fiskheroes:scale")) * 2;
            shrink_lights.render(renderLayer);
            
        }
        if (renderLayer == "HELMET") {
            visor.render();
        }
        else if (renderLayer == "CHESTPLATE") {
            wingsrender.setScale(0.2 + 0.8 *(entity.getData("fiskheroes:flight_timer")), 0.3 + 0.7 *(entity.getData("fiskheroes:flight_timer")), 0.1 + 0.9 * (entity.getData("fiskheroes:flight_timer")));
            wingsrender.render();
            var boost = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
            booster_back.speedScale = 0.5 * boost;
            booster_back.flutter = 1 + boost;

            var f = Math.min(Math.max(boost * 3 - 1.25, 0), 1);
            f = entity.isSprinting() ? 0.5 - Math.cos(2 * f * Math.PI) / 2 : 0;
            booster_back.setSize(1.5 + f * 2, 3.5 - f * 2);
            booster_back.render(entity, entity.getInterpolatedData("fiskheroes:dyn/booster_timer"));
        }
        else if (renderLayer == "BOOTS") {
            var boost = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
            booster_boots.speedScale = 0.5 * boost;
            booster_boots.flutter = 1 + boost;

            booster_boots.setRotation(15 - 10 * boost, 0, 0);
            booster_boots.render(entity, entity.getInterpolatedData("fiskheroes:dyn/booster_timer"));
        }
    }
    
}
