//
//  PwdPopupView.m
//  kneet2
//
//  Created by Jason Lee on 10/12/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//

#import "JDObject.h"

#import "RequestHandler.h"
#import "JDJSONModel.h"

#import "CustomLabel.h"
#import "CustomTextField.h"

#import "ValidateUtil.h"
#import "PwdPopupView.h"

@interface PwdPopupView () <CustomTextFieldDelegate>
{

}

@end

@implementation PwdPopupView

- (id)initFromNib {

    for (UIView *view in [CommonUtil nibViews:@"PwdPopupView"]) {
        if ([view isKindOfClass:[PwdPopupView class]]) {
            self = (PwdPopupView *)view;

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            self.lblTitle.text = NSLocalizedString(@"비밀번호 변경",nil);


            _txtPwd.secureTextEntry = YES;
            _txtPwd.keyboardType = UIKeyboardTypeDefault;
            _txtPwd.returnKeyType = UIReturnKeyDone;

            _txtPwdConfirm.delegate = self;
            _txtPwdConfirm.secureTextEntry = YES;
            _txtPwdConfirm.keyboardType = UIKeyboardTypeDefault;
            _txtPwdConfirm.returnKeyType = UIReturnKeyDone;

            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"취소") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {
    _txtPwd.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
    _txtPwdConfirm.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
}

- (void)requestSetNewPassword {

    //parameters
    NSDictionary *parameter = @{@"new_password": _txtPwd.text,
                                @"confirm_password": _txtPwdConfirm.text};

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

    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
            return;
        }

        JDJSONModel *result = (JDJSONModel *) responseObject;

        if (result) {//API 성공 ,
            [JDFacade facade].tmpPassword = nil;
            [JDFacade facade].loginUser.tempPasswordYn = @"N";

            [[JDFacade facade] toast:@"비밀번호를 변경했습니다"];
            [super btnConfirmTouched:nil]; //closePopup
        }
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}

- (void)btnConfirmTouched:(id)sender {
    if (![ValidateUtil validateTextfiled:_txtPwd type:ValidateTypePassword title:NSLocalizedString(@"비밀번호", @"비밀번호")]) {
        return;
    }

    if (![_txtPwd.text isEqualToString:_txtPwdConfirm.text]) {
        [[JDFacade facade] alert:@"두 비밀번호가 일치하지 않습니다"];
        return;
    }

    [self requestSetNewPassword];
}

#pragma mark - CustomTextField

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

    return YES;
}


@end
