/* mac-framework: -framework Cocoa */

#include "config.h"

#ifdef USE_WSYS_MODULE_COCOA

#import <Cocoa/Cocoa.h>
#include "sgl.h"

@interface OpenGLView : NSOpenGLView
{
}

//-(id) initWithFrame: (NSRect) frameRect;

-(void) drawRect: (NSRect) rect;
-(void) reshape;
/*-(void) keyDown: (NSEvent*) ev;
-(void) keyUp: (NSEvent*) ev;
-(void) mouseDown: (NSEvent*) ev;
-(void) mouseUp: (NSEvent*) ev;
-(void) rightMouseDown: (NSEvent*) ev;
-(void) rightMouseUp: (NSEvent*) ev;
-(void) otherMouseDown: (NSEvent*) ev;
-(void) otherMouseUp: (NSEvent*) ev;
-(void) mouseDragged: (NSEvent*) ev;
-(void) rightMouseDragged: (NSEvent*) ev;
-(void) otherMouseDragged: (NSEvent*) ev;*/

-(BOOL) acceptsFirstResponder;
@end

struct window {
	int wid;
	NSWindow *win;
	OpenGLView *view;
	struct window *next;
};


static int init(void);
static void shutdown(void);

/* video mode switching */
static int set_vidmode(int xsz, int ysz);
static int get_vidmode(int *xsz, int *ysz);

/* create/destroy windows */
static int create_window(int xsz, int ysz, unsigned int flags);
static void close_window(int wid);

/* window management */
static int set_active(int wid);
static struct window *find_window(int wid);
static int activate_window(struct window *win);
static int set_title(const char *str);
static void redisplay(void);
static void swap_buffers(void);

static int get_modifiers(void);

/* event handling and friends */
static void set_event(int idx, int enable);
static int process_events(void);
static int handle_event(NSEvent *ev);


static struct wsys_module ws = {
	"cocoa", 0,
	init,
	shutdown,
	set_vidmode,
	get_vidmode,
	create_window,
	close_window,
	set_active,
	set_title,
	redisplay,
	swap_buffers,
	get_modifiers,
	set_event,
	process_events,
	0
};

static NSApplication *app;
static struct window *winlist, *active_win;


void sgl_register_cocoa(void)
{
	sgl_register_cocoa(&ws);
}


@implementation OpenGLView

-(void) drawRect: (NSRect) rect
{
	sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY);
	if(func) {
		func();
	}
}

-(void) reshape
{
	sgl_reshape_callback_t func = sgl_get_callback(SGL_RESHAPE);
	if(func) {
		func(x, y);
	}
}

-(BOOL) acceptsFirstResponder
{
	return YES;
}
@end

static int init(void)
{
	app = [NSApplication sharedApplication];
}

static void shutdown(void)
{
	while(winlist) {
		struct window *win = winlist;
		winlist = winlist->next;

		/* TODO destroy window */
		free(win);
	}
}


/* video mode switching */
static int set_vidmode(int xsz, int ysz)
{
	return 0;	/* TODO */
}

static int get_vidmode(int *xsz, int *ysz)
{
	return 0;	/* TODO */
}


/* create/destroy windows */
static int create_window(int xsz, int ysz, unsigned int flags)
{
	NSWindow *nswin;
	NSRect rect;
	NSView *view;
	unsigned int style;
	struct window *win;
	static int next_id = 1;

	if(!(win = malloc(sizeof *win))) {
		return -1;
	}

	/* create the window and attach the OpenGL view */
	rect.origin.x = rect.origin.y = 0;
	rect.size.width = xsz;
	rect.size.height = ysz;

	style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask |
		NSResizableWindowMask;

	nswin = [[NSWindow alloc] initWithContentRect: rect styleMask: style
		backing: NSBackingStoreBuffered defer: YES];
	view = [[OpenGLView alloc] initWithFrame: rect];
	[nswin setContentView: view];
	[view release];

	win->win = nswin;
	win->view = view;
	win->wid = next_id++;
	win->next = winlist;
	winlist = win;

	return win->wid;
}

static void close_window(int wid)
{
	struct window *win, *prev, dummy;
	sgl_close_callback_t close_func;

	dummy.next = win = winlist;
	prev = &dummy;

	while(win) {
		if(win->wid == wid) {
			if(!(close_func = sgl_get_callback(SGL_CLOSE))) {
				close_func(wid);
			}
			[win->win close];

			if(active_win == win) {
				activate_window(winlist);
			}

			prev->next = win->next;
			free(win);
			return;
		}
		prev = win;
		win = win->next;
	}
}


/* window management */
static int set_active(int wid)
{
}

static struct window *find_window(int wid)
{
	struct window *win = winlist;

	while(win) {
		if(win->win == id) {
			return win;
		}
		win = win->next;
	}
	return 0;
}

static int activate_window(struct window *win)
{
	if(!win) {
		return -1;
	}
	[win->ctx makeCurrentContext];
	active_win = win;
	return 0;
}

static int set_title(const char *str)
{
	NSString *nsstr;

	nsstr = [[NSString alloc] initWithCString: str encoding: NSASCIIStringEncoding];
	[active_win->win setTitle: nsstr];
	[nsstr release];
}

static void redisplay(void)
{
	[active_win->view setNeedsRedisplay: YES];
}

static void swap_buffers(void)
{
	[active_win->flushBuffer];
}


static int get_modifiers(void)
{
	return 0;	/* TODO */
}


/* event handling and friends */
static void set_event(int idx, int enable)
{
}

static int process_events(void)
{
	NSAutoreleasePool *pool;
	int state;
	sgl_idle_callback_t idle;

	pool = [[NSAutoreleasePool alloc] init];

	for(;;) {
		NSEvent *ev = [app nextEventMatchingMask: NSAnyEventMask
			untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES];
		if(ev == nil) {
			break;
		}

		handle_event(ev);
	}
}

static int handle_event(NSEvent *ev)
{
	switch([ev type]) {
	case NSKeyDown:
	case NSKeyUp:

}

#endif	/* USE_WSYS_MODULE_COCOA */
