//
//  HomeHubViewController.m
//  kneet2
//
//  Created by Jason Lee on 10/28/15.
//  Copyright © 2015 ntels. All rights reserved.
//

#import "HomeHubViewController.h"
#import "RequestHandler.h"
#import "DeviceModel.h"
#import "CustomTableView.h"

@implementation HomeHubTableViewCell

@end

@implementation HomeHubRegistTableViewCell

@end

@implementation HomeHubSecureTableViewCell


@end

@implementation HomeHubInfoTableViewCell

@end

@interface HomeHubViewController () {
    DeviceModel *_homeHubDevice;
}
@end


#pragma mark - Class Definition

@implementation HomeHubViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {
    [self initTableViewAsDefaultStyle:_tableView];
}

- (void)prepareViewDidLoad {
    [self requestHubStatus];
}

#pragma mark - Main Logic

- (void)requestHubStatus {
    
    NSString *path = [NSString stringWithFormat:API_GET_HOMEHUB_STATUS];
    
    [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[DeviceModel class] completion:^(id responseObject) {
        if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
            return;
        }
        
        DeviceModel *fetchedDevice = (DeviceModel *) responseObject;
        
        if (fetchedDevice) {//API 성공,
            _homeHubDevice = fetchedDevice;
        }
        
        [_tableView reloadData];
        
    } failure:^(id errorObject) {
        JDErrorModel *error = (JDErrorModel *)errorObject;
        [[JDFacade facade] alert:error.errorMessage];
    }];

}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 0;
    
    if (indexPath.row == 0) {
        height = 110;
    } else if (indexPath.row == 1) {
        height = 110;
    } else if (indexPath.row == 2) {
        height = 195;
    } else if (indexPath.row == 3) {
        height = 150;
    }
    
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;
    
    if (indexPath.row == 0) {
        HomeHubTableViewCell *tcell = (HomeHubTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubCellIdentifier"];
        
        if (_homeHubDevice) {
            tcell.lblStatus.text = [_homeHubDevice.onlineState isEqualToString:@"ON"] ? @"온라인" : @"오프라인";
        } else {
            tcell.lblStatus.text = @"오프라인";
        }
        
        cell = tcell;
    } else if (indexPath.row == 1) {
        HomeHubRegistTableViewCell *tcell = (HomeHubRegistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RegistCellIdentifier"];
        
        if (_homeHubDevice) {
            tcell.lblDate.text = [CommonUtil formattedDate3:_homeHubDevice.createDatetime];
        } else {
            tcell.lblDate.text = ksEmptyString;
        }
        
        cell = tcell;
    } else if (indexPath.row == 2) {
        HomeHubSecureTableViewCell *tcell = (HomeHubSecureTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SecureCellIdentifier"];
        
        [tcell.lblCaution setLineSpacing:10];
        [tcell.lblCaution setColor:kUITextColor02 text:@"아래의 경우에 보안키가 필요합니다."];
        
        if (![tcell.btnSecure actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
            [tcell.btnSecure addTarget:self action:@selector(btnSecureTouched:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        cell = tcell;
        
    } else if (indexPath.row == 3) {
        HomeHubInfoTableViewCell *tcell = (HomeHubInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"InfoCellIdentifier"];
        
        cell = tcell;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
}


#pragma mark - UI Events
- (IBAction)btnSecureTouched:(id)sender {
    
}

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


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



@end