//
//  CommandClassControlDetailView.m
//  kneet
//
//  Created by Jason Lee on 3/16/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//

#import "JDObject.h"

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

#import "CustomLabel.h"
#import "CommonUtil.h"
#import "CustomButton.h"
#import "CustomRadioGroup.h"

#import "UIImageView+WebCache.h"

#import "CommandClassControlDetailView.h"
#import "CommandClassControlView.h"
#import "CustomImageView.h"

#define GRAPH_ANI_DUR 0.5f

#define kfControlDetailSwtchLeftPadding 45.0f
#define kfControlDetailSwtchRightPadding 28.0f

#define kfControlDetailDefaultWidth 320.0f
#define kfControlDetailDefaultHeight 275.0f

typedef void(^NodeCommandCompletionBlock)(id result);
typedef void(^NodeCommandFailureBlock)(id error);


@implementation CommandClassControlDetailView


+ (CommandClassControlView *)viewForCommandClass:(CmdClsType)cmdClsType
{
    CommandClassControlView *controlView = nil;
    for (UIView *view in [CommonUtil nibViews:@"CommandClassControlDetailView"]) {
        if (view.tag == cmdClsType) {
            controlView = (CommandClassControlView *)view;
            
            controlView.width = IPHONE_WIDTH;
            controlView.height = kfControlDetailDefaultHeight;
            
            break;
        }
    }

    if (!controlView) {//상위 부모에서 가져옴.
        controlView = [CommandClassControlView viewForCommandClass:cmdClsType];
    }

    return controlView;
}

@end


@implementation CommandClassSwitchDetailView

- (void)setLayoutUI {
    [super setLayoutUI];
    
    self.height = 347.0f;
    
    NSString *stmp = @"켜기";
    CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOff.titleLabel.font width:FLT_MAX].width;// + kfSwitchLabelPadding;
    
    CGFloat eWidth = width + kfControlDetailSwtchLeftPadding + kfControlDetailSwtchRightPadding;
    _constraintBtnSwitchWidth.constant = eWidth;
    
    _btnOn.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 28);
    _btnOff.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 28);
    
    _btnNode.enabled = _node.isDeviceOnline;
    _btnNode.selected = _btnNode.enabled && _isON;
    
    [self checkOffline];
}

- (IBAction)btnOnTouched:(id)sender {
    if (_isON) {
        [[JDFacade facade] toast:@"이미 켜진 상태입니다"];
        return;
    }
    [self requestNodeCommand:self.parameterForNodeCommand requestValue:_requestValueForOn completionHandler:nil failureHandler:nil];
}

- (IBAction)btnOffTouched:(id)sender {
    if (!_isON) {
        [[JDFacade facade] toast:@"이미 꺼진 상태입니다"];
        return;
    }
    
    [self requestNodeCommand:self.parameterForNodeCommand requestValue:_requestValueForOff completionHandler:nil failureHandler:nil];
}

- (void)setSwitchBinary {
    
    _pcontainer.hidden = !_node.isRequesting;
    if (!_pcontainer.hidden) {
        [self startProgressAni];
    } else {
        [self stopProgressAni];
    }
    
    _btnOn.enabled = _btnOff.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
    
    _lblContentValueMsg.text = _node.contentValueMsg;
    
    _btnNode.userInteractionEnabled = NO;
    [_btnOn setImage:[_btnOn imageForState:_btnOn.state] forState:_btnOn.state];
    [_btnOff setImage:[_btnOff imageForState:_btnOff.state] forState:_btnOff.state];
}

- (void)startProgressAni {
    
    //chagne button image
    [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
        _imgvProgress.transform = CGAffineTransformMakeRotation(M_PI);
    } completion:nil];
}

- (void)stopProgressAni {
    [UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear  animations:^{
        _imgvProgress.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
        _btnNode.selected = _btnNode.enabled && _isON;
        
        [self checkOffline];
    }];
}

- (void)checkOffline {
    if (!_btnNode.enabled) {//online?
        self.lblContentValueMsg.text = @"OFFLINE";
        [self.lblContentValueMsg setColor:kUITextColor01 text:self.lblContentValueMsg.text];
    }
}

@end



@implementation CommandClassValveDetailView


