var LOADTEXTURES = {
	"null": "tmf:null",
	"heatblast": "tmf:omnitrix/p1/aliens/heatblast",
	"heatblast_lights": "tmf:omnitrix/p1/lights/heatblast_lights",
	"flight_rock": "tmf:omnitrix/p1/lights/heatblast_flight_rock",
	"firebomb": "tmf:omnitrix/p1/lights/heatblast_firebomb"
};

var IMPLEMENTS = {
    utils: "fiskheroes:external/utils"
};

function init(renderer, getAlien, isCurrent) {
    badge = renderer.createEffect("fiskheroes:model");
        badge.setModel(utils.createModel(renderer, "tmf:omnitrix/device/badge", "badge", "badge_lights"));
        badge.anchor.set("body");

    heatblastOverlay = renderer.createEffect("fiskheroes:overlay");
    heatblastOverlay.texture.set("null", "heatblast_lights");

    utils.bindParticles(renderer, "tmf:alien_particles_p1").setCondition(entity => isCurrent(entity));
   
    var fire = renderer.createResource("ICON", "tmf:heatblast_fire_layer_%s");
    var head_flames = createHead(renderer, fire);
    var booster_flames = createBoosters(renderer, fire);



        utils.bindBeam(renderer, "fiskheroes:heat_vision", "tmf:fire_beam", "rightArm", 0xF3985B, [
        ]).setCondition(entity => entity.getData('tmf:dyn/pc_1') != 0 && isCurrent(entity));

        utils.bindBeam(renderer, "fiskheroes:heat_vision", "tmf:fire_beam", "rightArm", 0xF3985B, [
            { "firstPerson": [-5.5, 4.75, -10.0], "offset": [-0.5, 9.0, 0.0], "size": [3.0, 3.0, -3.0] }
        ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_energy_projection")).setCondition(entity => entity.getData('tmf:dyn/pc_1') == 0 && isCurrent(entity));



    var modelFireBomb = renderer.createResource("MODEL", "tmf:omnitrix/powers/firebomb");
        modelFireBomb.bindAnimation("tmf:omnitrix/powers/firebomb").setData((entity, data) => data.load(entity.loop(75)));
        modelFireBomb.texture.set("null", "firebomb");
        firebomb = renderer.createEffect("fiskheroes:model").setModel(modelFireBomb);
        firebomb.anchor.ignoreAnchor(true);	

    var modelFlightRock = renderer.createResource("MODEL", "tmf:omnitrix/powers/flight_rock");
        modelFlightRock.bindAnimation("tmf:omnitrix/powers/flight_rock").setData((entity, data) => {
            data.load(0, entity.loop(75));
            data.load(1, entity.getInterpolatedData("tmf:dyn/pt_1"));
            });
        modelFlightRock.texture.set("null", "flight_rock");
        flightRock = renderer.createEffect("fiskheroes:model").setModel(modelFlightRock);
        flightRock.anchor.set("body");	

    var shakeHeatblast = renderer.bindProperty("fiskheroes:camera_shake").setCondition(entity => {
        shakeHeatblast.factor = 0.2 * entity.getInterpolatedData("fiskheroes:flight_boost_timer");
        return isCurrent(entity);
        });
    var explosionShake = renderer.bindProperty("fiskheroes:camera_shake").setCondition(entity => {
        explosionShake.factor = 5 * tanh(entity.getInterpolatedData("tmf:dyn/pc_1"), 90, -80);
        return isCurrent(entity);
        });

    utils.addFlightAnimation(renderer, "heatblast.FLIGHT", "tmf:omnitrix/powers/flight/heatblast_rock").setCondition(entity => isCurrent(entity) && entity.getData('tmf:dyn/p_2'));
    utils.addFlightAnimation(renderer, "heatblast.FLIGHT2", "fiskheroes:flight/propelled_hands.anim.json").setCondition(entity => isCurrent(entity) && !entity.getData('tmf:dyn/p_2'));
    utils.addHoverAnimation(renderer, "heatblast.HOVER", "fiskheroes:flight/idle/propelled_hands").setCondition(entity => isCurrent(entity));
    addAnimationWithData(renderer, "heatblast.ROLL", "tmf:omnitrix/powers/flight/heatblast_rock_roll", "fiskheroes:barrel_roll_timer").setCondition(entity => isCurrent(entity) && entity.getData('tmf:dyn/p_2'));
    addAnimationWithData(renderer, "heatblast.ROLL2", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer").setCondition(entity => isCurrent(entity) && !entity.getData('tmf:dyn/p_2'));
	addAnimation(renderer, "heatblast.FIREBOMB", "tmf:omnitrix/powers/firebomb_player").setData((entity, data) => {
		data.load(0, entity.isSneaking() ? 1 : 0);
		data.load(1, entity.getInterpolatedData("tmf:dyn/pc_1"));
		}).setCondition(entity => isCurrent(entity));

    return {
        getTexture: entity => "heatblast",
        getLights: entity => ("null"),
        render: (entity, isFirstPersonArm) => {
            if (!isFirstPersonArm) {
                badge.render();
                head_flames.render(1-entity.getInterpolatedData('tmf:dyn/pt_3'));
                if (entity.getData('fiskheroes:flying') && entity.getData('tmf:dyn/p_2')) {
                    flightRock.setScale(Math.max(entity.getInterpolatedData('fiskheroes:flight_timer'), 0.5));
                    flightRock.render();
                    booster_flames.render(entity.getInterpolatedData("tmf:dyn/pt_1"));
                }
            }
            heatblastOverlay.opacity = 1-entity.getInterpolatedData('tmf:dyn/pt_3');
            heatblastOverlay.render();
            fb = entity.getInterpolatedData("tmf:dyn/pc_1");
            if (fb >= 0.5 && fb <= 0.95) {
                firebomb.setScale(0.85* tanh(fb, 15, -11) + 0.15*Math.sin(Math.PI*entity.loop(100)) + 5*tanh(fb, 90, -80));
                firebomb.opacity = 0.8* tanh(fb, 30, -22) + 0.2*entity.loop(40)*(1-tanh(fb, 90, -80)) - 0.8*tanh(fb, 90, -80);
                firebomb.setOffset(0, -2 + 120*tanh(fb, 90, -80), 0);
                firebomb.render();
            }
        }
    };
}

function tanh(data, x, y) {
	return 0.5 * ((Math.pow(Math.E, x*data + y) - Math.pow(Math.E, -(x*data + y))) / (Math.pow(Math.E, x*data + y) + Math.pow(Math.E, -(x*data + y)))) + 0.5;
}

function createHead(renderer, icon) {
    if (typeof icon === "string") {
        icon = renderer.createResource("ICON", icon);
    }

    var base = renderer.createEffect("fiskheroes:booster").setIcon(icon);
    base.setOffset(0.0, 1.0, 0.0).setRotation(-5.0, 0.0, 180.0);
    base.anchor.set("head");

    var top = renderer.createEffect("fiskheroes:booster").setIcon(icon);
    top.setOffset(0.0, -5.0, 0.0).setRotation(-1.0, 0.0, 180.0).setSize(7.5, 1.25);
    top.anchor.set("head");

    base.flutter = top.flutter = 0.25;
    base.speedScale = top.speedScale = 0;
    return {
        base: base,
        top: top,
        render: timer => {
            base.progress = top.progress = timer;
            base.setSize(8 + 2 * timer, 1.75);
            base.opacity = 0.2*timer;
            top.opacity = 0.8*timer;
            base.render();
            top.render();
        }
    };
}

function createBoosters(renderer, icon) {
    if (typeof icon === "string") {
        icon = renderer.createResource("ICON", icon);
    }

    var base = renderer.createEffect("fiskheroes:booster").setIcon(icon);
    base.anchor.set("body");
    var base2 = renderer.createEffect("fiskheroes:booster").setIcon(icon);
    base2.anchor.set("body");

    base.opacity = 0.9;
    base2.opacity = 0.45;
    base.flutter = base2.flutter = 0.25;
    base.speedScale = base2.speedScale = 1;
    return {
        base: base,
        base2: base2,
        render: timer => {
            base.progress = 1;
            base.setSize(11, 2.25);
            base.setOffset(timer*8, 27.0, -timer*8+8.0);
            base.setRotation(-93.0, 0, -timer*90+180.0);
            base.render();

            base2.progress = 1;
            base2.setSize(16, 2.2);
            base2.setOffset(-timer*8, 27.0,  -12 + timer*12);
            base2.setRotation(-93.0, 0, -timer*90+180.0);
            base2.render();
        }
    };
}
