//
//  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"
#import "MultiLevelPopupView.h"
#import "CustomRadioGroup.h"
#import "CommandClassControlView.h"

#define kfCommandNodeSwitchLabelPadding 18.0f
#define kfCommandNodeSwitchLeftPadding     18.0f
#define kfCommandNodeSwitchRightPadding    18.0f
#define kfCommandNodeLockRightPadding      18.0f

#define kfControlNodeDefaultWidth               215.0f
//#define kfControlMaxWidth                   IPHONE_WIDTH - 105.0f
#define kfControlNodeDefaultHeight              32.0f
#define kfControlNodeDefaultMargin              5.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 = kfControlNodeDefaultWidth;
            controlView.height = kfControlNodeDefaultHeight;
            
            break;
        }
    }
    
    return controlView;
}

//- (void)setNodeEnable:(BOOL)nodeEnable {
//    _nodeEnable = nodeEnable;
//    
//    [self setLayoutUI];
//}
//
//- (void)setLayoutUI {
//    
//}

- (CGSize)sizeForIntrinsic {
    return CGSizeMake(kfControlNodeDefaultWidth, kfControlNodeDefaultHeight);
}

- (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
    return CGSizeMake(kfControlNodeDefaultWidth, kfControlNodeDefaultHeight);
}

- (void)controlNodeAction {
    if ([self.delegate respondsToSelector:@selector(didControlNodeAction:)]) {
        [self.delegate didControlNodeAction:self];
    }
}

@end


@implementation CommandClassControlNodeSensorBinaryView

@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 + kfCommandNodeSwitchLeftPadding + kfCommandNodeSwitchRightPadding;
    _constraintBtnOpenWidth.constant = eWidth;

    
    CmdClsValueModel *cmdclsValueOn = nil, *cmdclsValueOff = nil;
    if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
        cmdclsValueOn = _subItem.cmdclsValueList[0];
        cmdclsValueOff = _subItem.cmdclsValueList[1];
        
        //default setting
        _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue;
        
        //layout setting
        [_btnOpen setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
        [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];

        
    } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
        cmdclsValueOff = _subItem.cmdclsValueList[0];
        
        //default setting
        _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOff.cmdclsValue;
        
        //layout setting
        [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
        
        [_btnOpen removeFromSuperview];
        [self removeConstraint:_constraintBtnSameWidth];
        
        _constraintBtnOpenWidth.constant = 0;
        _constraintBtnSpace.constant = 0;
        _constraintBtnCloseWidth.constant = eWidth;
    }
    
    if (_nodeEnable) {
        if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) {
            [self btnOpenTouched:nil];
        } else {
            [self btnCloseTouched:nil];
        }
    } else {
        _btnOpen.selected = NO;
        _btnClose.selected = NO;
        
        if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
            
            [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
            [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
            
        } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
            
            [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
        }
    }
}

- (IBAction)btnOpenTouched:(id)sender {
    
    if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {

        [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
        [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
    }
    
    _btnOpen.selected = YES;
    _btnClose.selected = NO;
    
    if (sender) {//트리거 설정
        [self controlNodeAction];
    }
}

- (IBAction)btnCloseTouched:(id)sender {
    
    if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {

        [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
        [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
        
        _btnOpen.selected = NO;
        _btnClose.selected = YES;
        
    } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
        
        [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
        
        _btnClose.selected = YES;
    }
    
    if (sender) {//트리거 설정
        [self controlNodeAction];
    }
}

@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 + kfCommandNodeSwitchLeftPadding + kfCommandNodeSwitchRightPadding;
    _constraintBtnOnWidth.constant = eWidth;

    
    CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0];
    CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
    
    //default setting
    _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue;
    
    [_btnOn setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
    [_btnOff setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
    
    if (_nodeEnable) {
        if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) {
            [self btnOnTouched:nil];
        } else {
            [self btnOffTouched:nil];
        }
    } else {
        _btnOn.selected = NO;
        _btnOff.selected = NO;
        
        [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
        [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
    }
}

- (IBAction)btnOnTouched:(id)sender {

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

    [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
    [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
    
    _btnOn.selected = YES;
    _btnOff.selected = NO;
    
    if (sender) {//트리거 설정
        [self controlNodeAction];
    }
}

- (IBAction)btnOffTouched:(id)sender {
    
    CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
    _subItem.cmdclsValue = cmdclsValueOff.cmdclsValue;

    [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
    [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
    
    _btnOn.selected = NO;
    _btnOff.selected = YES;
    
    if (sender) {//트리거 설정
        [self controlNodeAction];
    }
}

- (CGSize)sizeForIntrinsic {
    CGFloat width = (_constraintBtnOnWidth.constant * 2) + kfControlNodeDefaultMargin;
    return CGSizeMake(width, kfControlDefaultHeight);
}


@end

@interface CommandClassControlNodeSwitchMultiLevelView () {
    MultiLevelPopupView *_mpopup;
}

@end

@implementation CommandClassControlNodeSwitchMultiLevelView

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

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

- (void)setLayoutUI {
    
    CGFloat half = ([_subItem.controlMaxValue floatValue] - [_subItem.controlMinValue floatValue]) / 2;
    
    NSString *cmdClsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : [CommandClassControlView cmdclsValueForType:_subItem.dataTypeCode value:half];
    
    _subItem.conditionTypeCode = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ? _subItem.conditionTypeCode : ksConditionTypeCodeGreatOrEqual;
    
    NSString *condition = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ?
    ([_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하") : @"이상";
    
    NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", cmdClsValue, _subItem.unit, condition];
    _subItem.cmdclsValue = cmdClsValue;
    _subItem.cmdclsValueMsg = contentValue;
    
    [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal];
    
    _btnMultiLevel.selected = _nodeEnable;
}

- (IBAction)btnMutilLevelTouched:(id)sender {
    //call popup
    if (!_mpopup) {
        _mpopup = [[MultiLevelPopupView alloc] initFromNib];
    }
    
    _mpopup.lblTitle.text = _subItem.sourceSubName;
    _mpopup.refSubItem = _subItem;
    [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            
            NSString *condition = [_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
            NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", _subItem.cmdclsValue, _subItem.unit, condition];
            _subItem.cmdclsValueMsg = contentValue;
            
            [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal];
            
            if (sender) {//트리거 설정
                [self controlNodeAction];
            }
        }
    }];
}

@end

@implementation CommandClassControlNodeSensorMultiLevelView

@end

@implementation CommandClassControlNodeBatteryView

@end

@implementation CommandClassControlNodeMeterView

@end


