extend("fiskheroes:hero_basic");
loadTextures({
    "base": "skarlet:ultron/ultron_base",
    "lights": "skarlet:ultron/ultron_lights",
    "null": "skarlet:null"
});

var head;
var body;

var l_arm;
var r_arm;

var l_leg;
var r_leg;

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

var booster_legs;

var metal_heat;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        return "null";
    });

    renderer.showModel("HELMET", "head", "headwear", "body", "rightArm", "leftArm", "rightLeg", "leftLeg");
}

function initEffects(renderer) {
    var forcefield = renderer.bindProperty("fiskheroes:forcefield");
    forcefield.color.set(0xFF2227);
    forcefield.setShape(36, 18).setOffset(0.0, 6.0, 0.0).setScale(1.25);
    forcefield.setCondition(entity => {
        forcefield.opacity = entity.getInterpolatedData("fiskheroes:shield_blocking_timer") * 0.15;
        return true;
    });

    var model_l_arm = renderer.createResource("MODEL", "skarlet:ultron/ultron_left_arm");

    model_l_arm.texture.set("base", "lights");
    l_arm = renderer.createEffect("fiskheroes:model").setModel(model_l_arm);
    l_arm.anchor.set("leftArm");

    var model_r_arm = renderer.createResource("MODEL", "skarlet:ultron/ultron_right_arm");

    model_r_arm.texture.set("base", "lights");
    model_r_arm.bindAnimation("skarlet:ultron_aim").setData((entity, data) => {
        var f = entity.getInterpolatedData("fiskheroes:aimed_timer");
        data.load(f);
    });
    r_arm = renderer.createEffect("fiskheroes:model").setModel(model_r_arm);
    r_arm.anchor.set("rightArm");

    var model_l_leg = renderer.createResource("MODEL", "skarlet:ultron/ultron_left_leg");

    model_l_leg.texture.set("base", "lights");
    l_leg = renderer.createEffect("fiskheroes:model").setModel(model_l_leg);
    l_leg.anchor.set("leftLeg");

    var model_r_leg = renderer.createResource("MODEL", "skarlet:ultron/ultron_right_leg");

    model_r_leg.texture.set("base", "lights");
    r_leg = renderer.createEffect("fiskheroes:model").setModel(model_r_leg);
    r_leg.anchor.set("rightLeg");

    var model_head = renderer.createResource("MODEL", "skarlet:ultron/ultron_head");

    model_head.texture.set("base", "lights");
    head = renderer.createEffect("fiskheroes:model").setModel(model_head);
    head.anchor.set("head");

    var model_body = renderer.createResource("MODEL", "skarlet:ultron/ultron_body");

    model_body.texture.set("base", "lights");
    body = renderer.createEffect("fiskheroes:model").setModel(model_body);
    body.anchor.set("body");

    utils.setOpacity(renderer, 0.99, 1.0);

    var beam = renderer.createResource("BEAM_RENDERER", "fiskheroes:atom_booster");
    var red_fire = renderer.createResource("ICON", "fiskheroes:red_fire_layer_%s");
    var color = 0xFF2227;
    booster_legs = boosters.create(renderer, "rightLeg", true, color, red_fire, beam).setOffset(1.5, 2.0, 0.0).setSize(2.0, 3.0);

    metal_heat = renderer.createEffect("fiskheroes:metal_heat");
    metal_heat.includeEffects(head, body, r_arm, l_arm, r_leg, l_leg);

    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "fiskheroes:repulsor_blast", "rightArm", 0xFF2227, [
        { "firstPerson": [-4.0, 2.0, -7.0], "offset": [-4.0, 8.0, 0.0], "size": [1.5, 1.5] }
    ]);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.BLOCKING");
    utils.addHoverAnimation(renderer, "ultron.HOVER", "skarlet:flight/idle/ultron");
    utils.addFlightAnimation(renderer, "ultron.FLIGHT", "skarlet:flight/ultron.anim.json", (entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_boost_timer"));
    });
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (!isFirstPersonArm && renderLayer == "HELMET") {
        head.render();

        body.render();
		
        l_arm.render();

        l_leg.render();
        r_leg.render();
        
        var boost = entity.getInterpolatedData("fiskheroes:flight_boost_timer");

        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_legs.speedScale = 0.5 * boost;
        booster_legs.flutter = 1 + boost;

        booster_legs.setRotation(0, 0, 15 - 10 * boost);
        booster_legs.render(entity, entity.getInterpolatedData("fiskheroes:dyn/booster_timer"));
    }
    
    if (renderLayer == "HELMET") {
        r_arm.render();
    }

    metal_heat.opacity = entity.getInterpolatedData("fiskheroes:metal_heat");
    metal_heat.render();
}