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

#import "SettingsPasswdSetViewController.h"
#import "CustomTextField.h"
#import "CustomButton.h"
#import "CustomLabel.h"
#import "RequestHandler.h"
#import "ValidateUtil.h"

@interface SettingsPasswdSetViewController () <CustomTextFieldDelegate>

@end

@implementation SettingsPasswdSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
}

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

#pragma mark - User Event
- (IBAction)btnCancelTouched:(id)sender {
    [[self navigationController] popToRootViewControllerAnimated:YES];
}

- (IBAction)btnSaveTouched:(id)sender {
 
    if (![ValidateUtil validateTextfiled:_txtInputPasswd type:ValidateTypePassword title:NSLocalizedString(@"비밀번호", @"비밀번호")]) {
        return;
    }
    
    if (![_txtInputPasswd.text isEqualToString:_txtInputRePasswd.text]) {
        [[JDFacade facade] alert:NSLocalizedString(@"재입력된 비밀번호가 다릅니다.", @"재입력된 비밀번호가 다릅니다.")];
        return;
    }
    
    [self requestChangePass];
}


#pragma mark - text field delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if ([textField isEqual:_txtInputRePasswd]) {
        
        [textField resignFirstResponder];
    }
    else {
        
        [_txtInputRePasswd becomeFirstResponder];
    }
    
    return YES;
    
}

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}


//비번 바꾸기
- (void)requestChangePass {
    
    //parameters
    NSDictionary *parameter = @{@"type": @"password" ,
                                @"password": _txtInputPasswd.text.trim};
    
    NSString *path = [[JDFacade facade] getUrlWithCustGroupIDAndMemberID:API_PUT_MEMBER_UPDATE];
    
    //NSLog(@"path : %@ ", path);
    [[RequestHandler handler] sendAsyncPutRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
        
        [[self navigationController] popToRootViewControllerAnimated:YES];
        
        
    } failure:^(id errorObject) {
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}

@end
