//
//  ViewController.m
//  FinalProject
//
//  Created by Sean Campbell on 12/2/13.
//  Copyright (c) 2013 Sean Campbell. All rights reserved.
//

#import "RootViewController.h"
#import "HighScore.h"

@interface RootViewController ()

@end

@implementation RootViewController

@synthesize managedObjectContext;

- (id)initWithManagedObjectContext: (NSManagedObjectContext *)context
{
    if (self = [super init]) {
        self.managedObjectContext = context;
        
        self.title = @"Main Menu";
                        
        UIBarButtonItem *aboutItem = [[UIBarButtonItem alloc] init];
        aboutItem.title = @"Info";
        aboutItem.target = self;
        aboutItem.action = @selector(about);
        self.navigationItem.leftBarButtonItem = aboutItem;
        aboutItem = nil;
        
        UIBarButtonItem *highScoresItem = [[UIBarButtonItem alloc] init];
        highScoresItem.title = @"High Scores";
        highScoresItem.target = self;
        highScoresItem.action = @selector(highScores);
        self.navigationItem.rightBarButtonItem = highScoresItem;
        highScoresItem = nil;
        
        // Get everything from the database. If the database is empty,
        // then we have to initiate it. Otherwise, we don't have to do
        // anything.
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"HighScores" inManagedObjectContext:managedObjectContext];
        request.entity = entity;
        // Store high scores from the database into the high scores array.
        NSError *error = nil;
        NSArray *stuffInDatabase = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
        // Clean up memory.
        request = nil;

        // Initiate the database with the name of each set. This way, if the user
        // completes individual symbols without going through the whole set, he/she
        // will still be able to see the individual symbol scores.
        symbolSetNames = [[NSArray alloc] initWithObjects:@"Capital Alphabet", @"Lowercase Alphabet", @"Digits", @"Shapes", nil];
        if (stuffInDatabase.count == 0) {
            for (NSString *symbolSet in symbolSetNames) {
                HighScore *score = [NSEntityDescription insertNewObjectForEntityForName: @"HighScores" inManagedObjectContext:self.managedObjectContext];
                score.symbol = [NSString stringWithFormat:@"%@ Set", symbolSet];
                score.date = [NSDate date];
            }
            [managedObjectContext save:&error];
        }
        
        NSArray *capitalAlphabetSymbols = [[NSArray alloc] initWithObjects: @"Capital A", @"Capital B", @"Capital C", @"Capital D", @"Capital E", @"Capital F", @"Capital G", @"Capital H", @"Capital I", @"Capital J", @"Capital K", @"Capital L", @"Capital M", @"Capital N", @"Capital O", @"Capital P", @"Capital Q", @"Capital R", @"Capital S", @"Capital T", @"Capital U", @"Capital V", @"Capital W", @"Capital X", @"Capital Y", @"Capital Z", nil ];
        NSArray *lowercaseAlphabetSymbols = [[NSArray alloc] initWithObjects: @"Lowercase A", @"Lowercase B", @"Lowercase C", @"Lowercase D", @"Lowercase E", @"Lowercase F", @"Lowercase G", @"Lowercase H", @"Lowercase I", @"Lowercase J", @"Lowercase K", @"Lowercase L", @"Lowercase M", @"Lowercase N", @"Lowercase O", @"Lowercase P", @"Lowercase Q", @"Lowercase R", @"Lowercase S", @"Lowercase T", @"Lowercase U", @"Lowercase V", @"Lowercase W", @"Lowercase X", @"Lowercase Y", @"Lowercase Z", nil ];
        NSArray *digitSymbols = [[NSArray alloc] initWithObjects: @"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil ];
        NSArray *shapeSymbols = [[NSArray alloc] initWithObjects: @"Square", @"Triangle", @"Circle", nil ];
        symbolSets = [[NSArray alloc] initWithObjects: capitalAlphabetSymbols, lowercaseAlphabetSymbols, digitSymbols, shapeSymbols, nil ];
        capitalAlphabetSymbols = nil;
        lowercaseAlphabetSymbols = nil;
        digitSymbols = nil;
        shapeSymbols = nil;
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        aboutCtrl = [[AboutViewController alloc] init];
        highScoreCtrl = [[HighScoreViewController alloc] init];
        drawCtrl = [[DrawViewController alloc] init];
        symbolSetCtrl = [[SymbolSetViewController alloc] init];
        drawCtrl.managedObjectContext = self.managedObjectContext;
        highScoreCtrl.managedObjectContext = self.managedObjectContext;
    }
    
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

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

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    symbolSets = nil;
    aboutCtrl = nil;
    highScoreCtrl = nil;
    drawCtrl = nil;
    symbolSetCtrl = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

- (void)about
{
    [self.navigationController pushViewController:aboutCtrl animated:YES];
}

- (void)highScores
{
    [self.navigationController pushViewController:highScoreCtrl animated:YES];
}

#pragma mark Table view methods

- (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView
{
    return 1;
}

- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section
{
    return symbolSets.count;
}


- (BOOL) tableView: (UITableView *) tableView canEditRowAtIndexPath: (NSIndexPath *) indexPath
{
    return NO;
}

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
    cell.textLabel.text = [NSString stringWithFormat:@"  %@", [symbolSetNames objectAtIndex:indexPath.row]];
    cell.textLabel.font = [UIFont fontWithName:@"Arial" size:14];

    /*
     cell.textLabel.font = [UIFont fontWithName: @"Helvetica-Bold" size: 14];
     cell.textLabel.textColor = [UIColor blueColor];
     cell.textLabel.highlightedTextColor = [UIColor yellowColor];
     cell.imageView.image = [UIImage imageNamed: @"4seasons.png"];
     */
    return cell;
}

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
    symbolSetCtrl.drawCtrl = drawCtrl;
    symbolSetCtrl.symbols = [symbolSets objectAtIndex: indexPath.row];
    symbolSetCtrl.symbolSet = [symbolSetNames objectAtIndex:indexPath.row];
   [self.navigationController pushViewController:symbolSetCtrl animated:YES];
}

@end
