//
//  SettingsPasswdInputViewController.m
//  OneCable
//
//  Created by nComz on 2017. 4. 26..
//  Copyright © 2017년 ntels. All rights reserved.
//

#import "SettingsPasswdInputViewController.h"
#import "CustomTextField.h"
#import "RequestHandler.h"

@interface SettingsPasswdInputViewController ()<CustomTextFieldDelegate>

@end

@implementation SettingsPasswdInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];

}

- (void)initUI {
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    statusBar.backgroundColor = [UIColor whiteColor];
    
    _txtInputPasswd.returnKeyType = UIReturnKeyDone;
    _txtInputPasswd.delegate = self;
    _btnNext.enabled = NO;
    
    [self.navigationController.navigationBar setHidden:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}


#pragma mark - user event
- (IBAction)btnNextTouched:(id)sender {
    
    [self requestExistPWD];
}

- (IBAction)btnCancelTouched:(id)sender {
    [[self navigationController] popToRootViewControllerAnimated:YES];
}


#pragma mark - textfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([textField isEqual:_txtInputPasswd]) {
        [textField resignFirstResponder];
    }
    return YES;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    _btnNext.enabled = text.length >= 8;
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


//비번 맞는지 확인
- (void)requestExistPWD {
    //parameters
    NSDictionary *parameter = @{@"password": _txtInputPasswd.text.trim};
    
    NSString *path = [[JDFacade facade] getUrlWithCustGroupIDAndMemberID:API_POST_EXIST_PWD];
    
    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
        
        UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"SettingsPasswdSetViewController" storyboardName:@"Settings"];
        
        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(id errorObject) {
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
        
    }];
}



@end
