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

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

#import "ValidateUtil.h"
#import "CustomTextField.h"

#import "PwdPopupView.h"
#import "ConfirmPwdViewController.h"
#import "DeleteAccountPopupView.h"

@interface ConfirmPwdViewController () <CustomTextFieldDelegate> {

}
@end


#pragma mark - Class Definition

@implementation ConfirmPwdViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    self.title = NSLocalizedString(@"비밀번호 확인",nil);

    
    

    _txtUserPwd.delegate = self;
    _txtUserPwd.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    _txtUserPwd.secureTextEntry = YES;

    _txtUserPwd.keyboardType = UIKeyboardTypeDefault;
    _txtUserPwd.returnKeyType = UIReturnKeyDone;
    
    _txtUserPwd.placeholder = NSLocalizedString(@"비밀번호 입력", @"비밀번호 입력");
    _lblTitle.text = @"본인확인을 위해 비밀번호를\n입력해주세요";
    [_btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
    [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
}
- (void)prepareViewDidLoad {

}

#pragma mark - Main Logic
- (void)requestCheckPassword {
    //parameters
    NSDictionary *parameter = @{@"password": _txtUserPwd.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) {
        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
            return;
        }

        JDJSONModel *result = (JDJSONModel *) responseObject;

        if (result) {//API 성공 ,
            [self loadPopupView];
        }
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];

}

- (void)loadPopupView {
    CustomAlertView *popup = [[NSClassFromString(_popupName) alloc] initFromNib];

    if ([popup isKindOfClass:[PwdPopupView class]]) {

    } else if ([popup isKindOfClass:[DeleteAccountPopupView class]]) {
        ((DeleteAccountPopupView *)popup).passwd = _txtUserPwd.text;
    }

    [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

#pragma mark - UI Events
- (IBAction)btnConfirmTouched:(id)sender {

#ifdef DEBUG_MODE
    if ([_txtUserPwd.text isEmptyString]) {
        _txtUserPwd.text = @"goj132!#@";
    }
#else
    //validate
    if (![ValidateUtil validateTextfiled:_txtUserPwd type:ValidateTypeNull title:NSLocalizedString(@"비밀번호", @"비밀번호")]) {
        return;
    }
#endif

    [self requestCheckPassword];
}

- (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
