extend("fiskheroes:hero_basic");
loadTextures({
    "layer1": "skarlet:green_goblin/green_goblin_layer1",
    "eyes": "skarlet:green_goblin/green_goblin_eyes",
    "layer2": "skarlet:green_goblin/green_goblin_layer2",
    "glider": "skarlet:green_goblin/goblin_glider",
    "glider_lights": "skarlet:green_goblin/goblin_glider_lights",
    "glider_booster": "skarlet:green_goblin/goblin_glider_boosters.tx.json",
    "razor_bats": "skarlet:green_goblin/razor_bats",
    "razor_bats_lights": "skarlet:green_goblin/razor_bats_lights"
});

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

var glider;
var glider_booster;
var eyes;
var razor_bats;

function init(renderer) {
    parent.init(renderer);
}

function getGliderModel() {
    return "skarlet:goblin_glider";
}

function initEffects(renderer) {
    //glider
    var model_glider = renderer.createResource("MODEL", getGliderModel());
    model_glider.bindAnimation("skarlet:flight/glider_idle").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_timer") && Math.sin((entity.loop(100)) * Math.PI) / 2);
        data.load(2, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(3, entity.getInterpolatedData("fiskheroes:blade_timer"));
        data.load(4, entity.getInterpolatedData("fiskheroes:beam_charge"));
        data.load(5, entity.getPunchTimerInterpolated());
    });
    model_glider.texture.set("glider", "glider_lights");

    //glider boosters
    var model_booster = renderer.createResource("MODEL", getGliderModel());
    model_booster.bindAnimation("skarlet:flight/glider_idle").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_timer") && Math.sin((entity.loop(100)) * Math.PI) / 2);
        data.load(2, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(3, entity.getInterpolatedData("fiskheroes:blade_timer"));
        data.load(4, entity.getInterpolatedData("fiskheroes:beam_charge"));
        data.load(5, entity.getPunchTimerInterpolated());
    });
    model_booster.texture.set(null, "glider_booster");

    glider = renderer.createEffect("fiskheroes:model").setModel(model_glider);
    glider.anchor.set("body");
    glider.setOffset(0.0, 1.5, -12.1);
    glider.setScale(0.9);

    glider_booster = renderer.createEffect("fiskheroes:model").setModel(model_booster);
    glider_booster.anchor.set("body");
    glider_booster.setOffset(0.0, 1.5, -12.1);
    glider_booster.setScale(0.9);

    //foot pedals
    var model1 = renderer.createResource("MODEL", "skarlet:FootPedals");
    model1.texture.set("glider", "glider_lights");
    model1.generateMirror();

    FootPedalsEffect = renderer.createEffect("fiskheroes:model");
    FootPedalsEffect.setModel(model1);
    FootPedalsEffect.anchor.set("rightLeg");
    FootPedalsEffect.mirror = true;

    //razor_bats
	var model_razor_bats = utils.createModel(renderer, "skarlet:razor_bats", "razor_bats", "razor_bats_lights");
    model_razor_bats.bindAnimation("skarlet:razor_bats").setData((entity, data) => {
        data.load(entity.getInterpolatedData("skarlet:dyn/throw_timer"))
    })
    razor_bats = renderer.createEffect("fiskheroes:model");
    razor_bats.setModel(model_razor_bats);
    razor_bats.anchor.set("head");
    
    utils.bindBeam(renderer, "fiskheroes:heat_vision", "skarlet:clear", "head", 0xFFFFFF, [{
                "firstPerson": [0, 0, 0],
                "offset": [0, 0, 0],
                "size": [0, 0]
            }]);

    //mask eyes
    eyes = renderer.createEffect("fiskheroes:model");
    eyes.setModel(utils.createModel(renderer, "skarlet:head_model", "eyes"));
    eyes.anchor.set("head");

    //ears
    ears = renderer.createEffect("fiskheroes:ears");
    ears.anchor.set("head");
    ears.angle = 12.5;
    ears.inset = 0.012;

    //flight effects
    utils.bindParticles(renderer, "skarlet:goblin_glider").setCondition(entity => entity.getData("fiskheroes:flying"));
    utils.addCameraShake(renderer, 0.015, 1.5, "fiskheroes:flight_boost_timer");

    //machine gun
    utils.bindBeam(renderer, "fiskheroes:charged_beam", "skarlet:bullet", "body", 0xFF0000, [
        { "firstPerson": [-7.0, 20.0, -13.0], "offset": [-7.0, 19.0, -13.0], "size": [1.0, 1.0] },
        { "firstPerson": [-8.0, 21.0, -13.0], "offset": [-8.0, 20.0, -13.0], "size": [1.0, 1.0] }
    ]).setCondition(entity => entity.ticksExisted() % 4 < 2).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));

    utils.bindBeam(renderer, "fiskheroes:charged_beam", "skarlet:bullet", "body", 0xFF0000, [
        { "firstPerson": [10.0, 20.0, -13.0], "offset": [10.0, 19.0, -13.0], "size": [1.0, 1.0] },
        { "firstPerson": [11.0, 21.0, -13.0], "offset": [11.0, 20.0, -13.0], "size": [1.0, 1.0] }
    ]).setCondition(entity => entity.ticksExisted() % 4 >= 2).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.HEAT_VISION");
    addAnimation(renderer, "goblin.GLIDER", "skarlet:flight/glider_idle").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_timer") && Math.sin((entity.loop(100)) * Math.PI) / 2);
        data.load(2, entity.getInterpolatedData("fiskheroes:flight_timer"));
    });
    
    renderer.reprioritizeDefaultAnimation("PUNCH", 10);
}

function hasEyesMask() {
    return true;
}

function hasEars() {
    return true;
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (!isFirstPersonArm) {
        if (entity.getData('fiskheroes:flying') || entity.getInterpolatedData("fiskheroes:prev_flight_timer") > 50) {
            glider.opacity = entity.getInterpolatedData("fiskheroes:flight_timer");
            glider.render();
            glider.opacity_booster = entity.getInterpolatedData("fiskheroes:flight_timer");
            glider_booster.render();
            FootPedalsEffect.opacity = entity.getInterpolatedData("fiskheroes:flight_timer");
            if (entity.getInterpolatedData("fiskheroes:flight_timer") == 1) {
                FootPedalsEffect.render();
            }
        }
    }
    if (isFirstPersonArm && entity.getInterpolatedData("skarlet:dyn/throw_timer") > 0) {
        razor_bats.anchor.ignoreAnchor(true);
    } else {
        razor_bats.anchor.ignoreAnchor(false);
    }
    if (entity.getData("skarlet:dyn/throw")) {
        razor_bats.setOffset(0, 0, -entity.getInterpolatedData("skarlet:dyn/throw_timer") * (entity.getData("fiskheroes:heat_vision_length") * 16));
        razor_bats.render();
    }

    if (renderLayer == "HELMET") {
        if (hasEyesMask()) {
            var m = entity.getInterpolatedData("fiskheroes:mask_open_timer2");
            eyes.setOffset(0, -24 - 1.5 * m, 0.01 * m);
            eyes.render();
        }

        if (hasEars()) {
            ears.render();
        }
    }
}