//
//  GameModeSelectionLayer.m
//  Cartographic
//
//  Created by Starla Insigna on 8/18/11.
//  Copyright 2011 Four Island. All rights reserved.
//

#import "GameModeSelectionLayer.h"
#import "GameModeSelection.h"
#import <sqlite3.h>
#import "Cart_CollectAppDelegate.h"
#import "MainMenuLayer.h"
#import "TutorialMode.h"
#import "ClassicGameMode.h"
#import "NMPanelMenu.h"
#import "JumpGameMode.h"
#import "ZoomFadeTransition.h"

@implementation GameModeSelectionLayer

+ (CCScene*)scene
{
    CCScene* scene = [CCScene node];
    
    CCLayer* backgroundLayer = [[[CCLayer alloc] init] autorelease];
    CCSprite* backgroundImage = [CCSprite spriteWithFile:@"paintdaubs.png"];
    backgroundImage.position = ccp(240,160);
    [backgroundLayer addChild:backgroundImage];
    [scene addChild:backgroundLayer];
	
	GameModeSelectionLayer* layer = [GameModeSelectionLayer node];
	[scene addChild:layer];
	
	return scene;
}

- (id)init
{
    self = [super init];
    
    if (nil != self)
    {
        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        gameModes = [[NSMutableArray alloc] init];
        
        GameModeSelection* tutorialSelection = [GameModeSelection selectionWithName:@"Tutorial" location:@"Florence" filename:@"florence" unlocked:YES];
        [gameModes addObject:tutorialSelection];
        
        GameModeSelection* collectSelection;
        
        if ([defaults boolForKey:@"hasDoneTutorial"])
        {
            collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlocked:YES];
        } else {
            collectSelection = [GameModeSelection selectionWithName:@"Collect" location:@"Paris" filename:@"paris" unlockCondition:@"Beat the tutorial!"];
        }
        
        [gameModes addObject:collectSelection];
        
        GameModeSelection* jumpSelection;
        
        if ([defaults boolForKey:@"unlockedJumpMode"])
        {
            jumpSelection = [GameModeSelection selectionWithName:@"Jump" location:@"Venice" filename:@"venice" unlocked:YES];
        } else {
            jumpSelection = [GameModeSelection selectionWithName:@"Jump" location:@"Venice" filename:@"venice" unlockCondition:@"Get 3000 points in Collect!"];
        }
        
        [gameModes addObject:jumpSelection];
        
        CCMenu* menu = [CCMenu menuWithItems:nil];
        float onePanelWide = 128;
        float padding = 15;
        float totalPanelWidth = onePanelWide + padding*2;
        float numberOfPanels = [gameModes count];
        float totalWidth = numberOfPanels * totalPanelWidth;
        int currentWorldOffset = [defaults integerForKey:@"lastSelectedMode"];
        CCLayer* panels = [CCLayer node];
        
        for (GameModeSelection* gameMode in gameModes)
        {
            [gameMode setDelegate:self];
            [menu addChild:gameMode];
        }
        
        [menu alignItemsHorizontallyWithPadding:padding*2];
        [panels addChild:menu];
        [self addChild:panels];
        
        pageControl = [[UIPageControl alloc] init];
        pageControl.numberOfPages = numberOfPanels;
        pageControl.currentPage = currentWorldOffset;
        pageControl.frame = CGRectMake(0, 250, 480, 20);
        
        menu.position = ccpAdd(menu.position, ccp(totalWidth/2 - totalPanelWidth/2, 320));
        touchDelegatingView = [[TouchDelegatingView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
        scrollView = [[CocosOverlayScrollView alloc] initWithFrame:CGRectMake(0, 0, totalPanelWidth, 320) numPages:numberOfPanels width:totalPanelWidth layer:panels pageControl:pageControl];
        touchDelegatingView.scrollView = scrollView;
        [scrollView setContentOffset:CGPointMake(currentWorldOffset*totalPanelWidth+1,0) animated:NO];
        
        CCMenuItemImage* newgameMenuItem = [CCMenuItemImage itemFromNormalImage:@"back.png" selectedImage:@"back2.png" target:self selector:@selector(mainmenu)];
        NMPanelMenu* myMenu = [NMPanelMenu menuWithItems:newgameMenuItem, nil];
        myMenu.position = ccp(240, 30);
        [self addChild:myMenu];
    }
    
    return self;
}

- (void)onEnterTransitionDidFinish
{
    [super onEnterTransitionDidFinish];
    
    [[[CCDirector sharedDirector] openGLView] addSubview:pageControl];
    [[[CCDirector sharedDirector] openGLView] addSubview:touchDelegatingView];
    [[[CCDirector sharedDirector] openGLView] addSubview:scrollView];
}

- (void)onExit
{
    [super onExit];
    
    [touchDelegatingView removeFromSuperview];
    [scrollView removeFromSuperview];
    [pageControl removeFromSuperview];
}

- (void)mainmenu
{
    [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
}

- (void)didSelectGameMode:(GameModeSelection *)gameMode
{
    [scrollView setScrollEnabled:NO];
    [pageControl removeFromSuperview];
    
    CGPoint opp = [scrollView convertPoint:gameMode.position toView:[[CCDirector sharedDirector] openGLView]];
    CGPoint endPosition = ccp(0-(opp.x+158), opp.y);
    
    if ([gameMode.name isEqual:@"Tutorial"])
    {
        [[CCDirector sharedDirector] replaceScene:[ZoomFadeTransition transitionWithDuration:5.0f scene:[TutorialMode scene] position:endPosition]];
    } else if ([gameMode.name isEqual:@"Collect"])
    {
        [[CCDirector sharedDirector] replaceScene:[ZoomFadeTransition transitionWithDuration:5.0f scene:[ClassicGameMode scene] position:endPosition]];
    } else if ([gameMode.name isEqual:@"Jump"])
    {
        [[CCDirector sharedDirector] replaceScene:[ZoomFadeTransition transitionWithDuration:5.0f scene:[JumpGameMode scene] position:endPosition]];
    }
}

@end