//
//  ActionDeviceCommandPopupView.m
//  OneCable
//
//  Created by ncomz on 2017. 6. 9..
//  Copyright © 2017년 ntels. All rights reserved.
//

#import "ActionDeviceCommandPopupView.h"

@interface ActionDeviceCommandPopupView()

@property (weak, nonatomic) IBOutlet UIView *viewValve;
@property (weak, nonatomic) IBOutlet UIView *viewPlug;

@property (weak, nonatomic) IBOutlet UIButton *btnValveControl;
@property (weak, nonatomic) IBOutlet CustomButton *btnValveClose;

@property (weak, nonatomic) IBOutlet UIButton *btnPlugControl;
@property (weak, nonatomic) IBOutlet CustomButton *btnPlugOn;
@property (weak, nonatomic) IBOutlet CustomButton *btnPlugOff;


@property BOOL isShowValve; //

@end

@implementation ActionDeviceCommandPopupView

- (id)initFromNib {
    
    
    NSLog(@"common : %@", [CommonUtil nibViews:@"ActionDeviceCommandPopupView"]);
    
    for (UIView *view in [CommonUtil nibViews:@"ActionDeviceCommandPopupView"]) {
    
        NSLog(@"view : %@", view);
        
        if ([view isKindOfClass:[ActionDeviceCommandPopupView class]]) {
            
            self = (ActionDeviceCommandPopupView *)view;
            
            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            
            
            [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnValveClose setBackgroundImage:[UIImage imageNamed:@"img_input_round_bg_default"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnValveClose setBackgroundImage:[UIImage imageNamed:@"img_input_round_bg_default"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnValveClose setBackgroundImage:[UIImage imageNamed:@"img_btn_common2_active"] forState:UIControlStateSelected capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            [self.btnPlugOn setBackgroundImage:[UIImage imageNamed:@"common_toggle_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnPlugOn setBackgroundImage:[UIImage imageNamed:@"common_toggle_right"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnPlugOn setBackgroundImage:[UIImage imageNamed:@"common_toggle_right_select"] forState:UIControlStateSelected capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            [self.btnPlugOff setBackgroundImage:[UIImage imageNamed:@"common_toggle_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnPlugOff setBackgroundImage:[UIImage imageNamed:@"common_toggle_left"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnPlugOff setBackgroundImage:[UIImage imageNamed:@"common_toggle_left_select"] forState:UIControlStateSelected capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            
            //Localization
            [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
            [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
          
        }
    }
    
    return self;
}

- (void)didMoveToSuperview {
    
}


- (void)setCmdclsCode:(NSString *)cmdclsCode {
    
    _cmdclsCode = cmdclsCode;
    
    //NSLog(@"cmdclsCode : %@", cmdclsCode);
    
    _isShowValve = _cmdclsCode != nil && EQUALS(_cmdclsCode, ksCmdClassTypeValve);
    _viewPlug.hidden = _isShowValve;
    _viewValve.hidden = !_isShowValve;
}

- (void)setRefDevice:(ItemModel *)refDevice {
    
    _refDevice = [ItemModel new];
    _refDevice = refDevice;
    
    //NSLog(@"refDevice : %@", refDevice);
    
    self.lblTitle.text = _refDevice.sourceName;

    
    ItemSubModel *subModel =  [_refDevice.subItems objectAtIndex:0];
    
    if (_isShowValve && subModel.cmdclsValueList.count > 0) {
        
        CmdClsValueModel *cmdModel = [subModel.cmdclsValueList objectAtIndex:0];
        _btnValveClose.value = cmdModel;
        _btnValveClose.selected = _btnValveControl.selected = cmdModel.isSelected;
        
    }
    else if (subModel.cmdclsValueList.count > 1){
 
        CmdClsValueModel *cmdOffModel = [subModel.cmdclsValueList objectAtIndex:0];
        CmdClsValueModel *cmdOnModel = [subModel.cmdclsValueList objectAtIndex:1];
        
        _btnPlugOff.value = cmdOffModel;
        _btnPlugOn.value = cmdOnModel;
        
        _btnPlugOff.selected = cmdOffModel.isSelected;
        _btnPlugOn.selected = cmdOnModel.isSelected;
        _btnPlugControl.selected = _btnPlugOn.selected || _btnPlugOff.selected;
    }

}

- (ItemSubModel *)selectedNode {

    return [_refDevice.subItems objectAtIndex:0];
}


#pragma mark - User Action
//제어
- (IBAction)control:(id)sender {
    
    UIButton *btn = sender;
    btn.selected = YES;
    
}

//끄기켜기 - 플러그
- (IBAction)onOffPlug:(id)sender {
    
    if (_btnPlugControl.selected) {
        
        
        BOOL isOff = [_btnPlugOff isEqual:sender];
        
        _btnPlugOn.selected = !isOff;
        _btnPlugOff.selected = isOff;
        
        if (_btnPlugOn.value != nil && _btnPlugOff.value != nil) {
            
            ((CmdClsValueModel*)(_btnPlugOn.value)).isSelected = !isOff;
            ((CmdClsValueModel*)(_btnPlugOff.value)).isSelected = isOff;
        }
        
    }
}

//잠그기 - 벨브
- (IBAction)closeValve:(id)sender {

    if (_btnValveControl.selected) {
        
        _btnValveClose.selected = YES;
        
        if (_btnValveClose.value != nil) {
            
            ((CmdClsValueModel*)(_btnValveClose.value)).isSelected = YES;
        }
    }
}

- (IBAction)confirm:(id)sender {
    
    NSMutableArray<ItemModel> *checkedItems = (NSMutableArray<ItemModel> *)[_refDevice.subItems matchedArrayByObjectName:ksCustomCheckBoxStatus condition:YES];
    
    NSLog(@"checkedItems : %@", checkedItems);
    
    ItemSubModel *subModel =  [_refDevice.subItems objectAtIndex:0];
    
    NSLog(@"subModel : %@", subModel);

    
//    if (!_checkedItems || !_checkedItems.count) {
//        [[JDFacade facade] alert:NSLocalizedString(@"선택된 항목이 없습니다", @"선택된 항목이 없습니다")];
//        return;
//    }
    
    BOOL confirmValve = _isShowValve && _btnValveControl.selected && _btnValveClose.selected;
    BOOL confirmPlug = !_isShowValve && _btnPlugControl.selected && (_btnPlugOn.selected || _btnPlugOff.selected);
   
    if (confirmValve || confirmPlug) {

        for (CmdClsValueModel *cmdclsValue in subModel.cmdclsValueList) {
            
            if (cmdclsValue.isSelected) {
                
                subModel.cmdclsValue = cmdclsValue.cmdclsValue;
                subModel.cmdclsValueMsg = cmdclsValue.cmdclsValueMsg;
            }
        }
    }

    
    
    [super btnConfirmTouched:sender];
}





@end
