//
//  ModifyStreamDataPopupView.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 "ModifyStreamDataPopupView.h"
#import "ValidateUtil.h"

@interface ModifyStreamDataPopupView () <CustomTextFieldDelegate> {

}

@end

@implementation ModifyStreamDataPopupView

- (id)initFromNib {

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

            _txtFoscamId.keyboardType = UIKeyboardTypeDefault;
            _txtFoscamId.returnKeyType = UIReturnKeyDone;
            
            _txtFoscamPwd.keyboardType = UIKeyboardTypeDefault;
            _txtFoscamPwd.returnKeyType = UIReturnKeyDone;
            _txtFoscamPwd.secureTextEntry = YES;

            _txtFoscamURL.keyboardType = UIKeyboardTypeURL;
            _txtFoscamURL.returnKeyType = UIReturnKeyDone;
            
            _txtFoscamPort.keyboardType = UIKeyboardTypeNumberPad;
            _txtFoscamPort.returnKeyType = UIReturnKeyDone;
            
            
            _txtFoscamId.placeholder = NSLocalizedString(@"포스캠 아이디", @"포스캠 아이디");
            _txtFoscamPwd.placeholder = NSLocalizedString(@"비밀번호", @"비밀번호");
            _txtFoscamURL.placeholder = NSLocalizedString(@"접속 URL", @"접속 URL");
            _txtFoscamPort.placeholder = NSLocalizedString(@"포트번호", @"포트번호");
            
            
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
        }
    }

    return self;
}

- (void)didMoveToSuperview {
    _txtFoscamId.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
    _txtFoscamPwd.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
    _txtFoscamURL.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
    _txtFoscamPort.customTextFieldSuperview = CustomTextFieldSuperviewIsPopupContentView;
}

- (void)setDeviceDetail:(DeviceDetailModel *)deviceDetail {
    _deviceDetail = deviceDetail;
    
    _txtFoscamURL.text = _deviceDetail.ip;
    _txtFoscamPort.text = _deviceDetail.port;

    _txtFoscamId.text = _deviceDetail.cameraId;
}

#pragma mark - Main Logic


#pragma mark - UI Events
- (void)btnConfirmTouched:(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;
    }
    
    [super btnConfirmTouched:sender];
}

@end
