//
//  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 "UIImageView+WebCache.h"
#import "CustomLabel.h"
#import "CustomImageView.h"
#import "CustomTextField.h"
#import "CustomButton.h"
#import "JYRefreshController.h"
#import "WYPopoverController.h"
#import "ImageUtil.h"

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

#import "ThingsViewController.h"
#import "UIButton+WebCache.h"
#import "ThingsAddViewController.h"
#import "ThingsAddStartViewController.h"

#define kfThingsTableViewCellHeight 100.0f

@interface ThingsCollectionViewCell () {
    
}

@property (weak, nonatomic) NSIndexPath *indexPath;

@end

@implementation ThingsCollectionViewCell

- (void)awakeFromNib {
    _btnDelete.hidden = YES;
}
@end

@implementation ThingsAddCollectionViewCell

@end

@implementation ThingsCollectionFooterView

@end

@interface ThingsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> {
    NSMutableArray<DeviceModel> *_deviceList;
    NSString *_pagingId, *_pagingType;
    BOOL _isNotFirstLoading, _isDeleteMode;
}

@property (strong, nonatomic) JYPullToRefreshController *refreshController;
@property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;

@end


#pragma mark - Class Definition

@implementation ThingsViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];
    [[JDFacade facade] showFlex];
}

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

- (void)initUI {
    
    //set tableview option
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = kUIBgColor01;
    
    if ([JDFacade facade].loginUser.homehubDeviceId && ![[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
        [self setThingsPopoverOptions];
    }
    
    //set refresh controls
//    __weak typeof(self) weakSelf = self;
//    self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
//    self.refreshController.pullToRefreshHandleAction = ^{
//        [weakSelf requestPredefinedRulesRecently];
//    };
//    
//    self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
//    self.loadMoreController.pullToLoadMoreHandleAction = ^{
//        [weakSelf requestPredefinedRulesOlder];
//    };
}

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

    [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
                                      @"iconName": @"tp_01_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
    
    if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
        [_mainView bringSubviewToFront:_addHubContainerView];
        _collectionView.hidden = YES;
        
    } else {
        
        [_mainView bringSubviewToFront:_collectionView];
        _addHubContainerView.hidden = YES;
        _collectionView.hidden = NO;
        
        [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
    }
}

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

- (void)updateDevice:(DeviceModel *)device {
//    if ([device.deviceId isEqualToString:ldevice.deviceId]) {
//        ldevice.cmdclsValue = device.cmdclsValue;
//        ldevice.contentValue = device.contentValue;
//        break;
//    }
}

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

- (void)toggleEditMode {
    _isDeleteMode = !_isDeleteMode;
    
    [_collectionView reloadData];
    
    _constraintEditModeRight.constant = _isDeleteMode ? 0 : -_editModeView.width;
    
    [UIView animateWithDuration:kfAnimationDur animations:^{
        [self.view layoutIfNeeded];
    }];
}


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

#pragma mark - Main Logic

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

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

- (void)requestDeviceList {

    //parameters
    NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
                                @"paging_type": _pagingType ? _pagingType : ksEmptyString};

    NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];

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

        DeviceListModel *deviceList = (DeviceListModel *)responseObject;

        if (deviceList && deviceList.list && deviceList.list.count) {
            _deviceList = deviceList.list;
            _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
            [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
            
        } else {
            if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
//                    NoContentView *noContentView = [NoContentView viewFromNib];
//                    _tableView.tableFooterView = noContentView;
            }
        }
        
        [_collectionView reloadData];

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

#pragma mark - UICollectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
    NSInteger count = _deviceList.count % 2 == 0 ? _deviceList.count : _deviceList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
    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.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.lblDeviceName.text = device.deviceName;
        
        cell.btnDelete.hidden = !_isDeleteMode;
        cell.lblDeviceStatus.hidden = _isDeleteMode;
        if (!cell.lblDeviceStatus.hidden) {
            NSString *deviceStatus = [[JDFacade facade].loginUser.homehubOnlineState isEqualToString:@"OFF"] ? @"OFFLINE" : device.contentValue;
            cell.lblDeviceStatus.text = deviceStatus;
        } else {
            cell.btnDelete.value = device;
            if (![cell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
                [cell.btnDelete addTarget:self action:@selector(btnDeleteTouched:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
        
        rcell = cell;
        
    } else {//디바이스 추가 옵션
        ThingsAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
        
        if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
            [cell.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
        }
        rcell = cell;
    }
    
    return rcell;
}

- (void)btnDeleteTouched:(id)sender {
    CustomButton *btn = (CustomButton *)sender;
    
    DeviceModel *device = (DeviceModel *)btn.value;
    
    ThingsAddStartViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddStartViewController" storyboardName:@"Things"];
    vc.removableDevice = device;
    if (device.groupName && ![device.groupName isEmptyString]) {
        vc.removableGroups = [_deviceList filteredArrayUsingPredicateFormat:@"groupName == %@", device.groupName]; //group devices
    }
    
    vc.providesPresentationContextTransitionStyle = YES;
    vc.definesPresentationContext = YES;
    
    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    
    [self presentViewController:vc animated:NO completion:nil];
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{

    ThingsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
                                                                                    withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
    if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
        [footerView.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
    }
    return footerView;
}

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

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
        return CGSizeMake(212.0f, 197.0f);
    } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
        return CGSizeMake(187.5, 197.0f);
    }
    return CGSizeMake(160.0f, 197.0f);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < _deviceList.count) {
        ThingsCollectionViewCell *cell = (ThingsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
        DeviceModel *device = _deviceList[indexPath.row];
    }
}


#pragma mark - UI Events
- (void)btnAddHubTouched:(id)sender {
    
    [[JDFacade facade] gotoHomeHubRegistration];
}

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

- (IBAction)btnCloseOnEditModeTouched:(id)sender {
    [self toggleEditMode];
}

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

@end
