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

#import "AroundMeRadioViewController.h"
#import "AppDelegate.h"

@interface AroundMeRadioViewController ()

@end

@implementation AroundMeRadioViewController

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

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

- (void)accuracyChanged:(UISlider *)sender {
    
    if (sender.value <= 0.25) {
        [accuracySlider setValue:0];
        accuracy = 0;
    } else if (sender.value >= 0.75) {
        [accuracySlider setValue:1];
        accuracy = 2;
    } else {
        [accuracySlider setValue:0.5f];
        accuracy = 1;
    }
    if (accuracy != appDelegate.dataController.currentAccuracy) {
        [self refreshTableWithData:nil];
        [appDelegate.dataController parseLocationRadiosWithAccuracy:accuracy];
    }
}


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

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


//-----------------------------------------
//		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 *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];
    
    UIImageView *locationImage = [[UIImageView alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 30.0f, 30.0f)];
    [locationImage setContentMode:UIViewContentModeScaleAspectFit];
    [locationImage setClipsToBounds:YES];
    [locationImage setImage:[UIImage imageNamed:@"tabExplore"]];
    [header addSubview:locationImage];
    
    
    
    accuracySlider = [[UISlider alloc] initWithFrame:CGRectMake(60.0f, 0.0f, dataTable.frame.size.width - 110.0f, 50.0f)];
    [accuracySlider setContinuous:NO];
    [accuracySlider setMinimumTrackTintColor:[UIColor whiteColor]];
    [accuracySlider setMaximumTrackTintColor:[UIColor whiteColor]];
    accuracy = appDelegate.dataController.currentAccuracy;
    switch (accuracy) {
        //commune
        case 0: {
            [accuracySlider setValue:0];
        }
            break;
        //departement
        case 1: {
            [accuracySlider setValue:0.5];
        }
            break;
        //pays
        case 2: {
            [accuracySlider setValue:1];
        }
            break;
        default:
            break;
    }
    [accuracySlider addTarget:self action:@selector(accuracyChanged:) forControlEvents:UIControlEventValueChanged];
    
    /*
    UIView *cityMarker = [[UIView alloc] initWithFrame:CGRectMake(accuracySlider.frame.origin.x + 2, 25.0f, 2.0f, 12.0f)];
    [cityMarker setBackgroundColor:[UIColor whiteColor]];
    [header addSubview:cityMarker];
    
    UIView *departmentMarker = [[UIView alloc] initWithFrame:CGRectMake(accuracySlider.frame.origin.x + ((accuracySlider.frame.size.width - 2.0f)/ 2), 25.0f, 2.0f, 12.0f)];
    [departmentMarker setBackgroundColor:[UIColor whiteColor]];
    [header addSubview:departmentMarker];
    
    UIView *countryMarker = [[UIView alloc] initWithFrame:CGRectMake(accuracySlider.frame.origin.x + accuracySlider.frame.size.width - 4.0f, 25.0f, 2.0f, 12.0f)];
    [countryMarker setBackgroundColor:[UIColor whiteColor]];
    [header addSubview:countryMarker];
    */
    
    [header addSubview:accuracySlider];
    
    UILabel *cityLabel = [[UILabel alloc]initWithFrame:CGRectMake(accuracySlider.frame.origin.x - (100.0f / 2), accuracySlider.frame.origin.y + accuracySlider.frame.size.height, 100.0f, 20.0f)];
    [cityLabel setTextAlignment:NSTextAlignmentCenter];
    [cityLabel setText:@"Commune"];
    [cityLabel setFont:[UIFont systemFontOfSize:16]];
    [cityLabel setBackgroundColor:[UIColor clearColor]];
    [cityLabel setTextColor:[UIColor whiteColor]];
    [header addSubview:cityLabel];
    
    UILabel *departmentLabel = [[UILabel alloc]initWithFrame:CGRectMake(accuracySlider.frame.origin.x + (accuracySlider.frame.size.width - 100.0f)/2, accuracySlider.frame.origin.y + accuracySlider.frame.size.height, 100.0f, 20.0f)];
    [departmentLabel setTextAlignment:NSTextAlignmentCenter];
    [departmentLabel setText:@"Département"];
    [departmentLabel setFont:[UIFont systemFontOfSize:16]];
    [departmentLabel setBackgroundColor:[UIColor clearColor]];
    [departmentLabel setTextColor:[UIColor whiteColor]];
    [header addSubview:departmentLabel];
    
    UILabel *countryLabel = [[UILabel alloc]initWithFrame:CGRectMake(accuracySlider.frame.origin.x + accuracySlider.frame.size.width - (100.0f / 2), accuracySlider.frame.origin.y + accuracySlider.frame.size.height, 100.0f, 20.0f)];
    [countryLabel setTextAlignment:NSTextAlignmentCenter];
    [countryLabel setText:@"Pays"];
    [countryLabel setFont:[UIFont systemFontOfSize:16]];
    [countryLabel setBackgroundColor:[UIColor clearColor]];
    [countryLabel setTextColor:[UIColor whiteColor]];
    [header addSubview:countryLabel];
    
    return header;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    int nbRowInSection = 0;
    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];
    
    UIImageView *radioLogoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 5.0f, tableView.rowHeight - 10.0f, tableView.rowHeight - 10.0f)];
    [radioLogoImage setContentMode:UIViewContentModeScaleAspectFit];
    [radioLogoImage setClipsToBounds:YES];
    UIView *defaultView = [[UIView alloc] initWithFrame:radioLogoImage.frame];
    defaultView.backgroundColor = [UIColor colorWithRed:230.0f/255.0f green:230.0f/255.0f blue:230.0f/255.0f alpha:1.0f];
    [cell.contentView addSubview:defaultView];
    [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.row] objectForKey:@"thumb"] withMaxSize:(tableView.rowHeight - 10.0f)*2];
    } else if ([[iconDownloaderDictionary objectForKey:indexPath] appIcon] != nil) {
        [radioLogoImage setImage:[[iconDownloaderDictionary objectForKey:indexPath] appIcon]];
        [defaultView removeFromSuperview];
    }
    
    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 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 {
    
    [appDelegate setOldVC:self];
    [appDelegate.playerVC setNavigationTitle:@"Catégories"];
    [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
