//
//  HomeHubChangeViewController.h
//  kneet
//
//  Created by Jason Lee on 10/23/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 "CustomButton.h"

#import "QRCodeViewController.h"

#import "HomeHubChangeViewController.h"
#import "DeviceModel.h"

@interface HomeHubChangeViewController () <CustomTextFieldDelegate> {

}
@end


#pragma mark - Class Definition

@implementation HomeHubChangeViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:kfAnimationDur animations:^{
        _maskView.alpha = 0.7;
    } completion:^(BOOL finished) {
        
        _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
        
        [UIView animateWithDuration:kfAnimationDur animations:^{
            [self.view layoutIfNeeded];
        }];
    }];
}


- (void)initUI {

    _txtSecureCode.secureTextEntry = YES;
    _txtSecureCode.delegate = self;
    _txtSecureCode.returnKeyType = UIReturnKeyDone;
    _txtSecureCode.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
    
    _txtSecureCode.placeholder = NSLocalizedString(@"보안키", @"보안키");

    [_btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {
    
}

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

    //parameters
    NSDictionary *parameter = @{@"activation_code" : _activationCode,
                                @"activation_password" : _activationPassword,
                                @"user_device_password": _txtSecureCode.text};
    
    NSString *path = [NSString stringWithFormat:API_POST_PARTNER_QR];
    
    [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[DeviceModel class] completion:^(id responseObject) {
        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
            return;
        }
        
        DeviceModel *device = (DeviceModel *)responseObject;
        if (device) {
            [JDFacade facade].loginUser.homehubDeviceId = device.deviceId;
            
            [[JDFacade facade] updateLoginInfo:^{
                UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"WelcomeViewController" storyboardName:@"Main"];
                [self presentViewController:vc animated:YES completion:nil];
            }];
        }
        
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}


#pragma mark - UI Events

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

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

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

- (IBAction)btnCloseTouched:(id)sender {
    
    [[JDFacade facade] confirmTitle:@"연결취소" message:@"홈허브 연결을 취소할까요?" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            [[JDFacade facade] dismissModalStack:YES completion:nil];
        }
    }];
}

#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
