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

#import "CategoriesViewController.h"
#import "AppDelegate.h"

@interface CategoriesViewController ()

@end

@implementation CategoriesViewController

@synthesize selectedFilterIndex;

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

- (void)locationFilterButtonPressed {
    
    selectedFilterIndex = 0;
    [locationFilterView setBackgroundColor:[UIColor colorWithRed:31.0f/255.0f green:139.0f/255.0f blue:186.0f/255.0f alpha:1.0f]];
    [categoryFilterView setBackgroundColor:[UIColor colorWithRed:143.0f/255.0f green:197.0f/255.0f blue:220.0f/255.0f alpha:1.0f]];
    [self refreshTableWithData:self.dataArray];
}

- (void)categoryFilterButtonPressed {
    
    selectedFilterIndex = 1;
    [locationFilterView setBackgroundColor:[UIColor colorWithRed:143.0f/255.0f green:197.0f/255.0f blue:220.0f/255.0f alpha:1.0f]];
    [categoryFilterView setBackgroundColor:[UIColor colorWithRed:31.0f/255.0f green:139.0f/255.0f blue:186.0f/255.0f alpha:1.0f]];
    [self refreshTableWithData:self.dataArray];
    if (self.dataArray == nil || dataArray.count == 0) {
        [appDelegate.dataController parseCategories];
    }
}

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


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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    [self.loadingTableView setFrame:CGRectMake((dataTable.frame.size.width - 240.0f)/2, dataTable.frame.origin.y + dataTable.sectionHeaderHeight + 60.0f, 240.0f, 60.0f)];
    [self.noResultTableLabel setFrame:CGRectMake(dataTable.frame.origin.x + 10.0f, dataTable.frame.origin.y + dataTable.sectionHeaderHeight + 60.0f, dataTable.frame.size.width - 20.0f, dataTable.rowHeight - 10.0f)];
    
    UIView *filterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, appDelegate.screenBounds.size.width, 44.0f)];
    [filterView setBackgroundColor:[UIColor whiteColor]];
    
    locationFilterView = [[UIView alloc] initWithFrame:CGRectMake(5.0f, 5.0f, (filterView.frame.size.width / 2) - 7.5f, filterView.frame.size.height - 10.0f)];
    [filterView addSubview:locationFilterView];
    
    UILabel *locationFilterLabel = [[UILabel alloc] initWithFrame:locationFilterView.frame];
    [locationFilterLabel setBackgroundColor:[UIColor clearColor]];
    [locationFilterLabel setTextColor:[UIColor whiteColor]];
    [locationFilterLabel setTextAlignment:NSTextAlignmentCenter];
    [locationFilterLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [locationFilterLabel setText:@"Par Localisation"];
    [filterView addSubview:locationFilterLabel];
    
    UIButton *locationFilterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [locationFilterButton setFrame:locationFilterView.frame];
    [locationFilterButton addTarget:self action:@selector(locationFilterButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [filterView addSubview:locationFilterButton];
    
    categoryFilterView = [[UIView alloc] initWithFrame:CGRectMake((filterView.frame.size.width / 2) + 2.5f, 5.0f, (filterView.frame.size.width / 2) - 7.5f, filterView.frame.size.height - 10.0f)];
    [filterView addSubview:categoryFilterView];
    
    UILabel *categoryFilterLabel = [[UILabel alloc] initWithFrame:categoryFilterView.frame];
    [categoryFilterLabel setBackgroundColor:[UIColor clearColor]];
    [categoryFilterLabel setTextColor:[UIColor whiteColor]];
    [categoryFilterLabel setTextAlignment:NSTextAlignmentCenter];
    [categoryFilterLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [categoryFilterLabel setText:@"Par Genres"];
    [filterView addSubview:categoryFilterLabel];
    
    UIButton *categoryFilterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [categoryFilterButton setFrame:categoryFilterView.frame];
    [categoryFilterButton addTarget:self action:@selector(categoryFilterButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [filterView addSubview:categoryFilterButton];
    
    if (selectedFilterIndex == 0) {
        [locationFilterView setBackgroundColor:[UIColor colorWithRed:31.0f/255.0f green:139.0f/255.0f blue:186.0f/255.0f alpha:1.0f]];
        [categoryFilterView setBackgroundColor:[UIColor colorWithRed:143.0f/255.0f green:197.0f/255.0f blue:220.0f/255.0f alpha:1.0f]];
        [self.loadingTableView setHidden:YES];
        [self.noResultTableLabel setHidden:YES];
    } else if (selectedFilterIndex == 1) {
        [locationFilterView setBackgroundColor:[UIColor colorWithRed:143.0f/255.0f green:197.0f/255.0f blue:220.0f/255.0f alpha:1.0f]];
        [categoryFilterView setBackgroundColor:[UIColor colorWithRed:31.0f/255.0f green:139.0f/255.0f blue:186.0f/255.0f alpha:1.0f]];
    }
    
    
    
    return filterView;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    int nbRowInSection = 0;
    //par localisation
    if (selectedFilterIndex == 0) {
        nbRowInSection = 3;
    }
    //par categorie
    else if (dataArray != nil) {
        nbRowInSection = dataArray.count;
    }
    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];
    
    //par localisation
    if (selectedFilterIndex == 0) {
        UILabel *radioTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, tableView.frame.size.width - 40.0f, tableView.rowHeight - 20.0f)];
        switch (indexPath.row) {
            case 0: {
                [radioTitleLabel setText:@"Radios internationales"];
            }
                break;
            case 1: {
                [radioTitleLabel setText:@"Radios nationales"];
            }
                break;
            case 2: {
                [radioTitleLabel setText:@"Radios locales"];
            }
                break;
            default:
                break;
        }
        [radioTitleLabel setFont:[UIFont systemFontOfSize:16]];
        [radioTitleLabel setNumberOfLines:0];
        [radioTitleLabel setBackgroundColor:[UIColor clearColor]];
        [radioTitleLabel setTextColor:[UIColor blackColor]];
        [cell.contentView addSubview:radioTitleLabel];
    }
    //par categorie
    else {
        UILabel *radioTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, tableView.frame.size.width - 40.0f, tableView.rowHeight - 20.0f)];
        [radioTitleLabel setText:[[dataArray 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];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    cell.tag            = indexPath.row;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //par localisation
    if (selectedFilterIndex == 0) {
        switch (indexPath.row) {
            //radios internationales
            case 0: {
                [appDelegate.countryListVC setCurrentTabIndex:1];
                [appDelegate.countryListVC refreshTableWithData:nil];
                [appDelegate.dataController parseCountries];
                [appDelegate.window setRootViewController:appDelegate.countryListVC];
            }
                break;
            //radios nationales
            case 1: {
                [appDelegate.categoriesRadiosVC setParentViewController:self];
                [appDelegate.categoriesRadiosVC setCurrentTabIndex:1];
                NSMutableDictionary *category = [[NSMutableDictionary alloc] init];
                [category setObject:@"Nationale" forKey:@"title"];
                [appDelegate.categoriesRadiosVC setCategory:category];
                [appDelegate.categoriesRadiosVC refreshTableWithData:nil];
                [appDelegate.dataController parseNationalRadios];
                [appDelegate.window setRootViewController:appDelegate.categoriesRadiosVC];
            }
                break;
            //radios locales
            case 2: {
                [appDelegate.departmentListVC refreshTableWithData:nil];
                [appDelegate.dataController parseDepartmentsWithCountry:@"FR"];
                [appDelegate.window setRootViewController:appDelegate.departmentListVC];
            }
                break;
            default:
                break;
        }
    }
    //par categorie
    else if (selectedFilterIndex == 1) {
        [appDelegate selectCategoryWithData:[dataArray objectAtIndex:indexPath.row]];
        [appDelegate.categoriesRadiosVC setParentViewController:self];
        [appDelegate.categoriesRadiosVC setCurrentTabIndex:1];
    }
}

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