//
//  IdFindViewController.m
//  OneCable
//
//  Created by nComz on 2017. 3. 31..
//  Copyright © 2017년 ntels. All rights reserved.
//

#import "IdFindViewController.h"
#import "CustomButton.h"
#import "CustomLabel.h"
#import "CustomTextField.h"
#import "IdFindCompleteViewController.h"
#import "RequestHandler.h"

@interface IdFindViewController ()<UITextFieldDelegate>

@end

@implementation IdFindViewController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)initUI {
    
    [self.navigationController.navigationBar setHidden:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    [_lblStep setColor:kUITextColor03 text:[NSString stringWithFormat:@"%@", @"/ 2"]];
    
    _btnNext.enabled = NO ;
}



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

- (IBAction)btnNextTouched:(id)sender {
    
    [self requestFindMemberID] ;
    
}

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

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    _btnNext.enabled = _txtIdInput.text.length >= 4 && _txtNumInput.text.length >= 10 ;
    
}


//멤버 아이디 찾기
- (void)requestFindMemberID {
    
    NSString *path = [NSString stringWithFormat:API_POST_ID_FIND, _txtIdInput.text.trim];
    NSDictionary *parameter = @{@"phone": _txtNumInput.text.trim};
    
    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[FindIdPwModel class] completion:^(id responseObject) {
        
        FindIdPwModel *findIDModel = (FindIdPwModel *)responseObject;
        
        IdFindCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"IdFindCompleteViewController" storyboardName:@"SignUp"];
        vc.memberId = findIDModel.memberId;
        vc.nickName = findIDModel.nickname;
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
    } failure:^(id errorObject) {
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
        
        
    }];
    
}

@end
