//
//  ThingsViewController.m
//  kneet
//
//  Created by Jason Lee on 3/10/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "JDObject.h"

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


#import "CustomLabel.h"
#import "CustomImageView.h"
#import "CustomTextField.h"
#import "CustomButton.h"
#import "JYRefreshController.h"
#import "WYPopoverController.h"
#import "ImageUtil.h"

#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"

#import "CommandClassControlView.h"
#import "ThingsDetailViewController.h"

#import "ThingsViewController.h"
#import "ThingsAddViewController.h"
#import "ThingsAddStartViewController.h"
#import "CustomTableView.h"
#import "JYPullToRefreshController.h"
#import "HomeMemberViewController.h"

#define kfThingsTableViewCellHeight 100.0f

@interface ThingsCollectionViewCell () {
    NSInteger _commandStatusElapsedTime;
}

@property (weak, nonatomic) NSIndexPath *indexPath;

@end

@implementation ThingsCollectionViewCell

- (void)awakeFromNib {
    _pcontainerView.hidden = YES;
}

- (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:nil];
}

@end


@interface ThingsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> {
    NSMutableArray<DeviceModel> *_deviceList;
    NSString *_pagingId, *_pagingType;
    BOOL _isNotFirstLoading;
    
    NSMutableArray<DeviceModel> *_commandArray;
    NSTimer *_devicesBackgroundTimer;
    NSTimer *_deviceCommandsBackgroundTimer;
    
    NSInteger _deviceFlag;
    
}

@property (strong, nonatomic) JYPullToRefreshController *refreshController;

@end


#pragma mark - Class Definition

@implementation ThingsViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self initProperties];
    [self initUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self prepareViewDidLoad];
}

- (void)initProperties {
    _deviceFlag = IS_IPHONE_6P ? 3 : 2;
}

- (void)initUI {
    
    //set tableview option
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = kUIBgColor04;
    _collectionView.alwaysBounceVertical = YES;
    
    [self setThingsPopoverOptions];
    
    [self initRefreshController];
}

- (void)initRefreshController {
    //set refresh controls
    __weak typeof(self) weakSelf = self;
    self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.collectionView];
    self.refreshController.pullToRefreshHandleAction = ^{
        [weakSelf requestDeviceList:@YES];
    };
}


- (void)setThingsPopoverOptions {
    
    //set Popover Contents
    __weak typeof(self) weakSelf = self;
    _popooverOptionArray = [[NSMutableArray alloc] init];

    [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로고침", @"새로고침"),
                                      @"iconName": @"img_bg_morepopup_icon_refresh",
                                      @"target": weakSelf,
                                      @"selector": [NSValue valueWithPointer:@selector(refreshDeviceList)]}];

//    if ([JDFacade facade].loginUser.level == 90) {//권한
//        [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"추가", @"추가"),
//                                          @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
//                                          @"target": weakSelf,
//                                          @"selector": [NSValue valueWithPointer:@selector(addNewDevice)]}];
//        
//        [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
//                                          @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
//                                          @"target": weakSelf,
//                                          @"selector": [NSValue valueWithPointer:@selector(toggleEditMode)]}];
//    }
}


- (void)prepareViewDidLoad {
    //fetch devices from server
    [self updateTitle];
    [self performSelector:@selector(requestDeviceList:) withObject:@YES afterDelay:0.0f];
}

- (void)updateHomeHubStatusToDevices {
    [self updateTitle];
    
    for (DeviceModel *device in _deviceList) {
        device.onlineState = [JDFacade facade].loginUser.homehubOnlineState;
    }
    [_collectionView reloadData];
}


