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

#import "FavoriteViewController.h"
#import "AppDelegate.h"

@interface FavoriteViewController ()

@end

@implementation FavoriteViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    self.title = self.screenName = @"preferences";
    
    /*
    NSMutableDictionary *event;
    event = [[GAIDictionaryBuilder createEventWithCategory:@"UI"
                                                    action:@"navigation"
                                                     label:@"preferences"
                                                     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
        [self.dataTable setSectionHeaderHeight:44.0f];
        [self displayBackView];
    }
    return self;
}

- (void)selectRadioAtIndex:(id)sender {
    
    [appDelegate.playerVC.navTitleImage removeFromSuperview];
    int selectedRadioIndex = [sender intValue];
    [appDelegate selectRadioWithData:[dataArray objectAtIndex:1] atIndex:selectedRadioIndex];
}

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


//-----------------------------------------
//		IconDownloader Delegate
//-----------------------------------------

- (void)appImageDidLoad:(IconDownloader *)downloader {
    
    if (downloader.indexPathInTableView != nil && [[dataArray objectAtIndex:1] count] > downloader.indexPathInTableView.row) {
        [dataTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:downloader.indexPathInTableView] withRowAnimation:UITableViewRowAnimationNone];
    }
}


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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.dataTable.frame.size.width, self.dataTable.sectionHeaderHeight)];
    header.backgroundColor = [UIColor colorWithRed:22.0f/255.0f green:145.0f/255.0f blue:189.0f/255.0f alpha:1.0f];
    UILabel *title = [[UILabel alloc]initWithFrame:header.frame];
    [title setTextAlignment:NSTextAlignmentCenter];
    [title setFont:[UIFont systemFontOfSize:16]];
    [title setBackgroundColor:[UIColor clearColor]];
    [title setTextColor:[UIColor whiteColor]];
    if (section == 0) {
        [title setText:@"CATÉGORIES FAVORITES"];
    } else if (section == 1) {
        [title setText:@"RADIOS FAVORITES"];
    }
    [header addSubview:title];
    return header;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    int nbRowInSection = 0;
    if (dataArray != nil && [dataArray objectAtIndex:section] != nil) {
        nbRowInSection = [[dataArray objectAtIndex:section] count];
        if (nbRowInSection == 0) {
            nbRowInSection = 1;
        }
    }
    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];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    cell.tag            = indexPath.row;
    
    //section des categories favorites
    if (indexPath.section == 0) {
        if ([[dataArray objectAtIndex:0] count] > 0) {
            UILabel *categoryTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, tableView.frame.size.width - 40.0f, tableView.rowHeight - 20.0f)];
            [categoryTitleLabel setText:[[[dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"title"]];
            [categoryTitleLabel setFont:[UIFont systemFontOfSize:16]];
            [categoryTitleLabel setNumberOfLines:0];
            [categoryTitleLabel setBackgroundColor:[UIColor clearColor]];
            [categoryTitleLabel setTextColor:[UIColor blackColor]];
            [cell.contentView addSubview:categoryTitleLabel];
        }
        else {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType  = UITableViewCellAccessoryNone;
            UILabel *categoryTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, tableView.frame.size.width - 40.0f, tableView.rowHeight - 20.0f)];
            [categoryTitleLabel setText:@"Aucune catégorie favorite"];
            [categoryTitleLabel setFont:[UIFont systemFontOfSize:16]];
            [categoryTitleLabel setNumberOfLines:0];
            [categoryTitleLabel setBackgroundColor:[UIColor clearColor]];
            [categoryTitleLabel setTextColor:[UIColor blackColor]];
            [cell.contentView addSubview:categoryTitleLabel];
        }
    }
    //section des radios favorites
    else {
        if ([[dataArray objectAtIndex:1] count] > 0) {
            UIImageView *radioLogoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 5.0f, tableView.rowHeight - 10.0f, tableView.rowHeight - 10.0f)];
            [radioLogoImage setContentMode:UIViewContentModeScaleAspectFit];
            [radioLogoImage setClipsToBounds:YES];
            [cell.contentView addSubview:radioLogoImage];
            
            if([iconDownloaderDictionary objectForKey:indexPath] == nil) {
                IconDownloader *iconDownloader = [[IconDownloader alloc] init];
                iconDownloader.indexPathInTableView = indexPath;
                iconDownloader.delegate = self;
                [iconDownloaderDictionary setObject:iconDownloader forKey:indexPath];
                [iconDownloader startDownloadAtUrl:[[[dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"thumb"] withMaxSize:(tableView.rowHeight - 10.0f)*2];
            }
            [radioLogoImage setImage:[[iconDownloaderDictionary objectForKey:indexPath] appIcon]];
            
            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 - 20.0f, tableView.rowHeight - 20.0f)];
            [radioTitleLabel setText:[[[dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"title"]];
            [radioTitleLabel setFont:[UIFont systemFontOfSize:16]];
            [radioTitleLabel setNumberOfLines:0];
            [radioTitleLabel setBackgroundColor:[UIColor clearColor]];
            [radioTitleLabel setTextColor:[UIColor blackColor]];
            [cell.contentView addSubview:radioTitleLabel];
        }
        else {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType  = UITableViewCellAccessoryNone;
            UILabel *radioTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, tableView.frame.size.width - 20.0f, tableView.rowHeight - 20.0f)];
            [radioTitleLabel setFont:[UIFont systemFontOfSize:16]];
            [radioTitleLabel setNumberOfLines:0];
            [radioTitleLabel setBackgroundColor:[UIColor clearColor]];
            [radioTitleLabel setTextColor:[UIColor blackColor]];
            [radioTitleLabel setText:@"Aucune radio favorite"];
            [cell.contentView addSubview:radioTitleLabel];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && [[dataArray objectAtIndex:0] count] > 0) {
        [appDelegate selectCategoryWithData:[[dataArray objectAtIndex:0] objectAtIndex:indexPath.row]];
        [appDelegate.categoriesRadiosVC setParentViewController:self];
        [appDelegate.categoriesRadiosVC setCurrentTabIndex:3];
    }
    else if (indexPath.section == 1 && [[dataArray objectAtIndex:1] count] > 0) {
        [appDelegate setOldVC:self];
        [appDelegate.playerVC setNavigationTitle:@"Favoris"];
        [self selectRadioAtIndex:[NSNumber numberWithInt:indexPath.row]];
    }
}

- (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
