extend("ahhp:hero_with_models");

var utils = implement("fiskheroes:external/utils");
var models_stuff = implement("ahhp:external/model_stuff");
var mk50_cannon = implement("fiskheroes:external/mk50_cannon");

var jetpack;
var booster_main;
var booster_sides;
var models;
var metal_heat;

loadTextures({
    "wings_1": "ahhp:blue_beetle_wings_1",
    "wings_2": "ahhp:blue_beetle_wings_2",
    "wings_lights_1": "ahhp:blue_beetle_wings_lights_1",
    "wings_lights_2": "ahhp:blue_beetle_wings_lights_2",
    "shield": "ahhp:blue_beetle_shield",
    "shield_lights": "ahhp:blue_beetle_shield_lights",
    "cannon1": "ahhp:blue_beetle_cannon_1",
    "cannon2": "ahhp:blue_beetle_cannon_2",
    "cannon1_lights": "fiskheroes:iron_man_mk50_cannon1_lights",
    "cannon2_lights": "fiskheroes:iron_man_mk50_cannon2_lights",
    "cannon_inner": "fiskheroes:iron_man_mk50_cannon_inner",
    "model_lights": "ahhp:blue_beetle_scarab_lights",
    "model": "ahhp:blue_beetle_scarab"
});

function getColor() {
    return 0x0951D8;
}

function getTrail() {
    return "ahhp:blue_beetle";
}

function getFire() {
    return "fiskheroes:blue_fire_layer_%s";
}

function init(renderer) {
    parent.init(renderer);
    renderer.fixHatLayer("CHESTPLATE");
    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm", "rightLeg", "leftLeg");  
    initEffects(renderer);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimationWithLanding(renderer, "bluebeetle.FLIGHT", "fiskheroes:flight/default.anim.json");
    utils.addHoverAnimation(renderer, "bluebeetle.HOVER", "fiskheroes:flight/idle/neutral");
    addAnimationWithData(renderer, "bluebeetle.LAND", "fiskheroes:superhero_landing", "fiskheroes:dyn/superhero_landing_timer")
        .priority = -8;
    addAnimationWithData(renderer, "bluebeetle.BLOCKING", "fiskheroes:blocking", "fiskheroes:shield_blocking_timer")
        .priority = -5;
}

function initEffects(renderer) {
    cannon = mk50_cannon.create(renderer, "rightArm", getColor());
    renderer.bindProperty("fiskheroes:energy_bolt").color.set(getColor());

    models = models_stuff.chestplateOnly(renderer, "blue_beetle");
    shield = renderer.createEffect("fiskheroes:shield");
    shield.texture.set("shield", "shield_lights");
    shield.anchor.set("rightArm");
    shield.setRotation(0.0, 0.0, -10.0).setCurve(15.0, 50.0);
    shield.large = true;

    beetle_blade = renderer.createEffect("fiskheroes:model");
    beetle_blade.setModel(utils.createModel(renderer, "ahhp:blue_beetle_blade", "model"));
    beetle_blade.anchor.set("rightArm");

    wings_1 = renderer.createEffect("fiskheroes:wings").setYOffset(-1);
    wings_1.texture.set("wings_1", "wings_lights_1");
    wings_1.anchor.set("body");
    wings_2 = renderer.createEffect("fiskheroes:wings").setYOffset(2);
    wings_2.texture.set("wings_2", "wings_lights_2");
    wings_2.anchor.set("body");

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:charged_beam", "body", getColor(), [
        { "offset": [5.6, -5.6, -1.5], "size": [1.0, 1.0] },
        { "offset": [-5.6, -5.6, -1.5], "size": [1.0, 1.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));

    var fire = renderer.createResource("ICON", getFire());
    booster_main = renderer.createEffect("fiskheroes:booster");
    booster_main.setIcon(fire).setOffset(2.4, 5.0, 2.6).setSize(1.75, 3.0).setRotation(0.0, -18.0, 0.0);
    booster_main.anchor.set("body");
    booster_main.mirror = true;
    booster_sides = renderer.createEffect("fiskheroes:booster");
    booster_sides.setIcon(fire).setOffset(0.75, 7.0, 2.6).setSize(1.5, 2.0);
    booster_sides.anchor.set("body");
    booster_sides.mirror = true;

    scarab_model = renderer.createEffect("fiskheroes:model");
    scarab_model.setModel(utils.createModel(renderer, "ahhp:blue_beetle_scarab", "model", "model_lights"));
    scarab_model.anchor.set("body");
    models_stuff.bindBoostingTrail(renderer, getTrail());

    models_stuff.setOpacityWithMaskAndTransformation(renderer, 'ahhp:dyn/scarab')
    utils.bindParticles(renderer, "fiskheroes:cap_falcon").setCondition(entity => entity.getData("fiskheroes:flight_timer") > 0);

    trail = "ahhp:blue_beetle_transform";
    models_stuff.bindBeetleTransformTrail(renderer, trail)

    metal_heat = renderer.createEffect("fiskheroes:metal_heat");
    metal_heat.includeEffects(models.head_model, models.chest_model, models.rightArm_model, models.leftArm_model, models.rightLeg_model, models.leftLeg_model, scarab_model, beetle_blade, shield, booster_main, booster_sides, wings_1, wings_2, cannon.c1, cannon.c2, cannon.c3);
}

function render(entity, renderLayer, isFirstPersonArm) {
    shield.unfold = entity.getInterpolatedData("fiskheroes:shield_timer");
    shield.setOffset(2.9 + 1.8 * Math.min(shield.unfold * 5, 1), 6.0, 0.0);
    shield.render();
    scarab_model.render();
    models.render(entity, renderLayer, "ahhp:dyn/scarab");
    cannon.render(entity.getInterpolatedData("fiskheroes:aimed_timer"));
    if (entity.getData("ahhp:dyn/scarab")) {
        metal_heat.opacity = entity.getInterpolatedData("fiskheroes:metal_heat");
        metal_heat.render();
    }
    if (entity.getData("ahhp:dyn/beetleblade") && (entity.getHeldItem().isEmpty())) {
        beetle_blade.render();
    }
    if (renderLayer == "CHESTPLATE") {
        wings_1.unfold = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
        wings_1.render();
                
        wings_2.unfold = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
        wings_2.render();

        booster_main.progress = booster_sides.progress = entity.getInterpolatedData("fiskheroes:flight_timer");
        booster_main.render();
        booster_sides.render();
    }
}
