#include <assert.h>
#import <UIKit/UIKit.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>

@interface GLView : UIView {
@private
	BOOL active;
	BOOL use_disp_link;
	NSInteger frame_interval;
	id disp_link;
	NSTimer *anim_timer;
	EAGLContext *ctx;
	unsigned int fbo, rbuf_color, rbuf_depth;
}

-(void)start_anim;
-(void)stop_anim;
-(void)draw: (id)sender;
@end

@interface SGLDelegate : NSObject <UIApplicationDelegate> {
	UIWindow *win;
	GLView *view;
}
@end



@implementation GLView

@synthesize animating;
@dynamic frame_interval;

+(Class)layerClass
{
	return [CAEAGLLayer class];
}

//The EAGL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
-(id)initWithCoder: (NSCoder*)coder
{
	if((self = [super initWithCoder: coder])) {
		// Get the layer
		CAEAGLLayer *layer = (CAEAGLLayer*)self.layer;

		//self.contentScaleFactor = 2.0;

		layer.opaque = TRUE;
		layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
			[NSNumber numberWithBool: FALSE],
			kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8,
			kEAGLDrawablePropertyColorFormat,
			nil];

		ctx = [[EAGLContext alloc] initWithAPI: kEAGLRenderingAPIOpenGLES2];
		if(!ctx || ![EAGLContext setCurrentContext: ctx]) {
			[self release];
			return nil;
		}

		// initialize fbos etc...
		glGenFramebuffers(1, &fbo);
		glBindFramebuffer(GL_FRAMEBUFFER, fbo);

		glGenRenderbuffers(1, &rbuf_color);
		glBindRenderbuffer(GL_RENDERBUFFER, rbuf_color);
		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbuf_color);

		glGenRenderbuffers(1, &rbuf_depth);
		glBindRenderbuffer(GL_RENDERBUFFER, rbuf_depth);
		glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
		assert(glGetError() == GL_NO_ERROR);

		animating = FALSE;
		use_disp_link = FALSE;
		frame_interval = 1;
		disp_link = nil;
		anim_timer = nil;

		// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
		// class is used as fallback when it isn't available.
		NSString *req_ver = @"3.1";
		NSString *cur_ver = [[UIDevice currentDevice] systemVersion];
		if([curr_ver compare: req_ver options: NSNumericSearch] != NSOrderedAscending) {
			use_disp_link = TRUE;
		}

		//self.multipleTouchEnabled = 1;

		// TODO call user init ?
	}

	return self;
}

-(void)drawView: (id)sender
{
	// TODO call display
}

-(void)layoutSubviews
{
	// TODO call reshape
	// XXX originally call to renderer resizeFromLayer
	// XXX originally call to [self drawView: nil]
}

-(void)setAnimationFrameInterval: (NSInteger)interval
{
	/* Frame interval defines how many display frames must pass
	 * between each time the display link fires. The display
	 * link will only fire 30 times a second when the frame
	 * internal is two on a display that refreshes 60 times a
	 * second. The default frame interval setting of one will
	 * fire 60 times a second when the display refreshes at 60
	 * times a second. A frame interval setting of less than
	 * one results in undefined behavior.
	 */
	if(interval >= 1) {
		frame_interval = frame_interval;

		if(active)
		{
			[self stopAnimation];
			[self startAnimation];
		}
	}
}

-(void)startAnimation
{
	if(!active) {
		if(use_disp_link) {
			// CADisplayLink is API new to iPhone SDK 3.1. Compiling against earlier versions will result in a warning, but can be dismissed
			// if the system version runtime check for CADisplayLink exists in -initWithCoder:. The runtime check ensures this code will
			// not be called in system versions earlier than 3.1.

			disp_link = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self
				selector: @selector(drawView:)];
			[displayLink setFrameInterval: frame_interval];
			[displayLink addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
		} else {
			anim_timer = [NSTimer scheduledTimerWithTimeInterval: (NSTimeInterval)((1.0 / 60.0) * frame_interval)
				target: self selector: @selector(drawView:) userInfo: nil
				repeats: TRUE];
		}

		active = TRUE;
	}
}

- (void)stopAnimation
{
	if(active) {
		if(use_disp_link) {
			[disp_link invalidate];
			disp_link = nil;
		} else {
			[anim_timer invalidate];
			anim_timer = nil;
		}

		active = FALSE;
	}
}

-(void)dealloc
{
	// XXX originally [renderer release];
	[super dealloc];
}
@end

@implementation SGLDelegate

@synthesize win;
@synthesize view;

-(BOOL)application: (UIApplication*)app didFinishLaunchingWithOptions: (NSDictionary*)opt
{
	[view startAnimation];
	return YES;
}

-(void)applicationWillResignActive: (UIApplication*)app
{
	[view stopAnimation];
}

-(void)applicationDidBecomeActive: (UIApplication*)app
{
	[view startAnimation];
}

-(void)applicationWillTerminate: (UIApplication*)app
{
	[view stopAnimation];
}

-(void)dealloc
{
	[win release];
	[view release];
	[super dealloc];
}
@end
