//
//  TriggerSelectPopupView.m
//  kneet2
//
//  Created by Jason Lee on 11/23/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

#import "CustomLabel.h"
#import "CustomButton.h"
#import "CustomLabelButton.h"

#import "TriggerSelectPopupView.h"
#import "DeviceSelectPopupView.h"
#import "DeviceNodePopupView.h"
#import "TimePickerPopupView.h"
#import "DaylightPopupView.h"
#import "ExternHeatPopupView.h"

@interface TriggerSelectPopupView () {
    
    NSMutableArray<ItemModel> *_checkedItems;
}

@end

@implementation TriggerSelectPopupView

- (id)initFromNib {
    
    for (UIView *view in [CommonUtil nibViews:@"TriggerSelectPopupView"]) {
        if ([view isKindOfClass:[TriggerSelectPopupView class]]) {
            self = (TriggerSelectPopupView *)view;
            
            //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
            self.frame = [UIScreen mainScreen].bounds;
            
            [self.imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset: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.btnCancelSingle setBackgroundImage:[UIImage imageNamed:@"img_popup_btn"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
            [self.btnCancelSingle setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_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 {
    
}


#pragma mark - UI Events
- (IBAction)btnTimerTouched:(id)sender {
    
    ItemModel *trigger = _refTriggers && _refTriggers.count ? _refTriggers[0] : nil;
    
    BOOL isNewTrigger = !trigger.timers || !trigger.timers.count;
    ItemModel *timeTrigger = !isNewTrigger ? trigger : [[ItemModel alloc] init];
    
    TimePickerPopupView *tpopup = [[TimePickerPopupView alloc] initFromNib];
    tpopup.timeTrigger = timeTrigger;
    tpopup.refConditions = _refConditions;
    
    [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            if (isNewTrigger) {
                [_refTriggers addObject:timeTrigger];
                
                NSLog(@"_refTriggers : %@", _refTriggers);
                
                [super btnConfirmTouched:sender];
            }
        }
    }];
}

- (IBAction)btnDaylightTouched:(id)sender {
    
    ItemModel *trigger = _refTriggers && _refTriggers.count ? _refTriggers[0] : nil;
    
    BOOL isNewTrigger = !trigger.daylights || !trigger.daylights.count;
    ItemModel *daylightTrigger = !isNewTrigger ? trigger : [[ItemModel alloc] init];
    
    DaylightPopupView *dpopup = [[DaylightPopupView alloc] initFromNib];
    dpopup.daylightTrigger = daylightTrigger;
    dpopup.refConditions = _refConditions;
    
    [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            if (isNewTrigger) {
                [_refTriggers addObject:daylightTrigger];
                [super btnConfirmTouched:sender];
            }
        }
    }];
}

- (IBAction)btnHeatTouched:(id)sender {
    
    ItemModel *trigger = _refTriggers && _refTriggers.count ? _refTriggers[0] : nil;
    
    BOOL isNewTrigger = !trigger.heats || !trigger.heats.count;
    ItemModel *heatTrigger = !isNewTrigger ? trigger : [[ItemModel alloc] init];
    
    ExternHeatPopupView *epopup = [[ExternHeatPopupView alloc] initFromNib];
    epopup.externHeatTrigger = heatTrigger;
    epopup.refConditions = _refConditions;
    
    [epopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex == 0) {//OK
            if (isNewTrigger) {
                
                NSLog(@"heatTrigger : %@", heatTrigger);
                
                [_refTriggers addObject:heatTrigger];
                [super btnConfirmTouched:sender];
            }
        }
    }];
}

- (IBAction)btnDeviceTouched:(id)sender {
    
    DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
    dpopup.refDevices = _refDevices;
    dpopup.typeCode = ksItemTypeCodeTrigger;
    
    [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
        
        if (buttonIndex == 0) {//ok
            
            DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
            npopup.refDevice = dpopup.selectedDevices[0];
            npopup.typeCode = ksItemTypeCodeTrigger;
            
            [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
                //action add
                if (buttonIndex == 0) {//OK
                    npopup.refDevice.itemName = @"장치 상태가 바뀔 때";
                    npopup.refDevice.itemSubTypeCode = ksItemSubTypeCodeDevice; //트리거 타입 설정
                    
                    [_refTriggers addObject:npopup.refDevice];
                    [super btnConfirmTouched:sender];
                }
            }];
            
            
            //디바이스 트리거인 경우, 요일 반복 제거,
            [_refConditions enumerateObjectsUsingBlock:^(ItemModel *pCondition, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([pCondition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
                    [_refConditions removeObject:pCondition];
                    *stop = YES;
                }
            }];
        }
    }];
}



- (IBAction)btnCancelTouched:(id)sender {
    [super btnCancelSingleTouched:sender];
}


@end
