#import "BWMatrixPlus.h"
#import "BWOverworld.h"

@implementation BWMatrixPlus

// Option "Show Key Areas"

/*unsigned short areas[46] =
{
	0x462F , 0x4663 , 0x4664 , 0x4665 ,
	0x465C , 0x465E , 0x4661 , 0x4660 , 0x465F , 0x4662 , 0x465B ,
	0x4639 , 0x463A , 0x463B , 0x463C , 0x4640 , 0x4641 , 0x4630 , 0x4632 , 0x4634 , 0x463F , 0x463E , 0x463D ,
	0x4631 , 0x4633 , 0x4646 , 0x4635 , 0x4649 , 0x464A , 0x464B , 0x464C , 0x4648 ,
	0x4637 , 0x4647 , 0x464D ,
	0x4638 ,
	0x4642 , 0x4643 , 0x4644 , 0x4645 ,
	0x4636 , 0x464E , 0x464F , 0x4658 , 0x4659 , 0x465A
};*/

unsigned short ka[55] =
{
	0 , 52 , 53 , 54 ,
	45 , 47 , 50 , 49 , 48 , 51 , 44 ,
	10 , 11 , 12 , 13 , 17 , 18 , 1 , 3 , 5 , 16 , 15 , 14 ,
	2 , 4 , 23 , 6 , 26 , 27 , 28 , 29 , 25 ,
	8 , 24 , 30 ,
	9 ,
	19 , 20 , 21 , 22 ,
	7 , 31 , 32 , 41 , 42 , 43 ,
	33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 , 46
};

- (void)awakeFromNib
{
	ca = 3;
	sel = -1;
}

- (void)loadAreas:(NSMutableData *)theRom
{
	unsigned const char *ptr;
	int i;
	
	ptr = [theRom bytes];
	ptr += 0x462F;
	
	for (i=0; i<55; i++) {
		
		keyAreas[i][0].x = *(ptr + 0x3F);
		keyAreas[i][0].y = *ptr;
		NSLog(@"--------");
		
		if (i >= 41 && i <= 54) keyAreas[i][0].y -= 128;
		if (keyAreas[i][0].x > 64) keyAreas[i][0].x -= 64;
		
		keyAreas[i][0].y -= 30;
		
		NSLog(@"KA (%d) = %d,%d",i,keyAreas[i][0].x,keyAreas[i][0].y);
		//NSLog(@"area %d : %X,%X",i,*(ptr + 0x3F),*ptr);
		//NSLog(@"area %d : %d,%d",i,*(ptr + 0x3F),*ptr);
		//NSLog(@"area %d : %d,%d",i,keyAreas[i][0].x,keyAreas[i][0].y);
		
		ptr++;
		
	}
	
	//NSLog(@"--------");
	ptr = [theRom bytes];
	ptr += 0x862F;
	
	for (i=0; i<55; i++) {
		
		keyAreas[i][1].x = *(ptr + 0x3F);
		keyAreas[i][1].y = *ptr;
		
		if (i >= 40 && i <= 54) keyAreas[i][1].y -= 128;
		if (keyAreas[i][1].x > 64) keyAreas[i][1].x -= 64;
		//NSLog(@"area %d : %X,%X",i,*(ptr + 0x3F),*ptr);
		//NSLog(@"area %d : %d,%d",i,*(ptr + 0x3F),*ptr);
		//NSLog(@"area %d : %d,%d",i,keyAreas[i][1].x,keyAreas[i][1].y);
		
		ptr++;
		
	}
	
	//NSLog(@"--------");
	ptr = [theRom bytes];
	ptr += 0x610C;
	
	for (i=0; i<62; i++) {
		
		keyAreas[i][2].x = *(ptr + 0x3F);
		keyAreas[i][2].y = *ptr;
		
		if (i >= 41 && i <= 54) keyAreas[i][2].y -= 128;
		if (keyAreas[i][2].x > 64) keyAreas[i][2].x -= 64;
		//NSLog(@"area %d : %X,%X",i,*(ptr + 0x3F),*ptr);
		//NSLog(@"area %d : %d,%d",i,*(ptr + 0x3F),*ptr);
		//NSLog(@"area %d : %d,%d",i,keyAreas[i][2].x,keyAreas[i][2].y);
		
		ptr++;
		
	}
	[self setNeedsDisplay:YES];
	NSLog(@"Key Areas Loaded");
	NSLog(@"--------");
}

