//
//  TestCamOverlay.m
//  misc_test_app
//
//  Created by david van brink on 7/31/10.
//  Copyright 2010 omino.com. All rights reserved.
//

#import "TestCamOverlay.h"
#include "OmCoreGraphicsUtils.h"

#import <GameKit/GameKit.h>


@implementation TestCamOverlay


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) 
    {
        // Initialization code
        self->peerName = @"none";
        [self->peerName retain];
        
        GKPeerPickerController*		picker;
        
//        self.gameState = kStatePicker;			// we're going to do Multiplayer!
        
        picker = [[GKPeerPickerController alloc] init]; // note: picker is released in various picker delegate methods when picker use is done.
        picker.delegate = self;
        [picker show]; // show the Peer Picker
        
        
        
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect 
{
    CGContextRef g = UIGraphicsGetCurrentContext();
    GFillRectR(g, CGRectMake(0,0,320,480), 0x400080);
    GSetFillColor(g, 0xc000);
    GDrawPip(g, 160, 240, 100, true);
    
    G(g);
    GSetFont(g, 2, 22);
    GSetFillColor(g,0x202020);
    if(self->peerName);
    {
        NSSTRING_TO_C(self->peerName,pn);
        GPrintf(g,10,300,"peer: %s",pn);
    }
    
//    self.cont
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}


- (void)dealloc {
    [super dealloc];
}

- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type 
{
	self->session = [[GKSession alloc] initWithSessionID:@"gktank" sessionMode:GKSessionModePeer]; /*displayName:@"omino peer picker"*/ 
	return [session autorelease]; // peer picker retains a reference, so autorelease ours so we don't leak.
}

- (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error
{
    
}

- (void)session:(GKSession *)rSession didReceiveConnectionRequestFromPeer:(NSString *)peerId
{
    [rSession acceptConnectionFromPeer:peerId error:nil];
    [self->peerName retain];
    
}

//
// invalidate session
//
- (void)invalidateSession:(GKSession *)aSession 
{
    if(aSession != nil)
    {
		[aSession disconnectFromAllPeers]; 
		aSession.available = NO; 
		[aSession setDataReceiveHandler: nil withContext: NULL]; 
		aSession.delegate = nil; 
	}
}


- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker { 
	// Peer Picker automatically dismisses on user cancel. No need to programmatically dismiss.
    
	// autorelease the picker. 
    
    if(self->session != nil)	{
		[self invalidateSession:self->session];
		self->session = nil;
	}
    
	picker.delegate = nil;
    [picker autorelease]; 
}    

- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)aSession 
{
    self->peerName = peerID;
	[picker dismiss];
	picker.delegate = nil;
	[picker autorelease];
} 





@end
