//
//  ConfirmPasswdPopupView.m
//  OneCable
//
//  Created by Jason Lee on 01/14/15.
//  Copyright (c) 2016 ntels. All rights reserved.
//
#import "JDObject.h"

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

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

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

@interface ConfirmPasswdPopupView () <CustomTextFieldDelegate>
{

}

@end

@implementation ConfirmPasswdPopupView

- (id)initFromNib {

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

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            
            _txtPasswd.keyboardType = UIKeyboardTypeDefault;
            _txtPasswd.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 {
    if (!self.superview) {
        return;
    }
    _txtPasswd.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
}

#pragma mark - Main Logic
- (void)requestCheckUserPasswd {
    //parameters
    NSDictionary *parameter = @{@"password": _txtPasswd.text};
    
    NSString *path = [NSString stringWithFormat:API_GET_CHECK_PWD, [JDFacade facade].loginUser.memberId];
    
    [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
        
        [self loadPopup];
        
    } failure:^(id errorObject) {
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}

- (void)loadPopup {
    
    PwdPopupView *popup = [[PwdPopupView alloc] initFromNib];
    [popup show];
    
    [super btnConfirmTouched:nil];
}

#pragma mark - UI Events
- (IBAction)btnConfirmTouched:(id)sender {
    //validate
    if (![ValidateUtil validateTextfiled:_txtPasswd type:ValidateTypeNull title:@"비밀번호"]) {
        return;
    }
    
    [self requestCheckUserPasswd];
}


#pragma mark - CustomTextField

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

    return YES;
}


@end
