//
//  ModifySecureKeyPopupView.m
//  kneet
//
//  Created by Jason Lee on 6/23/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "RequestHandler.h"
#import "JDJSONModel.h"

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

#import "DeviceModel.h"
#import "ModifySecureKeyPopupView.h"
#import "ValidateUtil.h"

@interface ModifySecureKeyPopupView () <CustomTextFieldDelegate> {

}

@end

@implementation ModifySecureKeyPopupView

- (id)initFromNib {

    for (UIView *view in [CommonUtil nibViews:@"ModifySecureKeyPopupView"]) {
        if ([view isKindOfClass:[ModifySecureKeyPopupView class]]) {
            self = (ModifySecureKeyPopupView *)view;
            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;

            self.lblTitle.text = NSLocalizedString(@"장치등록 보안키 변경", @"장치등록 보안키 변경");

            _txtSecureCode.delegate = self;
            _txtSecureCode.secureTextEntry = YES;
            _txtSecureCode.keyboardType = UIKeyboardTypeDefault;
            _txtSecureCode.returnKeyType = UIReturnKeyDone;
            
            
            _txtSecureCodeConfirm.delegate = self;
            _txtSecureCodeConfirm.secureTextEntry = YES;
            _txtSecureCodeConfirm.keyboardType = UIKeyboardTypeDefault;
            _txtSecureCodeConfirm.returnKeyType = UIReturnKeyDone;
            
            _txtSecureCode.placeholder = NSLocalizedString(@"보안키 입력", @"보안키 입력");
            _txtSecureCodeConfirm.placeholder = NSLocalizedString(@"보안키 재확인", @"보안키 재확인");
            
            _lblDesc.text = NSLocalizedString(@"보안키를 안전하게 설정할수록\n다른 사람이 장치를 함부로\n등록할 수 없게 보호할 수 있습니다", @"보안키를 안전하게 설정할수록\n다른 사람이 장치를 함부로\n등록할 수 없게 보호할 수 있습니다");
            
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {
    _txtSecureCode.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
    _txtSecureCodeConfirm.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
}

#pragma mark - Main Logic


#pragma mark - UI Events
- (void)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;
    }

    [super btnConfirmTouched:sender];
}

#pragma mark - CustomTextField

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

    return YES;
}


@end