- (void)changeArea:(unsigned char)na
{
	if (ca != na); {
		ca = na;
		max = ((ca == 2) ? 62 : 55);
		[self changeKeyAreasMenu:ca];
	}
}

- (void)changeKeyAreasMenu:(int)area
{
	int i;
	int c = 0;
	
	NSString *str = [[NSBundle mainBundle] pathForResource:@"KeyAreasWest" ofType:@"plist"];
	
	NSArray *menuArray = [NSArray arrayWithContentsOfFile:str];
	NSDictionary *item;
	NSArray *subArray;
	NSMenu *mta;
	
	NSEnumerator *en = [menuArray objectEnumerator];
	while (item = [en nextObject]) {
		
		mta = [[NSMenu alloc] initWithTitle:[item objectForKey:@"Name"]];
		subArray = [item objectForKey:@"Items"];
		
		for (i=0; i<[subArray count]; i++) {
			[mta addItemWithTitle:[subArray objectAtIndex:i] action:@selector(keyAreaFromMenu:) keyEquivalent:@""];
			[[mta itemAtIndex:[mta numberOfItems]-1] setTarget:self];
			[[mta itemAtIndex:[mta numberOfItems]-1] setTag:c];
			c++;
		}
		
		NSMenuItem *ni = [[NSMenuItem alloc] initWithTitle:[item objectForKey:@"Name"] action:nil keyEquivalent:@""];
		[menuKeyAreas addItem:ni];
		[menuKeyAreas setSubmenu:mta forItem:ni];
		
	}
	
	NSLog(@"Key Areas Menu Loaded");
}

- (void)drawRect:(NSRect)theRect
{
	[super drawRect:[self bounds]];
	
	char p1,p2;
	if (sel == -1) {
		p1 = 0; p2 = max;
	} else {
		p1 = sel; p2 = sel+1;
	}
	
	NSBezierPath *bp;
	NSRect rect;
	
	[[NSColor colorWithDeviceRed:0.8 green:0.0 blue:0.8 alpha:0.5] set];
	
	int i;
	for (i=p1; i<p2; i++) {
		
		if (keyAreas[i][ca].x == 0) continue;
		
		rect.origin.x = keyAreas[i][ca].x * 16;
		rect.origin.y = keyAreas[i][ca].y * 16;
		rect.size.width = 16;
		rect.size.height = 16;
		
		bp = [NSBezierPath bezierPathWithRect:rect];
		[bp fill];
		
	}
	NSLog(@"Redraw");
	NSLog(@"--------");
}

- (void)showAll
{
	sel = -1;
	[self setNeedsDisplay:YES];
}

- (void)keyAreaFromMenu:(id)sender
{
	[objOverworld setSelectTool:nil];
	sel = ka[[sender tag]];
	//NSLog(@"Selection : %d",[sender tag]);
	[self setNeedsDisplay:YES];
}

- (void)clickArea:(short)location
{
	int i;
	unsigned char x,y;
	
	x = location >> 8;
	y = location & 0x0F;
	//NSLog(@"Clicked Location : %d,%d",x,y);
	
	if (sel == -1) {
		
		for (i=0; i<max; i++) {
			if ( (x == keyAreas[i][ca].x) && (y == keyAreas[i][ca].y) ) {
				sel = i;
				break;
			}
		}
		if (i == max) sel = -1;
		[self setNeedsDisplay:YES];
		
	} else {
		
		//NSLog(@"sel = %d",sel);
		keyAreas[sel][ca].x = x;
		keyAreas[sel][ca].y = y;
		[self setNeedsDisplay:YES];
		
	}
}

- (BOOL)acceptsFirstMouse:(NSEvent *)ev
{
	return NO;
}

@end


