//
//  ResetPwdViewController.m
//  kneet
//
//  Created by Jason Lee on 4/23/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "CustomTextField.h"

#import "ResetPwdViewController.h"
#import "ValidateUtil.h"
#import "RequestHandler.h"
#import "JDJSONModel.h"
#import "CustomLabel.h"
#import "CustomButton.h"


@interface ResetPwdViewController () <CustomTextFieldDelegate> {

}
@end


#pragma mark - Class Definition

@implementation ResetPwdViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.title = NSLocalizedString(@"비밀번호 재설정",nil);
}

- (void)initUI {
    
    

    _txtUserEmail.delegate = self;
    _txtUserEmail.keyboardType = UIKeyboardTypeEmailAddress;
    _txtUserEmail.returnKeyType = UIReturnKeyDone;
    _txtUserEmail.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    
    //Localization
    _txtUserEmail.placeholder = NSLocalizedString(@"이메일", @"이메일");
    
    _lblTitle.text = NSLocalizedString(@"비밀번호가 기억나지 않으세요?", @"비밀번호가 기억나지 않으세요?");
    _lblDesc.text = NSLocalizedString(@"이메일 인증을 통해서 임시 비밀번호를\n재설정 할 수 있습니다", @"이메일 인증을 통해서 임시 비밀번호를\n재설정 할 수 있습니다");
    [_btnRequestEmail setTitle:NSLocalizedString(@"인증메일 받기", @"인증메일 받기") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {

}


#pragma mark - Main Logic
- (void)requestResetPassword {

    //parameters
    NSDictionary *parameter = @{@"email_id": _txtUserEmail.text};

    NSString *path = [NSString stringWithFormat:API_POST_RESET_PWD];

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

        JDJSONModel *result = (JDJSONModel *) responseObject;

        if (result) {//API 성공 ,
            NSString *msg = NSLocalizedString(@"인증 메일을 확인 후\n임시 비밀번호로 로그인하세요", @"인증 메일을 확인 후\n임시 비밀번호로 로그인하세요");
            [[JDFacade facade] alert:msg completionHander:^{
                [[JDFacade facade] dismissViewControllerByPush:self];
            }];
        }
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}


#pragma mark - UI Events
- (IBAction)btnRequestEmailTouched:(id)sender {
    if (![ValidateUtil validateTextfiled:_txtUserEmail type:ValidateTypeEmail title:NSLocalizedString(@"이메일", @"이메일")]) {
        return;
    }

    //비밀번호 재설정 요청
    [self requestResetPassword];
}


#pragma mark - CustomTextField

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

    return YES;
}


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


@end