- (void)setLayoutUI {
    [super setLayoutUI];
    
    self.height = 347.0f;
    
    NSString *stmp = @"잠그기";
    CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnClose.titleLabel.font width:FLT_MAX].width;// + kfSwitchLabelPadding;
    
    CGFloat eWidth = width + kfControlDetailSwtchLeftPadding + kfControlDetailSwtchRightPadding;
    _constraintBtnWidth.constant = eWidth;
    
    _btnClose.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 28);
    
    [self checkOffline];
}

- (IBAction)btnCloseTouched:(id)sender {
    if (!_isOpened) {
        [[JDFacade facade] toast:@"이미 잠긴 상태입니다"];
        return;
    }
    
    [self requestNodeCommand:self.parameterForNodeCommand requestValue:_requestValueForClose completionHandler:nil failureHandler:nil];
}

- (void)setSwitchBinary {
    
    _pcontainer.hidden = !_node.isRequesting;
    if (!_pcontainer.hidden) {
        [self startProgressAni];
    } else {
        [self stopProgressAni];
    }
    
    _btnClose.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
    
    _lblContentValueMsg.text = _node.contentValueMsg;
    
    [_btnClose setImage:[_btnClose imageForState:_btnClose.state] forState:_btnClose.state];
}

- (void)startProgressAni {
    
    //chagne button image
    [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
        _imgvProgress.transform = CGAffineTransformMakeRotation(M_PI);
    } completion:nil];
}

- (void)stopProgressAni {
    [UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear  animations:^{
        _imgvProgress.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
        _btnNode.selected = _btnNode.enabled && _isOpened;
        
        [self checkOffline];
    }];
}

- (void)checkOffline {
    if (!_btnNode.enabled) {//online?
        self.lblContentValueMsg.text = @"OFFLINE";
        [self.lblContentValueMsg setColor:kUITextColor01 text:self.lblContentValueMsg.text];
    }
}   

@end


#pragma mark - Sensors View

@implementation CommandClassSensorBinaryDetailView


- (void)setLayoutUI {
    [super setLayoutUI];
    
    _lblNodeName.hidden = !_isMandatoryNode;
    if (!_lblNodeName.hidden) {
        _lblNodeName.text = _device.nodeName;
    }
    
    _btnNode.userInteractionEnabled = NO;
    
    [_btnNode setImage:[_node imageForMandataryForState:UIControlStateNormal] forState:UIControlStateNormal];
    [_btnNode setImage:[_node imageForMandataryForState:UIControlStateSelected] forState:UIControlStateSelected];
    [_btnNode setImage:[_node imageForMandataryForState:UIControlStateDisabled] forState:UIControlStateDisabled];
    
    _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
    _btnNode.selected = _btnNode.enabled && [_node.contentValue isEqualToString:@"TRUE"];
    
    if (_btnNode.enabled) {//online?
        [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];

        if ([_device.contentValue isEqualToString:@"TRUE"]) {
            [self.lblControl setColor:kUITextColor02 text:self.lblControl.text];
        }
    } else {
        self.lblControl.text = @"OFFLINE";
        [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
    }
}

@end


@implementation CommandClassSensorMultiLevelDetailView


- (void)setLayoutUI {
    [super setLayoutUI];
    
    _btnNode.userInteractionEnabled = NO;
    
    _lblNodeName.hidden = !_isMandatoryNode;
    if (!_lblNodeName.hidden) {
        _lblNodeName.text = _device.nodeName;
    }
    
    [_btnNode setImage:[_node imageForMandataryForState:UIControlStateNormal] forState:UIControlStateNormal];
    [_btnNode setImage:[_node imageForMandataryForState:UIControlStateSelected] forState:UIControlStateSelected];
    [_btnNode setImage:[_node imageForMandataryForState:UIControlStateDisabled] forState:UIControlStateDisabled];
    
    _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
    _btnNode.selected = _btnNode.enabled && [_node.contentValue isEqualToString:@"TRUE"];
    
    _lblCondition.hidden = !_node.isDeviceOnline || ![JDFacade facade].loginUser.isHomehubOnline;
    _lblCondition.text = [_node conditionForMandatary];
    
    if (_btnNode.enabled) {//online?
        [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
    } else {
        self.lblControl.text = @"OFFLINE";
        [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
    }
}

@end