//제어를 요청한 장치상태를 조회함.
- (void)requestPollingCommandStatusOfDeviceInBackground:(DeviceModel *)device {
    if (!_commandArray) {
        _commandArray = (NSMutableArray<DeviceModel> *)[[NSMutableArray alloc] init];
    }
    
    __block BOOL isStatusChanged = NO;
    if (device && [device isKindOfClass:[DeviceModel class]]) {//validate, aleady have,
        if (![_commandArray objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", device.deviceId, device.nodeId]) {//일치하는 디바이스가 있을 경우, 추가하지 않음.
            [_commandArray addObject:device];
            
            isStatusChanged = YES;
        }
    }
    
    if (_commandArray.count) {
        
        NSMutableString *pathParams = [[NSMutableString alloc] init];
        for (DeviceModel *pDevice in _commandArray) {
            NSString *prefix = [pathParams isEmptyString] ? ksEmptyString : @",";
            [pathParams appendFormat:@"%@%@_%@", prefix, pDevice.deviceId, pDevice.nodeId];
        }
        
        //20
        NSString *path = [NSString stringWithFormat:API_GET_DEVICE_NODE_STATUS, pathParams];
        
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
            DeviceListModel *fdevices = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
                                                                            modelClass:[DeviceListModel class] showLoadingView:YES];
            
            if (fdevices && fdevices.list && fdevices.list.count) {
                
                [_commandArray enumerateObjectsUsingBlock:^(DeviceModel *rdevice, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    DeviceModel *matchedDevice = (DeviceModel *)[fdevices.list objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", rdevice.deviceId, rdevice.nodeId];
                    
                    //실행 여부 및 10초 경과 확인
                    BOOL isOverTimeLimit = [self elapsedSecondsFromNow:rdevice] > 10;
                    
                    //실행 여부 확인
                    NSInteger elapsedTime = [self elapsedSecondsFrom:rdevice to:matchedDevice];
                    BOOL hasChangedStatus = elapsedTime > 0;
                    
                    rdevice.isRequesting = [rdevice.contentValue isEqualToString:matchedDevice.contentValue] && !hasChangedStatus && !isOverTimeLimit;
                    
                    //TODO - home hub check
//                    rdevice.requestTime = matchedDevice.requestTime;
//                    rdevice.collectTime = matchedDevice.collectTime;
                    rdevice.onlineState = matchedDevice.onlineState;
                    
                    if (!rdevice.isRequesting || !rdevice.isOnline || ![JDFacade facade].loginUser.isHomehubOnline) {//정상적으로 변경됨.
                        rdevice.contentValue = matchedDevice.contentValue;
                        [_commandArray removeObject:rdevice];
                        
                        isStatusChanged = YES;
                    }
                    
#ifdef DEBUG_MODE
                    NSLogInfo(@"==########== device command status = %@, elapsedTime = %zd ==########==", [JDFacade facade].loginUser.homehubOnlineState, elapsedTime);
#endif
                }];
            } else {//
                NSLog(@"no devices");
            }
            
            if (_commandArray.count) {//커맨드 실행 중인 디바이스가 있을 경우,
                //schedul timer.
                if (!_deviceCommandsBackgroundTimer) {
                    _deviceCommandsBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(requestPollingCommandStatusOfDeviceInBackground:) userInfo:nil repeats:YES];
                }
            } else {
                [_deviceCommandsBackgroundTimer invalidate];
                _deviceCommandsBackgroundTimer = nil;
            }
            
            //리스트와 상세의 상태를 계속 매핑해줌.
            [self matchDeviceListWithOnCommandsDevices];
            
            //변화가 있을 경우, 컬렉션뷰를 리로드
            if (isStatusChanged) {
                [_collectionView reloadData];
                
                ThingsDetailViewController *vc = (ThingsDetailViewController *)[JDFacade facade].currentViewController;
                if ([vc isKindOfClass:[ThingsDetailViewController class]]) {
                    [vc.tableView reloadData];
                }
            }
        });
    }
}

- (NSInteger)elapsedSecondsFromNow:(DeviceModel *)runningDevice {
    NSInteger seconds = 0;
    if (runningDevice.requestTime && ![runningDevice.requestTime isEmptyString]) {
        NSDate *rdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:runningDevice.requestTime]];
        NSTimeInterval elapsed = [[NSDate systemDate] timeIntervalSinceDate:rdate];
        seconds = elapsed;
    }
    return seconds;
}

- (NSInteger)elapsedSecondsFrom:(DeviceModel *)runningDevice to:(DeviceModel *)fetchedDevice {
    NSInteger seconds = 0;
    if (runningDevice.requestTime && ![runningDevice.requestTime isEmptyString] && fetchedDevice.collectTime && ![fetchedDevice.collectTime isEmptyString]) {
        NSDate *rdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:runningDevice.requestTime]];
        NSDate *fdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:fetchedDevice.collectTime]];
        seconds = [fdate secondsAfterDate:rdate];
    }
    return seconds;
}

- (void)addNewDevice {
    UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)refreshDeviceList {
    [self performSelector:@selector(requestDeviceList:) withObject:@YES afterDelay:0.0f];
}

#pragma mark - Main Logic

- (void)requestDeviceListRecently {
    DeviceModel *firstDevice = [_deviceList firstObject];
    _pagingType = ksListPagingTypeUpward;
    _pagingId = firstDevice.createDatetime;
    
    [self performSelector:@selector(requestDeviceList:) withObject:@YES afterDelay:0.0f];
}

- (void)requestDeviceListOlder {
    DeviceModel *lastDevice = [_deviceList lastObject];
    _pagingType = ksListPagingTypeDownward;
    _pagingId = lastDevice.createDatetime;
    
    [self performSelector:@selector(requestDeviceList:) withObject:@YES afterDelay:0.0f];
}

