var LOADTEXTURES = {
    "null": "tmf:null",
    "wildmutt": "tmf:omnitrix/p1/aliens/wildmutt",
    "wildmutt_lights": "tmf:omnitrix/p1/lights/wildmutt_lights.tx.json"

};

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

function init(renderer, getAlien, isCurrent) {

    utils.bindParticles(renderer, "tmf:alien_particles_p1").setCondition(entity => isCurrent(entity));

    wildmuttTrackerBeam = utils.createLines(renderer, "tmf:tracking", 0xbaf43c, [
        {"start": [0.0, 0.0, 0.0], "end": [0.0, -16.0, 0.0], "size": [10.0, 10.0]}
    ]);
    wildmuttTrackerBeam.anchor.ignoreAnchor(true);
    wildmuttTrackerBeam.setRotationOrder("ZYX"); 

    var modelWildmutt = renderer.createResource("MODEL", "tmf:omnitrix/aliens/wildmutt");
    modelWildmutt.bindAnimation("tmf:omnitrix/aliens/wildmutt").setData((entity, data) => {
        data.load(0, entity.getInterpolatedData('tmf:dyn/pt_1'));
        data.load(1, entity.loop(60));
        data.load(2, entity.getData('tmf:dyn/p_1') ? entity.getInterpolatedData('tmf:dyn/pc_1') : 0);
        data.load(3, entity.loop(40));
        data.load(4, entity.getPunchTimerInterpolated());
        });
        modelWildmutt.texture.set("wildmutt", "wildmutt_lights");
    wildmutt = renderer.createEffect("fiskheroes:model").setModel(modelWildmutt);
    wildmutt.anchor.ignoreAnchor(true);	

    var wildmuttNightVision = renderer.bindProperty("fiskheroes:night_vision").setCondition(entity => {
        wildmuttNightVision.factor = 0.5;
        wildmuttNightVision.firstPersonOnly = false;
        return isCurrent(entity);
    });


    addAnimation(renderer, "wildmutt.ARM", "tmf:omnitrix/powers/remove_arm").setData((entity, data) => {
        data.load(0, 1);
    }).setCondition(entity => isCurrent(entity));

    return {
        getTexture: entity => "null",
        render: (entity, isFirstPersonArm) => {
                var targetX = entity.getData('tmf:dyn/cX');
                var targetY = entity.getData('tmf:dyn/cY');
                var targetZ = entity.getData('tmf:dyn/cZ');
                var pos = entity.pos();
                targetY -= 1.5; // Compensate for the sombrero's rendered height off the ground
                var yaw = pos.xz().angleTo(targetX, targetZ);
                var pitch = Math.asin((pos.y() - targetY) / pos.distanceTo(targetX, targetY, targetZ)) / Math.PI * 180;
            
                if (!isFirstPersonArm) {
                    if (entity.getData('tmf:dyn/pc_1') > 0) {
                        wildmuttTrackerBeam.progress = 6*entity.getInterpolatedData('tmf:dyn/pc_1');
                        wildmuttTrackerBeam.opacity = entity.getInterpolatedData('tmf:dyn/pc_1')*(0.1 + 0.2*Math.sin(Math.PI * entity.loop(40)));
                        wildmuttTrackerBeam.setRotation(pitch + 90, yaw - 90 - entity.rotBodyYawInterpolated(), 0);
                        wildmuttTrackerBeam.render();
                    }
                    wildmutt.render();
                }
        }
    };
}
