//
//  CommandClassControlNodeView.m
//  kneet2
//
//  Created by Jason Lee on 11/5/15.
//  Copyright © 2015 ntels. All rights reserved.
//
#import "JDObject.h"

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

#import "CommandClassControlNodeView.h"

#define kfSwitchLabelPadding 20.0f
#define kfCommandClassSwitchLeftPadding     20.0f
#define kfCommandClassSwitchRightPadding    20.0f
#define kfCommandClassLockRightPadding      20.0f

#define kfControlDefaultWidth               215.0f;
#define kfControlMaxWidth                   IPHONE_WIDTH - 105.0f
#define kfControlDefaultHeight              35.0f

@implementation CommandClassControlNodeView

+ (CommandClassControlNodeView *)viewForCommandClass:(CmdClsType)cmdClsType
{
    CommandClassControlNodeView *controlView = nil;
    for (UIView *view in [CommonUtil nibViews:@"CommandClassControlNodeView"]) {
        if (view.tag == cmdClsType) {
            controlView = (CommandClassControlNodeView *)view;
            controlView.width = kfControlDefaultWidth;
            controlView.height = kfControlDefaultHeight;
            
            break;
        }
    }
    
    return controlView;
}

- (CGSize)sizeForIntrinsic {
    return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
}

- (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
    return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
}

@end


@implementation CommandClassControlNodeSensorBinaryView


@end


@implementation CommandClassControlNodeSensorMultiLevelView


@end

@implementation CommandClassControlNodeValveView

- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (!newSuperview)
        return;
    
    [self initControl];
}

- (void)initControl {
    
    [self setLayoutUI];
}

- (void)setLayoutUI {
    
    NSString *stmp = _subItem.cmdclsValueMsgLongest;
    CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOpen.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
    
    CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
    _constraintBtnOpenWidth.constant = eWidth;
    
    //    CGRect sr = self.frame;
    //    sr.size.width = eWidth;
    //    self.frame = sr;
    
    CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0];
    CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
    _subItem.cmdclsValue = cmdclsValueOn.cmdclsValue;
    
    [_btnOpen setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
    [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
    
    
    _btnOpen.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
    _btnClose.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
}

- (IBAction)btnOpenTouched:(id)sender {
    
}

- (IBAction)btnCloseTouched:(id)sender {
    
}

@end

@implementation CommandClassControlNodeSwitchBinaryView

- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (!newSuperview)
        return;
    
    [self initControl];
}

- (void)initControl {
    
    [self setLayoutUI];
}

- (void)setLayoutUI {
    
    NSString *stmp = _subItem.cmdclsValueMsgLongest;
    CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOff.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
    
    CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
    _constraintBtnOnWidth.constant = eWidth;

    
    CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0];
    CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
    _subItem.cmdclsValue = cmdclsValueOn.cmdclsValue;

    [_btnOn setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
    [_btnOff setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
    
    
    _btnOn.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
    _btnOff.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
}

- (IBAction)btnOnTouched:(id)sender {
    _selectedCmdClsValue = _subItem.cmdclsValueList[0];
    [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
    [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
    
    _btnOn.selected = YES;
    _btnOff.selected = NO;
}

- (IBAction)btnOffTouched:(id)sender {
    _selectedCmdClsValue = _subItem.cmdclsValueList[1];
    [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
    [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
    
    _btnOn.selected = NO;
    _btnOff.selected = YES;
}

@end

@implementation CommandClassControlNodeSwitchMultiLevelView

@end