//
//  ExploreViewController.m
//  Allzic
//
//  Created by Alia Bourouis on 24/03/14.
//  Copyright (c) 2014 Alia Bourouis. All rights reserved.
//

#import "ExploreViewController.h"
#import "AppDelegate.h"

@interface ExploreViewController ()

@end

@implementation ExploreViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.title = self.screenName = @"explore";
    
    /*
    NSMutableDictionary *event;
    event = [[GAIDictionaryBuilder createEventWithCategory:@"UI"
                                                    action:@"navigation"
                                                     label:@"explore"
                                                     value:nil] build];
    [[GAI sharedInstance].defaultTracker send:event];
    [[GAI sharedInstance] dispatch];
    */
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        dataTable.rowHeight = 90.0f;
        [self displayBackView];
    }
    return self;
}

- (void)backToParentView {
    
    [appDelegate.window setRootViewController:appDelegate.introVC];
}

//-----------------------------------------
//		TableView Delegate
//-----------------------------------------

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    int nbRowInSection = 5;
    return nbRowInSection;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"dataCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //if(cell == nil) {
    //NSLog(@"cell = nil");
    
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
    UIView *background = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f , tableView.frame.size.width,tableView.rowHeight)];
    if (indexPath.row % 2) {
        background.backgroundColor = [UIColor colorWithRed:232.0f/255.0f green:243.0f/255.0f blue:248.0f/248.0f alpha:1.0f];
    } else {
        background.backgroundColor = [UIColor whiteColor];
    }
    [cell.contentView addSubview:background];
    
    UIImageView *radioLogoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 10.0f, tableView.rowHeight - 20.0f, tableView.rowHeight - 20.0f)];
    [radioLogoImage setContentMode:UIViewContentModeScaleAspectFit];
    [radioLogoImage setClipsToBounds:YES];
    [cell.contentView addSubview:radioLogoImage];
    
    UILabel *radioTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(radioLogoImage.frame.origin.x + radioLogoImage.frame.size.width + 10.0f, 10.0f, tableView.frame.size.width - radioLogoImage.frame.origin.x - radioLogoImage.frame.size.width - 40.0f, tableView.rowHeight - 20.0f)];
    [radioTitleLabel setFont:[UIFont systemFontOfSize:16]];
    [radioTitleLabel setNumberOfLines:0];
    [radioTitleLabel setBackgroundColor:[UIColor clearColor]];
    [radioTitleLabel setTextColor:[UIColor blackColor]];
    [cell.contentView addSubview:radioTitleLabel];
    
    
    switch (indexPath.row) {
        case 0: {
            [radioTitleLabel setText:@"Écoutes récentes"];
            [radioLogoImage setImage:[UIImage imageNamed:@"lastRadioPicto"]];
        }
            break;
        case 1: {
            [radioTitleLabel setText:@"Autour de moi"];
            [radioLogoImage setImage:[UIImage imageNamed:@"aroundMeRadioPicto"]];
        }
            break;
        case 2: {
            [radioTitleLabel setText:@"Autour du monde"];
            [radioLogoImage setImage:[UIImage imageNamed:@"world"]];
        }
            break;
        case 3: {
            [radioTitleLabel setText:@"Découvrir"];
            [radioLogoImage setImage:[UIImage imageNamed:@"randomRadioPicto"]];
        }
            break;
        case 4: {
            [radioTitleLabel setText:@"Top 10 des radios"];
            [radioLogoImage setImage:[UIImage imageNamed:@"topRadioPicto"]];
        }
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    cell.tag            = indexPath.row;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.row) {
        //Ecoutes recentes
        case 0: {
            [appDelegate.lastRadiosVC refreshTableWithData:appDelegate.dataController.lastRadioArray];
            [appDelegate.window setRootViewController:appDelegate.lastRadiosVC];
        }
            break;
        //Autour de moi
        case 1: {
            [appDelegate.aroundMeRadioVC refreshTableWithData:nil];
            [appDelegate.dataController parseLocationRadiosWithAccuracy:appDelegate.dataController.currentAccuracy];
            [appDelegate.window setRootViewController:appDelegate.aroundMeRadioVC];
        }
            break;
        //Autour du monde
        case 2: {
            [appDelegate.countryListVC refreshTableWithData:nil];
            [appDelegate.dataController parseCountries];
            [appDelegate.countryListVC setCurrentTabIndex:2];
            [appDelegate.window setRootViewController:appDelegate.countryListVC];
        }
            break;
        //Decouvrir
        case 3: {
            [appDelegate.randomRadioVC refreshTableWithData:nil];
            [appDelegate.dataController parseRandomRadios];
            [appDelegate.window setRootViewController:appDelegate.randomRadioVC];
        }
            break;
        //Top 10 des radios
        case 4: {
            [appDelegate.randomRadioVC refreshTableWithData:nil];
            [appDelegate.dataController parseTopRadios];
            [appDelegate.window setRootViewController:appDelegate.topRadioVC];
        }
            break;
        default:
            break;
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
