//
//  AdminAuthPopupView.m
//  kneet2
//
//  Created by Jason Lee on 1/13/16.
//  Copyright (c) 2016 ntels. All rights reserved.
//
@import AVFoundation;
#import "JDObject.h"

#import "RequestHandler.h"
#import "JDJSONModel.h"

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

#import "ValidateUtil.h"
#import "AdminAuthPopupView.h"

#define ksSetTopAdminPasswd @"198877"

@interface AdminAuthPopupView () <CustomTextFieldDelegate> {

}

@end

@implementation AdminAuthPopupView

- (id)initFromNib {

    for (UIView *view in [CommonUtil nibViews:@"AdminAuthPopupView"]) {
        if ([view isKindOfClass:[AdminAuthPopupView class]]) {
            self = (AdminAuthPopupView *)view;

            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            self.lblTitle.text = @"관리자 비밀번호";

            _txtPwd.secureTextEntry = YES;
            _txtPwd.keyboardType = UIKeyboardTypeDefault;
            _txtPwd.returnKeyType = UIReturnKeyDone;
            
            [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

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


- (void)btnConfirmTouched:(id)sender {

    if (![ValidateUtil validateTextfiled:_txtPwd type:ValidateTypeNull title:NSLocalizedString(@"비밀번호", @"비밀번호")]) {
        return;
    }

    if (![_txtPwd.text isEqualToString:ksSetTopAdminPasswd]) {
        [[JDFacade facade] toast:@"비밀번호가 틀렸습니다"];
        return;
    }
    
    
    AVAuthorizationStatus avAuthrizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (avAuthrizationStatus != AVAuthorizationStatusAuthorized) {//카메라 권한이 없을 경우,
        NSString *message = MSG_CAMERA_DISABLE;
        
        CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:message delegate:nil OKButtonTitle:@"설정" cancelButtonTitle:@"취소"];
        [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 0) {//OK - 이전화면으로
                
                NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                [[UIApplication sharedApplication] openURL:settingsURL];
                return;
            }
        }];
    }

    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"QRCodeViewController" storyboardName:@"HomeHub"];
    
    vc.providesPresentationContextTransitionStyle = YES;
    vc.definesPresentationContext = YES;
    
    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    [super btnConfirmTouched:sender];
    
    [[JDFacade facade].currentViewController presentViewController:vc animated:YES completion:nil];
}

#pragma mark - CustomTextField

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

    return YES;
}


@end
