extend("ahhp:hero_with_models");

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

loadTextures({
    "model_lights": "ahhp:war_machine_mark_1_lights",
    "minigunmodel": "ahhp:war_machine_minigun",
    "model": "ahhp:war_machine_mark_1"
});

function getColor() {
    return 0x6E0E08;
}

function getIcon() {
    return "fiskheroes:red_fire_layer_%s";
}

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

function initEffects(renderer) {
    utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:charged_beam", "head", getColor(), [
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));
    models_stuff.setOpacityWithMaskTimer(renderer);
    models = models_stuff.noHelmetModels(renderer, "war_machine");
    var head = renderer.createResource("MODEL", "ahhp:war_machine_head");
    head.texture.set("model", "model_lights");
    head.bindAnimation("ahhp:war_machine").setData((entity, data) => {
		data.load(entity.getInterpolatedData("fiskheroes:mask_open_timer2"));
	});
    head_model = renderer.createEffect("fiskheroes:model").setModel(head);
    head_model.anchor.set("head");
    var minigun = renderer.createResource("MODEL", "ahhp:war_machine_minigun");
    minigun.texture.set("minigunmodel");
    minigun.bindAnimation("ahhp:minigun").setData((entity, data) => {
		data.load(0, entity.getInterpolatedData("fiskheroes:beam_charge"));
        data.load(1, entity.getInterpolatedData("fiskheroes:beam_shooting"));
	});
    minigun_model = renderer.createEffect("fiskheroes:model").setModel(minigun);
    minigun_model.anchor.set("body");
    boosters = iron_man_boosters.create(renderer, "fiskheroes:repulsor_layer_%s", true);
    utils.addCameraShake(renderer, 0.015, 1.5, "fiskheroes:flight_boost_timer");
    utils.bindParticles(renderer, "ahhp:war_machine_mark_1").setCondition(entity => entity.getData("fiskheroes:flying"));
    utils.bindBeam(renderer, "fiskheroes:repulsor_blast", "fiskheroes:repulsor_blast", "rightArm", getColor(), [
        { "firstPerson": [-4.5, 3.75, -8.0], "offset": [-0.5, 9.0, 0.0], "size": [1.5, 1.5] }
    ]);
    icon = renderer.createResource("ICON", getIcon());
    minigun_booster = renderer.createEffect("fiskheroes:booster").setIcon(icon);
    minigun_booster.setOffset(6.1, -2.0, -11.2).setSize(2.0, 2.0).setRotation(-90.0, 0.0, 0.0);
    minigun_booster.anchor.set("body");
    
    metal_heat = renderer.createEffect("fiskheroes:metal_heat");
    metal_heat.includeEffects(head_model, models.chest_model, models.rightArm_model, models.leftArm_model, models.rightLeg_model, models.leftLeg_model, minigun_model, minigun_booster, boosters.handL, boosters.handR, boosters.back, boosters.boots);
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimationWithLanding(renderer, "iron_man.FLIGHT", "fiskheroes:flight/iron_man.anim.json");
    utils.addHoverAnimation(renderer, "iron_man.HOVER", "fiskheroes:flight/idle/iron_man");
    addAnimationWithData(renderer, "iron_man.LAND", "fiskheroes:superhero_landing", "fiskheroes:dyn/superhero_landing_timer")
        .priority = -8;

    addAnimationWithData(renderer, "iron_man.ROLL", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer")
        .priority = 10;
}

function render(entity, renderLayer, isFirstPersonArm) {
    models.render(entity, renderLayer);
    boosters.render(entity, renderLayer, isFirstPersonArm, true);
    head_model.render();
    minigun_model.render();
    minigun_booster.progress = minigun_booster.opacity = entity.getData("fiskheroes:beam_shooting");
    minigun_booster.render();
    metal_heat.opacity = entity.getInterpolatedData("fiskheroes:metal_heat");
    metal_heat.render();
}
