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

#import "UserNameSetViewController.h"
#import "CustomButton.h"
#import "CustomTextField.h"
#import "RequestHandler.h"
#import "JoinFinishViewController.h"
#import "ValidateUtil.h"

@interface UserNameSetViewController () <CustomTextFieldDelegate> {
    
}

@end

@implementation UserNameSetViewController
@synthesize signUpModel;

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

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

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

    [_lblStep setColor:kUITextColor03 text:[NSString stringWithFormat:@"%@", @"/ 5"]];
    
    _txtNickInput.delegate = self;
    _txtNickInput.keyboardType = UIKeyboardTypeDefault;
    _txtNickInput.returnKeyType = UIReturnKeyDone;
    
}

// 닉네임 변경 요청 및 회원가입 완료 메뉴로 이동
// 닉네임이 공백일 경우 변경 없이 회원가입 완료 메뉴로 이동
- (void)requestChangeNickname {
    //parameters
    NSDictionary *parameter = @{@"type": @"nickname" ,
                                @"nickname": _txtNickInput.text};

    NSString *path = [[JDFacade facade]getUrlWithCustAndGroupIDAndInputMemId:API_PUT_MEMBER_UPDATE inputId:signUpModel.memberId];
    
    [[RequestHandler handler] sendAsyncPutRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {

        JoinFinishViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"JoinFinishViewController" storyboardName:@"SignUp"];
        
        vc.signUpModel = signUpModel;
        
        [self.navigationController pushViewController:vc animated:YES];
        
    } failure:^(id errorObject) {
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}

// 다음 버튼 터치시 사용자가 입력한 닉네임 변경
- (IBAction)btnNextTouched:(id)sender {
//
//    if (_txtNickInput.text.trim.length != 0 && ![ValidateUtil validateTextfiled:_txtNickInput
//                                    type:ValidateTypeNickname
//                                   title:NSLocalizedString(@"이름", @"이름")])
//        return;
    
    [self requestChangeNickname];
}

// 나중에 버튼 터치시 닉네임 변경 없이 회원가입 완료 메뉴로 이동
- (IBAction)btnAfterTouched:(id)sender {
    JoinFinishViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"JoinFinishViewController" storyboardName:@"SignUp"];
    
    vc.signUpModel = signUpModel;
    [self.navigationController pushViewController:vc animated:YES];
}

// TextField Done 버튼 설정
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end
