//
//  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.delegate = self;
            _txtPwd.secureTextEntry = YES;
            _txtPwd.keyboardType = UIKeyboardTypeDefault;
            _txtPwd.returnKeyType = UIReturnKeyNext;

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

            [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            //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 = @{@"password1": _txtPwd.text,
//                                @"password2": _txtPwdConfirm.text};
       NSDictionary *parameter = @{@"type": @"password",
                                   @"password": _txtPwdConfirm.text};
    
    NSString *path = [[JDFacade facade] getUrlWithCustGroupIDAndMemberID:API_PUT_MEMBER_UPDATE];
//    NSString *path = [NSString stringWithFormat:API_PUT_MEMBER_UPDATE, @"password"];

    [[RequestHandler handler] sendAsyncPutRequestAPIPath: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];
    }
    if ([textField isEqual:_txtPwd]) {
        [_txtPwdConfirm becomeFirstResponder];
    }

    return YES;
}



@end
