//
//  ThingsAddCameraViewController.m
//  kneet
//
//  Created by Jason Lee on 5/26/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//

#import "ThingsAddCameraViewController.h"
#import "JDObject.h"
#import "ValidateUtil.h"
#import "CustomTextField.h"
#import "RequestHandler.h"
#import "CustomLabel.h"
#import "CustomButton.h"


@interface ThingsAddCameraViewController () {

}
@end


#pragma mark - Class Definition

@implementation ThingsAddCameraViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.title = NSLocalizedString(@"새 장치 추가",nil);
}

- (void)initUI {
    
    

    //set tableview option
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];

    self.tableView.tableFooterView = [[UIView alloc] init]; //this call table events;

    _txtFoscamURL.keyboardType = UIKeyboardTypeURL;
    _txtFoscamURL.returnKeyType = UIReturnKeyDone;

    _txtFoscamPort.keyboardType = UIKeyboardTypeNumberPad;
    _txtFoscamPort.returnKeyType = UIReturnKeyDone;

    _txtFoscamId.keyboardType = UIKeyboardTypeDefault;
    _txtFoscamId.returnKeyType = UIReturnKeyDone;

    _txtFoscamPwd.keyboardType = UIKeyboardTypeDefault;
    _txtFoscamPwd.returnKeyType = UIReturnKeyDone;
    _txtFoscamPwd.secureTextEntry = YES;
    
    _txtFoscamId.placeholder = NSLocalizedString(@"포스캠 아이디", @"포스캠 아이디");
    _txtFoscamPwd.placeholder = NSLocalizedString(@"비밀번호", @"비밀번호");
    _txtFoscamURL.placeholder = NSLocalizedString(@"접속 URL", @"접속 URL");
    _txtFoscamPort.placeholder = NSLocalizedString(@"포트번호", @"포트번호");
    
    //Localization
    _lblTitle.text = NSLocalizedString(@"FOSCAM을 현재 홈에 등록합니다", @"FOSCAM을 현재 홈에 등록합니다");
    
    _lblStep1Title.text = NSLocalizedString(@"Foscam의 매뉴얼에 따라 인터넷을 연결을 완료하세요", @"Foscam의 매뉴얼에 따라 인터넷을 연결을 완료하세요");
    _lblStep2Title.text = NSLocalizedString(@"아래 정보를 입력하면 바로 등록됩니다", @"아래 정보를 입력하면 바로 등록됩니다");
    _lblStep3Title.text = NSLocalizedString(@"● 포트포워딩이 되어있어야 합니다\n● 외부에서 연결하려면 URL에 DDNS 또는 외부접속  IP를 입력해주세요", @"● 포트포워딩이 되어있어야 합니다\n● 외부에서 연결하려면 URL에 DDNS 또는 외부접속  IP를 입력해주세요");
    
    [_btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {

}

#pragma mark - Main Logic
- (void)requestRegisterCamera {
    //parameters
    NSDictionary *parameter = @{@"camera_id": _txtFoscamId.text,
                                @"camera_password": _txtFoscamPwd.text,
                                @"url": _txtFoscamURL.text,
                                @"port": _txtFoscamPort.text};

    NSString *path = [NSString stringWithFormat:ksEmptyString];//API_POST_CAMERA];

    [[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
- (IBAction)btnConfrimTouched:(id)sender {

#ifdef DEBUG
    if ([_txtFoscamId.text isEmptyString]) {
        _txtFoscamId.text = @"ntels_cam";
    }

    if ([_txtFoscamPwd.text isEmptyString]) {
        _txtFoscamPwd.text = @"wotdemo2015@";
    }

    if ([_txtFoscamURL.text isEmptyString]) {
        _txtFoscamURL.text = @"http://192.168.2.100";
    }

    if ([_txtFoscamPort.text isEmptyString]) {
        _txtFoscamPort.text = @"8090";
    }
//http://192.168.2.100:8090/
//    ID : ntels_cam
//    PW : wotdemo2015@
#endif
    
    //1.validate
    if (![ValidateUtil validateTextfiled:_txtFoscamId type:ValidateTypeNull title:NSLocalizedString(@"포스캠 아이디", @"포스캠 아이디")]) {
        return;
    }
    
    if (![ValidateUtil validateTextfiled:_txtFoscamPwd type:ValidateTypeNull title:NSLocalizedString(@"비밀번호", @"비밀번호")]) {
        return;
    }
    
    if (![ValidateUtil validateTextfiled:_txtFoscamURL type:ValidateTypeNull title:NSLocalizedString(@"접속 URL", @"접속 URL")]) {
        return;
    }
    
    if (![ValidateUtil validateTextfiled:_txtFoscamPort type:ValidateTypeNull title:NSLocalizedString(@"포트번호", @"포트번호")]) {
        return;
    }

    [self requestRegisterCamera];
}



#pragma mark - MemoryWarning
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