- (void)requestDeviceList:(id)arg {

    if (![JDFacade facade].loginUser.hasHomeHub) {
        return;
    }
    
    BOOL showLoadingView = [arg isKindOfClass:[NSTimer class]] ? [((NSTimer *)arg).userInfo boolValue] : [arg boolValue];
    
    //parameters
    NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
                                @"paging_type": _pagingType ? _pagingType : ksEmptyString};

    NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];

    [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestGET parameters:parameter
                                           modelClass:[DeviceListModel class] showLoadingView:showLoadingView completion:^(id responseObject) {
        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
            return;
        }

        DeviceListModel *deviceList = (DeviceListModel *)responseObject;

        if (deviceList && deviceList.list && deviceList.list.count) {
            _deviceList = deviceList.list;
            
            [self updateTitle];
        } else {
            if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
                _lblConnectHub.text = @"등록된 장치가 없습니다";
                _imgvHubAlert.hidden = YES;
                
                _imgvConnectHub.image = [UIImage imageNamed:@"img_1depth_nodevice"];
            }
        }
        [_collectionView reloadData];
                                               
        [self requestPollingDevicesStatusInBackground];
                                               
        //refresh controller
        if (self.refreshController && self.refreshController.refreshState == JYRefreshStateLoading) {
            [self.refreshController stopRefreshWithAnimated:YES completion:nil];
        }

    } failure:^(id errorObject) {
        [self releaseDevicesTimer];
        
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];
}


//디바이스 상태를 3초마다 갱신함.
- (void)requestPollingDevicesStatusInBackground {
    
    //schedul timer.
    if (!_devicesBackgroundTimer) {
        _devicesBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(requestDeviceList:) userInfo:@NO repeats:YES];
    }
}

