//
//  RankingViewController.m
//  LifeLog
//
//  Created by Nguyen Van Phong on 7/25/17.
//  Copyright © 2017 PhongNV. All rights reserved.
//

#import "RankingViewController.h"
#import "RankingTableViewCell.h"

#import "ServerAPI.h"
#import "Utilities.h"

@interface RankingViewController ()

@end

@implementation RankingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.viewCollectionType attribute:NSLayoutAttributeBottom multiplier:1 constant:5]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.viewHeader attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    // Do any additional setup after loading the view from its nib.
//    self.title = NSLocalizedString(@"lifelog.rank.title", nil);
    
    [self setupView];
    
    _startDate = [NSDate date];
    _endDate   = _startDate;
    self.lblDateTime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"];

    //register nib for table view
    [self.tableBase registerNib:[UINib nibWithNibName:@"RankingTableViewCell" bundle:nil] forCellReuseIdentifier:@"RankCell"];
    
    [self callRequestToUpdateData];
}

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

#pragma mark Setup and Update View

- (void)setupView {
    NSArray *timeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.rank.time.1", nil), NSLocalizedString(@"lifelog.rank.time.2", nil), NSLocalizedString(@"lifelog.rank.time.3", nil), NSLocalizedString(@"lifelog.rank.time.4", nil), nil];
    [self.viewCollectionTime setButtonNumber:timeTitle.count];
    [self.viewCollectionTime setSpacing:1];
    [self.viewCollectionTime setArrayTitle:timeTitle];
    self.viewCollectionTime.changeCurrentIndex = ^(int index){
        [self changeDate];
    };
    
    NSArray *modeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.rank.mode.1", nil), NSLocalizedString(@"lifelog.rank.mode.2", nil), NSLocalizedString(@"lifelog.rank.mode.3", nil), NSLocalizedString(@"lifelog.rank.mode.4", nil), nil];
    [self.viewCollectionMode setButtonNumber:modeTitle.count];
    [self.viewCollectionMode setSpacing:1];
    [self.viewCollectionMode setArrayTitle:modeTitle];
    self.viewCollectionMode.changeCurrentIndex = ^(int index){
        [self callRequestToUpdateData];
    };
    
    NSArray *typeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.rank.type.1", nil), NSLocalizedString(@"lifelog.rank.type.2", nil), NSLocalizedString(@"lifelog.rank.type.3", nil), NSLocalizedString(@"lifelog.rank.type.4", nil), nil];
    [self.viewCollectionType setButtonNumber:typeTitle.count];
    [self.viewCollectionType setSpacing:1];
    [self.viewCollectionType setArrayTitle:typeTitle];
    self.viewCollectionMode.changeCurrentIndex = ^(int index){
        [self callRequestToUpdateData];
    };
}


#pragma mark IBAction function
- (IBAction)clickBackward:(UIButton *)sender {
    _endDate = [_endDate dateByAddingTimeInterval:-86400];
    [self changeDate];
}

- (IBAction)clickForward:(UIButton *)sender {
    _endDate = [_endDate dateByAddingTimeInterval:86400];
    [self changeDate];
}

#pragma mark UITableView Delegate

-  (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RankingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RankCell"];
    if(indexPath.row == 0) {
        cell.imgRank.image = [UIImage imageNamed:@"ic_rank_1"];
    } else if(indexPath.row == 1) {
        cell.imgRank.image = [UIImage imageNamed:@"ic_rank_2"];
    } else if(indexPath.row == 2) {
        cell.imgRank.image = [UIImage imageNamed:@"ic_rank_3"];
    } else {
        cell.imgRank.image = NULL;
    }
    [cell setRankingData:[_curDataList objectAtIndex:indexPath.row]];
    return cell;
}

#pragma mark Private function
- (void)callRequestToUpdateData {
    [super callRequestToUpdateData];
    
    NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
    MBProgressHUD *hudView = nil;
    int mode = self.viewCollectionMode.getCurrentIndex + 1;
    //dont have all
    mode = fmin(mode, 3);
    
    if(_curPage == 1 && !self.refreshControl.isRefreshing) {
        hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    }
    [[ServerAPI server] requestRankingList:token startDate:_startDate endDate:_endDate mode:mode page:_curPage CompletionHandler:^(NSArray *array, NSError *error){
        dispatch_async(dispatch_get_main_queue(), ^{
            if(hudView != nil) {
                [hudView hideAnimated:true];
            }
        });
        RankingViewController __weak *weakSelf = self;
        [weakSelf updateTableData:array error:error];
    }];
}

- (void) changeDate {
    switch (self.viewCollectionTime.getCurrentIndex) {
        case 1:
            _startDate = [_endDate dateByAddingTimeInterval:-86400 * 7];
            break;
        case 2:
            _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30];
            break;
        case 3:
            _startDate = [_endDate dateByAddingTimeInterval:-86400 * 365];
            break;
        default:
            _startDate = _endDate;
            break;
    }
    if(_startDate == _endDate) {
        self.lblDateTime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"];
    }
    else {
        NSString * startDateString = [Utilities stringFromDate:_startDate withFormat:@"YYYY年MM月dd日" locale:@"ja_JP"];
        NSString * endDateString = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日" locale:@"ja_JP"];
        self.lblDateTime.text = [NSString stringWithFormat:@"%@-%@", startDateString, endDateString];
    }
    [self callRequestToUpdateData];
}
@end
