//
//  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"
#import "SignUpTypeSelectViewController.h"

/**
 현재 Term,Service,All,Guide Cell을 사용 중이나 Guide Cell 에서 lblGuide는 Hidden 처리 중
 **/

@implementation AgreementTermTableViewCell

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

@end

@implementation AgreementServiceTableViewCell

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

@end

@implementation AgreementLocationTableViewCell

- (void)didMoveToSuperview{
    
}

@end

@implementation AgreementAllTableViewCell

- (void)didMoveToSuperview {

}

@end

@implementation AgreementGuideTableViewCell

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

@end

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


#pragma mark - Class Definition

@implementation AgreementViewController 


- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    [self prepareViewDidLoad];
}

// UI 설정
- (void)initUI {
    [self.navigationController.navigationBar setHidden:YES];

    _btnNext.enabled = NO;
    [_lblStep setColor:kUITextColor03 text:[NSString stringWithFormat:@"%@", @"/ 5"]];

    NSLog(@"signType : %@",[JDFacade facade].signType);
//    _tableZombieValues = @[@"t0", @"t1", @"t2", @"t3",@"t4"];
    _tableZombieValues = @[@"t0", @"t1", @"t3",@"t4"];
    [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;
//    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 114;
    
    if (indexPath.row == 3) {
        height = 120;
    }
//    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];
    
    // 서비스 이용약관 Cell
    if (indexPath.row == 0) {
        AgreementTermTableViewCell *tcell = (AgreementTermTableViewCell * )[tableView dequeueReusableCellWithIdentifier:@"TermCellIdentifier"];
        
        // CheckBox Check여부 확인
        if (!_chkTerm) {
            _chkTerm = tcell.chkAgree;
        }
        
        tcell.chkAgree.delegate = self;
        tcell.chkAgree.value = tzombie;
        tcell.chkAgree.checked = [tcell.chkAgree getCheckStatusFromValue];
        
        cell = tcell;
        
    // 개인정보 취급방침 Cell
    } 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;
        
    }
    
    // 위치기반 서비스 이용안내 Cell
//    else if (indexPath.row == 2) {
//        AgreementLocationTableViewCell *tcell = (AgreementLocationTableViewCell * )[tableView dequeueReusableCellWithIdentifier:@"locationCellIdentifier"];
//        
//        if (!_chkLocation) {
//            _chkLocation = tcell.chkAgree;
//        }
//        
//        tcell.chkAgree.delegate = self;
//        tcell.chkAgree.value = tzombie;
//        tcell.chkAgree.checked = [tcell.chkAgree getCheckStatusFromValue];
//
//        cell = tcell;
//    }
    // 전체 동의 Cell
    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;
    
    // Guide Cell
    }else if (indexPath.row == 3) {
        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:@"%@%@", kWebLinkServer, URL_PATH_TERMS];
        
    // 개인정보 취급방침 내용보기
    } else if (indexPath.row == 1) {
        URLString = [NSString stringWithFormat:@"%@%@", kWebLinkServer, URL_PATH_POLICY];
    }
    NSLog(@"URL String : %@", URLString);
    [[JDFacade facade] loadURLExternalBrowser:URLString];
}

// CheckBox Check여부에 따라 다음버튼 Enable여부 설정
#pragma mark - CustomCheckBox Delegate {
- (void)didCheckBoxClicked:(id)sender {
    
    if ([sender isEqual:_chkAll]) {
//        _chkTerm.checked = _chkService.checked = _chkLocation.checked =_chkAll.checked;
        _chkTerm.checked = _chkService.checked =_chkAll.checked;
        _btnNext.enabled = _chkAll.checked;

    } else {
        
        _chkAll.checked = _chkTerm.checked && _chkService.checked;
        _btnNext.enabled = _chkTerm.checked && _chkService.checked;
    }
}

// 휴대폰 번호 인증 메뉴로 이동
#pragma mark - UI Events
- (IBAction)btnNextTouched:(id)sender {
    
//    if (!_chkAll.checked) {
//        [[JDFacade facade] toast:@"모든 약관에 동의해주세요"];
//        return;
//    }
    
    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"CertificationViewController" storyboardName:@"SignUp"];
    
    [self.navigationController pushViewController:vc animated:YES];
}

// 메뉴 닫기
- (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
