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

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

#import "QRCodeViewController.h"
#import "SecureChangeViewController.h"
#import "CustomButton.h"

@interface SecureChangeViewController () <CustomTextFieldDelegate> {

}
@end


#pragma mark - Class Definition

@implementation SecureChangeViewController


- (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.delegate = self;
    _txtSecureCode.returnKeyType = UIReturnKeyDone;
    _txtSecureCode.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    
    _txtSecureCode.placeholder = NSLocalizedString(@"보안키 입력", @"보안키 입력");
    
    _lblDesc.text = NSLocalizedString(@"이 장치를 사용 중인 다른 홈이 있다면\n더 이상 사용할 수 없게 됩니다", @"이 장치를 사용 중인 다른 홈이 있다면\n더 이상 사용할 수 없게 됩니다");
    
    [_btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
    [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {
    NSString *actitle = _qrcodeType == QRCodeTypeCommaxWallpad ? NSLocalizedString(@"코맥스 월패드", @"코맥스 월패드"): NSLocalizedString(@"다원 스마트 플러그", @"다원 스마트 플러그");

    _lblTitle.text = [NSString stringWithFormat:NSLocalizedString(@"[%@]에 [%@] 장치를 등록하려면 장치등록 보안키를 입력하세요", @"[%@]에 [%@] 장치를 등록하려면 장치등록 보안키를 입력하세요"), [JDFacade facade].loginHomeGroup.homegrpName, actitle];
}

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

    //parameters
    NSDictionary *parameter = @{@"device_id": _deviceId,
                                @"user_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
- (void)btnBackTouched:(id)sender {
    [self btnCancelTouched:nil];
}

- (IBAction)btnConfirmTouched:(id)sender {
    [self.view endEditing:YES];
    //1.validate

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

    //보안키 확인
    [self requestSecureKeyConfirm];
}

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

#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
