//
//  AgreementViewController.m
//  kneet2
//
//  Created by Jason Lee on 10/15/15.
//  Copyright © 2015 ntels. All rights reserved.
//

#import "AgreementViewController.h"
#import "CustomLabel.h"
#import "CustomCheckBox.h"
#import "CustomTableView.h"
#import "WebBrowseViewController.h"

@implementation AgreementTermTableViewCell

- (void)didMoveToSuperview {
    [_lblTitle setUnderLine:_lblTitle.text];
}

@end

@implementation AgreementServiceTableViewCell

- (void)didMoveToSuperview {
    [_lblTitle setUnderLine:_lblTitle.text];
}

@end

@implementation AgreementAllTableViewCell

- (void)didMoveToSuperview {

}

@end

@implementation AgreementGuideTableViewCell

- (void)didMoveToSuperview {
//    [_lblGuide setUnderLine:_lblGuide.text];
}

@end

@interface AgreementViewController () <CustomCheckBoxDelegate> {
    NSArray *_tableZombieValues;
    CustomCheckBox *_chkTerm, *_chkService, *_chkAll;
    BOOL _isNotFirstLoading;
}
@end


#pragma mark - Class Definition

@implementation AgreementViewController


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


- (void)initUI {
    
    _tableZombieValues = @[@"t0", @"t1", @"t2", @"t3"];
    
    [self initTableViewAsDefaultStyle:_tableView];
}

- (void)prepareViewDidLoad {
    
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

#pragma mark - Main Logic

#pragma mark - UITableView DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 65;
    
    if (indexPath.row == 2) {
        height = 80;
    } else if (indexPath.row == 3) {
        height = 120;
    }
    
    return height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;

    NSString *tzombie = _tableZombieValues[indexPath.row];
    
    if (indexPath.row == 0) {
        AgreementTermTableViewCell *tcell = (AgreementTermTableViewCell * )[tableView dequeueReusableCellWithIdentifier:@"TermCellIdentifier"];
        
        if (!_chkTerm) {
            _chkTerm = tcell.chkAgree;
        }
        
        tcell.chkAgree.delegate = self;
        tcell.chkAgree.value = tzombie;
        tcell.chkAgree.checked = [tcell.chkAgree getCheckStatusFromValue];
        
        cell = tcell;
    } else if (indexPath.row == 1) {
        AgreementServiceTableViewCell *tcell = (AgreementServiceTableViewCell * )[tableView dequeueReusableCellWithIdentifier:@"ServiceCellIdentifier"];
        
        if (!_chkService) {
            _chkService = tcell.chkAgree;
        }
        
        tcell.chkAgree.delegate = self;
        tcell.chkAgree.value = tzombie;
        tcell.chkAgree.checked = [tcell.chkAgree getCheckStatusFromValue];
        
        cell = tcell;
    } else if (indexPath.row == 2) {
        AgreementAllTableViewCell *tcell = (AgreementAllTableViewCell * )[tableView dequeueReusableCellWithIdentifier:@"AllCellIdentifier"];
        
        if (!_chkAll) {
            _chkAll = tcell.chkAgree;
        }
        
        tcell.chkAgree.delegate = self;
        tcell.chkAgree.value = tzombie;
        tcell.chkAgree.checked = [tcell.chkAgree getCheckStatusFromValue];
        
        cell = tcell;
    } else if (indexPath.row == 3) {//Guide
        AgreementGuideTableViewCell *tcell = (AgreementGuideTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"GuideCellIdentifier"];
        cell = tcell;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    if (indexPath.row == 2 || indexPath.row == 3) {
        return;
    }
    
    NSString *URLString = nil;
    if (indexPath.row == 0) {
        URLString = [NSString stringWithFormat:@"%@%@", kAPIServer, URL_PATH_TERMS];
        
    } else if (indexPath.row == 1) {
        URLString = [NSString stringWithFormat:@"%@%@", kAPIServer, URL_PATH_POLICY];
    }
    
    [[JDFacade facade] loadURLExternalBrowser:URLString];
}

#pragma mark - CustomCheckBox Delegate {
- (void)didCheckBoxClicked:(id)sender {
    
    if ([sender isEqual:_chkAll]) {
        _chkTerm.checked = _chkService.checked = _chkAll.checked;
    } else {
        if (_chkTerm.checked && _chkService.checked) {
            _chkAll.checked = YES;
        } else {
            _chkAll.checked = NO;
        }
    }
}

#pragma mark - UI Events
- (IBAction)btnNextTouched:(id)sender {
    
    if (!_chkAll.checked) {
        [[JDFacade facade] toast:@"모든 약관에 동의해주세요"];
        return;
    }
    
    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SignUpViewController" storyboardName:@"SignUp"];
    
    vc.providesPresentationContextTransitionStyle = YES;
    vc.definesPresentationContext = YES;
    
    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)btnCancelTouched:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - MemoryWarning

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    for (NSString *zombie in _tableZombieValues) {
        [[JDFacade facade] setCheckBoxStatus:@NO object:zombie];
    }
    
    _tableZombieValues = nil;
}

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


@end