//
//  SecureThingsViewController.m
//  kneet
//
//  Created by Jason Lee on 5/18/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//

#import "SecureThingsViewController.h"
#import "ValidateUtil.h"
#import "CustomTextField.h"
#import "RequestHandler.h"
#import "CustomLabel.h"
#import "CustomButton.h"


@interface SecureThingsViewController () <CustomTextFieldDelegate> {

}
@end


#pragma mark - Class Definition

@implementation SecureThingsViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    self.title = NSLocalizedString(@"장치등록 보안키 설정",nil);

    
    

    _txtSecureCode.secureTextEntry = YES;
    _txtSecureCode.returnKeyType = UIReturnKeyDone;

    _txtSecureCodeConfirm.delegate = self;
    _txtSecureCodeConfirm.secureTextEntry = YES;
    _txtSecureCodeConfirm.returnKeyType = UIReturnKeyDone;

    _txtSecureCode.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    _txtSecureCodeConfirm.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    
    _txtSecureCode.placeholder = NSLocalizedString(@"보안키 입력", @"보안키 입력");
    _txtSecureCodeConfirm.placeholder = NSLocalizedString(@"보안키 재확인", @"보안키 재확인");
    
    //Localization
    _lblTitle.text = NSLocalizedString(@"장치 등록을 성공했습니다", @"장치 등록을 성공했습니다");
    _lblDesc.text = NSLocalizedString(@"장치 등록 보안키를 설정해보세요\n다른 사람이 이 장치를 함부로\n등록할 수 없게 하세요", @"장치 등록 보안키를 설정해보세요\n다른 사람이 이 장치를 함부로\n등록할 수 없게 하세요");
    
    [_btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
    [_btnNext setTitle:NSLocalizedString(@"다음에", @"다음에") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {

}

#pragma mark - Main Logic
- (void)requestRegisterSecureCode {
    //parameters
    NSDictionary *parameter = @{@"device_id": _deviceId,
                                @"change_device_password": _txtSecureCode.text};

    NSString *path = [NSString stringWithFormat:API_POST_PARTNER_PASSWD];

    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {

        UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
        [[JDFacade facade] presentViewControllerByPush:vc pvc:self];

    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}


#pragma mark - UI Events


- (IBAction)btnConfirmTouched:(id)sender {
    //1.validate
    if (![ValidateUtil validateTextfiled:_txtSecureCode type:ValidateTypeNull title:NSLocalizedString(@"보안키", @"보안키")]) {
        return;
    }

    if (![ValidateUtil validateTextfiled:_txtSecureCodeConfirm type:ValidateTypeNull title:NSLocalizedString(@"보안키 확인", @"보안키 확인")]) {
        return;
    }

    if (![_txtSecureCode.text isEqualToString:_txtSecureCodeConfirm.text]) {
        [[JDFacade facade] alert:NSLocalizedString(@"보안키와 보안키확인이 다릅니다\n다시 입력해주세요", @"보안키와 보안키확인이 다릅니다\n다시 입력해주세요")];
        return;
    }

    [self requestRegisterSecureCode];
}

- (IBAction)btnNextTouched:(id)sender {

    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
    [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
}

#pragma mark - CustomTextField

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

    return YES;
}


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

@end