- (void)updateTitle {
    
    _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
    
    if (![JDFacade facade].loginUser.hasHomeHub) {//홈허브 아이디가 없는 경우,
        
        [_mainView bringSubviewToFront:_addHubContainerView];
        _addHubContainerView.hidden = NO;
        _collectionView.hidden = YES;
        _btnOption.hidden = YES;
        
        if (![JDFacade facade].loginUser.homegrpId) {//연결한 적이 없음
            _lblConnectHub.text = @"초대를 받아서\n시작해 보세요";
            _imgvHubAlert.hidden = YES;
            
            _imgvConnectHub.image = [UIImage imageNamed:@"img_1depth_invitation"];

            _lblLeaveAccount.hidden = YES;
            _lblSimpleMemberInfo.hidden = YES;
            
        } else {//홈허브가 삭제됨.
            _lblTitle.text = @"     홈허브 삭제됨";
            
            _imgvHubAlert.hidden = NO;
            _lblConnectHub.text = @"홈허브를 다시 연결하려면\n고객센터에 문의해주세요";
            
            _imgvConnectHub.image = [UIImage imageNamed:@"img_things_homehub_img_hubdelete_cscenter"];
            
            if ([JDFacade facade].loginUser.level < 90) {//일반 유저일 경우,
                _imgvConnectHub.image = [UIImage imageNamed:@"img_things_homehub_img_hubdelete_wait"];
                _lblLeaveAccount.hidden = NO;
                _lblSimpleMemberInfo.hidden = NO;
                [_lblLeaveAccount setUnderLine:_lblLeaveAccount.text];
                
                if (!_lblLeaveAccount.touchHandler) {
                    [_lblLeaveAccount addTouchEventHandler:^(id label) {
                        [self leaveHomegroup];
                    }];
                }
            }
        }
        
    } else {
        
        if (![JDFacade facade].loginUser.isHomehubOnline) {
            _imgvHubAlert.hidden = NO;
            _lblTitle.text = @"     홈허브 오프라인";
            [_lblTitle setColor:kUITextColor01 text:_lblTitle.text];
        } else {
            _imgvHubAlert.hidden = YES;
        }
        
        [_mainView bringSubviewToFront:_collectionView];
        _addHubContainerView.hidden = YES;
        _collectionView.hidden = NO;
        _btnOption.hidden = NO;
        
    }
    
    if ([_lblTitle.text rangeOfString:@"장치 전체"].location != NSNotFound) {
         [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
    }
}

- (void)leaveHomegroup {
    HomeMemberViewController *vc = [[HomeMemberViewController alloc] init];
    [vc leaveHomegroup];
}

- (void)matchDeviceListWithOnCommandsDevices {
    
    for (DeviceModel *rdevice in _commandArray) {
        DeviceModel *matchedDevice = [_deviceList objectByUsingPredicateFormat:@"deviceId == %@", rdevice.deviceId]; //일치하는 디바이스가 있을 경우, 추가하지 않음.
//        DeviceModel *matchedDevice = [_deviceList objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", rdevice.deviceId, rdevice.nodeId]; //일치하는 디바이스가 있을 경우, 추가하지 않음.
        if (matchedDevice) {
            matchedDevice.isRequesting = rdevice.isRequesting;
            matchedDevice.requestTime = rdevice.requestTime;
        }
    }
    
//    [_collectionView reloadData];
}

- (void)releaseDevicesTimer {
    if (_devicesBackgroundTimer) {
        [_devicesBackgroundTimer invalidate];
        _devicesBackgroundTimer = nil;
    }
}

#pragma mark - UICollectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = _deviceList.count;
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *rcell = nil;
    
    if (indexPath.row < _deviceList.count) {
        ThingsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
        
        DeviceModel *device =_deviceList[indexPath.row];
        cell.indexPath = indexPath;
        
        cell.lblDeviceName.text = device.deviceName;
        
        [cell.btnDevice sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] forState:UIControlStateNormal
                          placeholderImage:nil options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [cell layoutIfNeeded];
        }];
        [cell.btnDevice addTarget:self action:@selector(btnDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
        cell.btnDevice.value = indexPath;
        
        //커맨드 클래스 뷰를 초기화함.
        [[cell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            UIView *subview = (UIView *)obj;
            [subview removeFromSuperview];
        }];

        cell.pcontainerView.hidden = !device.isRequesting;
        if (!cell.pcontainerView.hidden) {
            [cell startProgressAni];
        } else {
            [cell stopProgressAni];
        }
        
        //허브 On-Off line check
        cell.lblDeviceStatus.hidden = !([[JDFacade facade].loginUser.homehubOnlineState isEqualToString:@"OFF"] || [device.onlineState isEqualToString:@"OFF"]);
        cell.controlContainer.hidden = !cell.lblDeviceStatus.hidden;

        if (!cell.controlContainer.hidden) {//커맨드 클래스 타입별 컨트롤 호출
            
            [cell.btnDevice setBackgroundImage:[device backgroundImageForMandatary:device.contentValue] forState:UIControlStateNormal];
            
            CommandClassControlView *controlView = [CommandClassControlView viewForCommandClass:device.cmdclsType];
            controlView.device = device;
            
            cell.controlContainer.hidden = !controlView;
            if (!cell.controlContainer.hidden) {
                UIView *superview = cell.controlContainer;
                [superview addSubview:controlView];
                
                controlView.width = IS_IPHONE_6P ? 98.0f : 120;
                [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(controlView.frame.size);
                    make.center.equalTo(superview);
                }];
            }
            
        } else {
            cell.lblDeviceStatus.text = @"OFFLINE";
            cell.lblDeviceStatus.textColor = kUITextColor01;
            
            [cell.btnDevice setBackgroundImage:[UIImage imageNamed:@"img_thing_icon_bg_default"] forState:UIControlStateNormal];
        }
        
        NSInteger mod = indexPath.row % _deviceFlag;
        cell.vline.hidden = mod == (_deviceFlag - 1);
        
        rcell = cell;
    }
    
    return rcell;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
//    return CGSizeZero;
//
//    //FIXME : 권한 추가
////    if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode)  {//마스터 권한이 아니거나, 짝수가 아닐 경우
//    if (_deviceList.count % 2 == 1 || _isDeleteMode)  {//마스터 권한이 아니거나, 짝수가 아닐 경우
//    }
//
//    return CGSizeMake(IPHONE_WIDTH, 160.0f);
//}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(IPHONE_WIDTH / _deviceFlag, 177.0f);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < _deviceList.count) {//디바이스인 경우,
        DeviceModel *device = _deviceList[indexPath.row];
        
        ThingsDetailViewController *vc = (ThingsDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsDetailViewController" storyboardName:@"Things"];
        vc.refDevice = device;
        [self presentViewController:vc animated:YES completion:nil];
    }
}


#pragma mark - UI Events

- (IBAction)btnOptionTouched:(id)sender {
    [self toggleOptions:sender];
}


#pragma mark - MemoryWarning

- (void)viewWillDisappear:(BOOL)animated {
    
    if (_deviceCommandsBackgroundTimer) {
        [_deviceCommandsBackgroundTimer invalidate];
        _deviceCommandsBackgroundTimer = nil;
    }
    
    [self releaseDevicesTimer];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)btnDeviceTouched:(id)sender {
    CustomButton *btn = (CustomButton *)sender;
    
//    [_collectionView selectItemAtIndexPath:btn.value animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    [self collectionView:_collectionView didSelectItemAtIndexPath:btn.value];
}

@end
