//
//  QRCodeInputPopupView.m
//  kneet
//
//  Created by Jason Lee on 5/18/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 "QRCodeInputPopupView.h"
#import "ValidateUtil.h"

@interface QRCodeInputPopupView () <CustomTextFieldDelegate> {

}

@end

@implementation QRCodeInputPopupView

- (id)initFromNib {

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

            NSLog(@"%s\n %@", __PRETTY_FUNCTION__, self.popUpView);
            self.lblTitle.text = NSLocalizedString(@"홈허브 코드 직접 입력",nil);


            _txtActivationCode.keyboardType = UIKeyboardTypeDefault;
            _txtActivationCode.returnKeyType = UIReturnKeyDone;

            _txtPasswd.delegate = self;
            _txtPasswd.secureTextEntry = YES;
            _txtPasswd.keyboardType = UIKeyboardTypeDefault;
            _txtPasswd.returnKeyType = UIReturnKeyDone;
         
            
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {
    self.txtActivationCode.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
    self.txtPasswd.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
}

#pragma mark - Main Logic


#pragma mark - UI Events
- (void)btnConfirmTouched:(id)sender {

#if TARGET_IPHONE_SIMULATOR
    if (_qrcodeType == QRCodeTypeHomeHub) {
        if ([_txtActivationCode.text isEmptyString]) {
//            _txtActivationCode.text = @"NTels-d73df166b7a8151b";
            _txtActivationCode.text = @"NTels-cd39b0d2526513ec";
        }
        
        if ([_txtPasswd.text isEmptyString]) {
            _txtPasswd.text = @"091377";
        }
    }
#endif

    //1.validate
    if (![ValidateUtil validateTextfiled:_txtActivationCode type:ValidateTypeNull title:@"코드"]) {
        return;
    }

    if (![ValidateUtil validateTextfiled:_txtPasswd type:ValidateTypeNull title:@"PW"]) {
        return;
    }

    QRCodeViewController *vc = (QRCodeViewController *)[JDFacade facade].currentViewController;
    if (![vc isKindOfClass:[QRCodeViewController class]]) {
        vc = [[JDFacade facade] viewControllerOnPresentedViewController:[JDFacade facade].currentViewController viewControllerClass:[QRCodeViewController class]];
    }

    if (vc) {
        [vc requestQRAuthrize:_txtActivationCode.text password:_txtPasswd.text];
    }
    
//    [super btnConfirmTouched:sender];
}

#pragma mark - CustomTextField

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

    return YES;
}


@end
