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

#import "SettingsNameSetViewController.h"
#import "CustomLabel.h"
#import "CustomButton.h"
#import "CustomTextField.h"
#import "JDObject.h"
#import "RequestHandler.h"
#import "JDJSONModel.h"
#import "ValidateUtil.h"

@interface SettingsNameSetViewController () <CustomTextFieldDelegate>

@end

@implementation SettingsNameSetViewController

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

}

- (void)initUI {
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    statusBar.backgroundColor = [UIColor whiteColor];
    
    _txtInputName.placeholder = [[JDFacade facade].loginUser.nickname isEqualToString:@""]? @"" :[JDFacade facade].loginUser.nickname;
    
    _txtInputName.returnKeyType = UIReturnKeyDone;
     _txtInputName.delegate = self;
    
    [self.navigationController.navigationBar setHidden:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

- (void)requestChangeNickname {
    //parameters
    NSDictionary *parameter = @{@"type": @"nickname" ,
                                @"nickname": _txtInputName.text};
    
    //    NSString *path = [NSString stringWithFormat:API_PUT_MEMBER_UPDATE, [JDFacade facade].loginUser.memberId, @"name"];
    
    NSString *path = [[JDFacade facade] getUrlWithCustGroupIDAndMemberID:API_PUT_MEMBER_UPDATE];
    
    [[RequestHandler handler] sendAsyncPutRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
//        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
//            return;
//        }
        
//        JDJSONModel *result = (JDJSONModel *) responseObject;
        
//        if (result) {//API 성공 ,
//            [[JDFacade facade] toast:NSLocalizedString(@"이름이 변경되었습니다", nil)];

            [JDFacade facade].loginUser.nickname = _txtInputName.text;

            [[self navigationController] popToRootViewControllerAnimated:YES];
//            [super btnConfirmTouched:nil];
//        }
    } failure:^(id errorObject) {
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}



- (IBAction)btnSaveTouched:(id)sender {

//    if (_txtInputName.text.trim.length != 0 && ![ValidateUtil validateTextfiled:_txtInputName
//                                                                           type:ValidateTypeNickname
//                                                                          title:NSLocalizedString(@"이름", @"이름")])
//        return;

    
    [self requestChangeNickname];
}

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

    [[self navigationController] popToRootViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([textField isEqual:_txtInputName]) {
        [textField resignFirstResponder];
    }
    return YES;
    
}

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