//
//  MultiHomeHubViewController.m
//  OneCable
//
//  Created by nComz on 2017. 5. 11..
//  Copyright © 2017년 ntels. All rights reserved.
//

#import "MultiHomeHubViewController.h"
#import "RequestHandler.h"
#import "DeviceModel.h"
#import "CustomTableView.h"
#import "CustomButton.h"
#import "Reachability.h"

#import "HomeHubViewController.h"

/**
 
 네트워크, 동글 On / Off 이미지
    On  : img_homehub_list_connect
    Off : img_homehub_list_notconnect
 
 업데이트 On / Off 이미지
    On  : img_homehub_list_update_on
    Off : img_homehub_list_update_off
 **/
@interface MultiHomeHubViewController ()
{
    UIImage *_imgOnline, *_imgOffLine, *_imgUptOn, *_imgUptOff;
}

@end

@implementation MultiHomeHubTableViewCell
- (void)didMoveToSuperview {
    
//    [self layoutIfNeeded];
}
@end

@implementation MultiHomeHubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self prepareViewDidLoad];
    
}

- (void)initUI {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    statusBar.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController.navigationBar setHidden:YES];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
    [self initTableViewAsDefaultStyle:_tableView];
}

- (void)prepareViewDidLoad {
    
    _imgOnline = [UIImage imageNamed:@"img_homehub_list_connect"];
    _imgOffLine = [UIImage imageNamed:@"img_homehub_list_notconnect"];
    _imgUptOn = [UIImage imageNamed:@"img_homehub_list_update_on"];
    _imgUptOff = [UIImage imageNamed:@"img_homehub_list_update_off"];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    NSLog(@"multi home hub view will appear");
    
    [_tableView reloadData];
    
//    UIViewController *vc = [CommonUtil currentViewController];
//    NSLog(@"CommonUtil currentViewController : %@", [CommonUtil currentViewController]);
}


- (void)operationComplite {
    
    NSLog(@"multi home hub operation complite");
    
    if (_closeWhenOperationComplite)
        [self btnCloseTouched:nil];
    
}

#pragma mark - TableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[JDFacade facade].loginUser.deviceList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = 151.0f;
    
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DeviceModel *info = [[JDFacade facade].loginUser.deviceList objectAtIndex:indexPath.row];
    
    MultiHomeHubTableViewCell *cell = (MultiHomeHubTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"MultiHubCellIdentifier"];
    
    cell.lblTitle.text = [NSString stringWithFormat:@"%@", info.deviceName];
    cell.btnHubDetail.tag = indexPath.row;
    
    // 네트워크 연결상태 확인
    if ([info isDeviceConn]) {
        cell.imgNetwork.image = _imgOnline;
    } else {
        cell.imgNetwork.image = _imgOffLine;
    }

    // Z-Wave 동작상태 확인
    if (info.isDeviceOnlined) {
        cell.imgDongle.image = _imgOnline;
    } else {
        cell.imgDongle.image = _imgOffLine;
    }
    
    if ([info isUpdateNeed]) {
        cell.imgUpdate.image = _imgUptOn;
    } else {
        cell.imgUpdate.image = _imgUptOff;
    }
        
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
     [self showHomeHubDetail:indexPath];
}


- (IBAction)btnHubDetail:(id)sender {
    UIButton *senderButton = (UIButton *)sender;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:senderButton.tag inSection:0];
    
    [self showHomeHubDetail:indexPath];
}


-(void)showHomeHubDetail:(NSIndexPath *)indexPath
{
    DeviceModel *info = [[JDFacade facade].loginUser.deviceList objectAtIndex:indexPath.row];
    
    HomeHubViewController *vc = (HomeHubViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubViewController" storyboardName:@"Main"];
    vc.selectHub = info;
    
    [self.navigationController pushViewController:vc animated:YES];
    

//    vc.providesPresentationContextTransitionStyle = YES;
//    vc.definesPresentationContext = YES;
//    
//    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
//    

    //    UIViewController *pvc = self.presentingViewController;
//    [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
//        [pvc presentViewController:vc animated:YES completion:nil];
//    }];
   
//    UIViewController *pvc = self.presentingViewController;
//    [self dismissViewControllerAnimated:NO completion:^{
//        [pvc presentViewController:vc animated:YES completion:nil];
//    }];
//
//    [self presentViewController:vc animated:YES completion:nil];
}


- (IBAction)btnCloseTouched:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - SocketService
-(void) receiveSocketData:(NSNotification *)notification {
    
    SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
    //NSLog(@"receiveSocketData Result : %@", result);
    
    [self processSocket:result];
}

- (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result {
    
    //NSLog(@"receiveSocketData Result : %@", result);
    
    [self processSocket:result];
}

- (void)processSocket:(SocketModel*)result {
    
    if (EQUALS(result.messageType, MSG_TYPE_DEVICE_CONN) ||
        EQUALS(result.messageType, MSG_TYPE_DEVICE_CONTENT)) {
        
        [_tableView reloadData];
    }
}


@end
