//
//  SignUpInfoViewController.m
//  kneet
//
//  Created by Jason Lee on 4/23/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//

#import "SignUpInfoViewController.h"
#import "CustomLabel.h"
#import "JDObject.h"
#import "RequestHandler.h"
#import "JDJSONModel.h"
#import "CustomButton.h"


@interface SignUpInfoViewController () {

}
@end


#pragma mark - Class Definition

@implementation SignUpInfoViewController


- (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(@"가입", @"가입");
}

- (void)initUI {
    
    

    [_lblSendAgain setUnderLine:_lblSendAgain.text];
    [_lblSignUpAgain setUnderLine:_lblSignUpAgain.text];

    [_lblSendAgain addTouchEventHandler:^{
        [self lblSendAgainTouched];
    }];
    
//    [_lblSignUpAgain addTouchEventHandler:^{
//        [self lblSignUpAgainTouched];
//    }];
    
    //Localization
    _lblDesc1.text = NSLocalizedString(@"인증을 마치고 로그인하면\n바로 시작할 수 있습니다", @"인증을 마치고 로그인하면\n바로 시작할 수 있습니다");
    _lblDesc2.text = NSLocalizedString(@"이메일이 도착하지 않는다면 아래 해결 방법을 참고하세요", @"이메일이 도착하지 않는다면 아래 해결 방법을 참고하세요");
    
    [_btnLogin setTitle:NSLocalizedString(@"로그인", @"로그인") forState:UIControlStateNormal];
    
    _lblSubDesc1.text = NSLocalizedString(@"인증메일을\n다시 발송하세요", @"인증메일을\n다시 발송하세요");
    _lblSubDesc2.text = NSLocalizedString(@"스팸 메일함을\n확인해보세요", @"스팸 메일함을\n확인해보세요");
    _lblSubDesc3.text = NSLocalizedString(@"다른 이메일주소로\n가입하세요", @"다른 이메일주소로\n가입하세요");
    
    _lblSendAgain.text = NSLocalizedString(@"재발송", @"재발송");
    [_lblSendAgain setUnderLine:_lblSendAgain.text];
    
}

- (void)prepareViewDidLoad {

    if ([JDFacade facade].tmpEmailId) {
        _lblEmailInfo.text = [NSString stringWithFormat:NSLocalizedString(@"%@으로 인증 메일이 발송되었습니다.", @"%@으로 인증 메일이 발송되었습니다."), [JDFacade facade].tmpEmailId];
    }
}

#pragma mark - Main Logic
- (void)requestAuthMail {//인증 메일 재요청
    //parameters
    NSDictionary *parameter = @{@"email_id": [JDFacade facade].tmpEmailId};

    NSString *path = [NSString stringWithFormat:API_POST_AUTH_MAIL];

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

        JDJSONModel *result = (JDJSONModel *) responseObject;

        if (result) {//API 성공 ,
            [[JDFacade facade] toast:NSLocalizedString(@"인증 메일이 발송되었습니다", @"인증 메일이 발송되었습니다")];
        }
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}


#pragma mark - UI Events
- (void)lblSendAgainTouched {
    //이메일 인증 재요청.
    if (![JDFacade facade].tmpEmailId) {
        [[JDFacade facade] alert:NSLocalizedString(@"가입 정보가 없습니다", @"가입 정보가 없습니다")];
        return;
    }
    [self requestAuthMail];
}

- (void)lblSignUpAgainTouched {
    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"StartHomeViewController" storyboardName:@"SignUp"];
    [[JDFacade facade] presentViewControllerByPush:vc pvc:self.navigationController];
}

- (IBAction)btnLoginTouched:(id)sender {
    [[JDFacade facade] dismissViewControllerByPush:self];
}

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


@end
