extend("ahhp:hero_with_models");

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

var booster1;
var booster2;
var booster3;

loadTextures({
    "glider": "ahhp:green_goblin_glider",
    "glider_lights": "ahhp:green_goblin_glider_lights",
    "pumpkin_bomb": "ahhp:green_goblin_pumpkin_bomb",
    "pumpkin_bomb_lights": "ahhp:green_goblin_pumpkin_bomb_lights",
    "model_lights": "ahhp:green_goblin_model_lights",
    "model": "ahhp:green_goblin_model"
});

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

function getColor() {
    return 0x30A255;
}

function getPBColor() {
    return 0xB98C05;
}

function getBoosterFire() {
    return "ahhp:green_fire_layer_%s";
}

function initEffects(renderer) {
    utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:charged_beam", "head", getColor(), [
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));
    models = models_stuff.maskToggleModels(renderer, "green_goblin");
    models_stuff.setOpacityWithMask(renderer);

    var glider = renderer.createResource("MODEL", "ahhp:green_goblin_glider");
    glider.texture.set("glider", "glider_lights");
    glider.bindAnimation("ahhp:glider_minigun").setData((entity, data) => {
		data.load(entity.getInterpolatedData("fiskheroes:beam_shooting"));
	});
    models_glider = renderer.createEffect("fiskheroes:model").setModel(glider);
    models_glider.anchor.set("body");

    var glider_in = renderer.createResource("MODEL", "ahhp:green_goblin_glider");
    glider_in.texture.set("glider", "glider_lights");
    glider_in.bindAnimation("ahhp:glider_in").setData((entity, data) => {
		data.load(entity.getInterpolatedData("fiskheroes:flight_timer"));
	});
    models_glider_in = renderer.createEffect("fiskheroes:model").setModel(glider_in);
    models_glider_in.anchor.set("body");

    var glider_out = renderer.createResource("MODEL", "ahhp:green_goblin_glider");
    glider_out.texture.set("glider", "glider_lights");
    glider_out.bindAnimation("ahhp:glider_out").setData((entity, data) => {
		data.load(entity.getInterpolatedData("fiskheroes:flight_timer"));
	});
    models_glider_out = renderer.createEffect("fiskheroes:model").setModel(glider_out);
    models_glider_out.anchor.set("body");

    var pumpkinbomb = renderer.createResource("MODEL", "ahhp:green_goblin_pumpkin_bomb");
    pumpkinbomb.texture.set("pumpkin_bomb", "pumpkin_bomb_lights");
    models_pumpkinbomb = renderer.createEffect("fiskheroes:model").setModel(pumpkinbomb);
    models_pumpkinbomb.anchor.set("rightArm");

    var goblinblade = renderer.createResource("MODEL", "ahhp:green_goblin_blades");
    goblinblade.texture.set("glider");
    models_goblinblade = renderer.createEffect("fiskheroes:model").setModel(goblinblade);
    models_goblinblade.anchor.set("rightArm");

    glider_icon = renderer.createResource("ICON", getBoosterFire());
    booster1 = renderer.createEffect("fiskheroes:booster").setIcon(glider_icon);
    booster1.setOffset(-2.0, 27.0, 7.0).setSize(2.0, 2.0).setRotation(70.0, 0.0, 0.0);
    booster1.anchor.set("body");

    booster2 = renderer.createEffect("fiskheroes:booster").setIcon(glider_icon);
    booster2.setOffset(0.0, 27.0, 7.0).setSize(2.0, 2.0).setRotation(70.0, 0.0, 0.0);
    booster2.anchor.set("body");
    
    booster3 = renderer.createEffect("fiskheroes:booster").setIcon(glider_icon);
    booster3.setOffset(2.0, 27.0, 7.0).setSize(2.0, 2.0).setRotation(70.0, 0.0, 0.0);
    booster3.anchor.set("body");

    minigun_icon = renderer.createResource("ICON", "fiskheroes:orange_fire_layer_%s");
    minigun_left_booster = renderer.createEffect("fiskheroes:booster").setIcon(minigun_icon);
    minigun_left_booster.setOffset(8.6, 26.75, -14.0).setSize(1.25, 1.25).setRotation(-90.0, 0.0, 0.0);
    minigun_left_booster.anchor.set("body");

    minigun_right_booster = renderer.createEffect("fiskheroes:booster").setIcon(minigun_icon);
    minigun_right_booster.setOffset(-8.6, 26.75, -14.0).setSize(1.25, 1.25).setRotation(-90.0, 0.0, 0.0);
    minigun_right_booster.anchor.set("body");
    utils.bindBeam(renderer, "fiskheroes:lightning_cast", "fiskheroes:lightning_cast", "rightArm", getPBColor(), [
        { "firstPerson": [-8.0, 4.5, -10.0], "offset": [-0.5, 9.0, 0.0], "size": [0.75, 0.75] }
    ]);
}

function addAnimation(renderer, key, anim) {
    if (typeof anim === "string") {
        anim = renderer.createResource("ANIMATION", anim);
    }

    renderer.addCustomAnimation(key, anim);
    return anim;
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimation(renderer, "green_goblin.flight", "ahhp:green_goblin.anim.json");
    addAnimation(renderer, "green_goblin.PUMPKINBOMB", "ahhp:green_goblin_pumpkin_bomb")
        .setCondition(entity => entity.getData("ahhp:dyn/pumpkinbomb_timer") > 0 && entity.getData("ahhp:dyn/pumpkinbomb_timer") < 1);
}

function render(entity, renderLayer, isFirstPersonArm) {
    models.render(entity, renderLayer);
    if (entity.getInterpolatedData("fiskheroes:flight_timer") > 0 && entity.getData("fiskheroes:flying")) {
        models_glider_in.render();
    }
    if (entity.getInterpolatedData("fiskheroes:flight_timer") > 0 && !entity.getData("fiskheroes:flying")) {
        models_glider_out.render();
    }
    if (entity.getInterpolatedData("fiskheroes:flight_timer") == 1) {
        models_glider.render();
        booster1.progress = booster2.progress = booster3.progress = entity.getInterpolatedData("fiskheroes:flight_timer");
        booster1.render();
        booster2.render();
        booster3.render();
    }
    if (entity.getData("ahhp:dyn/pumpkinbomb")) {
        models_pumpkinbomb.render();
    }
    if (entity.getData("ahhp:dyn/goblinblade")) {
        models_goblinblade.render();
    }
    minigun_left_booster.progress = minigun_left_booster.opacity = entity.getData("fiskheroes:beam_shooting");
    minigun_right_booster.progress = minigun_right_booster.opacity = entity.getData("fiskheroes:beam_shooting");
    minigun_left_booster.render();
    minigun_right_booster.render();
}
