//
//  ChangeNameViewController.m
//  kneet
//
//  Created by Jason Lee on 5/13/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

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


@interface ChangeNameViewController () <CustomTextFieldDelegate> {

}
@end


#pragma mark - Class Definition

@implementation ChangeNameViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {

    self.title = NSLocalizedString(@"이름 변경",nil);

    
    

    _txtUserName.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    _txtUserName.delegate = self;
    _txtUserName.keyboardType = UIKeyboardTypeDefault;
    _txtUserName.returnKeyType = UIReturnKeyDone;

    _txtUserName.text = [JDFacade facade].loginUser.nickname;
    
    //Localization
    
    _txtUserName.placeholder = NSLocalizedString(@"이름 입력", @"이름 입력");
    _lblTitle.text = NSLocalizedString(@"바꿀 이름을 입력하세요", @"바꿀 이름을 입력하세요");
    [_btnConfirm setTitle:NSLocalizedString(@"변경", @"변경") forState:UIControlStateNormal];
    [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {

}

#pragma mark - Main Logic
- (void)requestChangeNickname {
    //parameters
    NSDictionary *parameter = @{@"nickname": _txtUserName.text};

    NSString *path = [NSString stringWithFormat:API_POST_MEMBER_UPDATE, [JDFacade facade].loginUser.memberId, @"name"];

    [[RequestHandler handler] sendAsyncPostRequestAPIPath: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 = _txtUserName.text;

            NSInteger count = self.navigationController.viewControllers.count;
            UIViewController *vc = self.navigationController.viewControllers[count-2];
            [self.navigationController popToViewController:vc animated:YES];
        }
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}


#pragma mark - UI Events
- (IBAction)btnConfirmTouched:(id)sender {
    //validate
    if (![ValidateUtil validateTextfiled:_txtUserName type:ValidateTypeNull title:NSLocalizedString(@"이름", @"이름")]) {
        return;
    }

    [self requestChangeNickname];
}

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

#pragma mark - CustomTextField

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self btnConfirmTouched:nil];

    return YES;
}

#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
