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

#import "DeviceModel.h"
#import "CustomLabel.h"
#import "CustomButton.h"

#import "ThingsInfoViewController.h"

#define kfThingsInfoTableViewCellHeight 64.0f

@implementation ThingsInfoTableViewCell

- (void)awakeFromNib {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

@end

@interface ThingsInfoViewController () <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *_dataArray;
}
@end


#pragma mark - Class Definition

@implementation ThingsInfoViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}


- (void)initUI {

    //set tableview option
    _tableView.dataSource = self;
    _tableView.delegate = self;

    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];

    _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
    
    //Localization
    [_btnClose setTitle:NSLocalizedString(@"닫기", @"닫기") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {
    _dataArray = [[NSMutableArray alloc] init];

    if (_deviceDetail.prdName && ![_deviceDetail.prdName isEmptyString]) {
        [_dataArray addObject:@{@"title": NSLocalizedString(@"제품명", @"제품명"), @"content": _deviceDetail.prdName}];
    }

    if (_deviceDetail.deviceModelId && ![_deviceDetail.deviceModelId isEmptyString]) {
        [_dataArray addObject:@{@"title": NSLocalizedString(@"모델넘버", @"모델넘버"), @"content": _deviceDetail.deviceModelId}];
    }

    if (_deviceDetail.deviceMfId && ![_deviceDetail.deviceMfId isEmptyString]) {
        [_dataArray addObject:@{@"title": NSLocalizedString(@"제조사", @"제조사"), @"content": _deviceDetail.deviceMfId}];
    }

    if (_deviceDetail.firmwareVersion && ![_deviceDetail.firmwareVersion isEmptyString]) {
        [_dataArray addObject:@{@"title": NSLocalizedString(@"펌웨어", @"펌웨어"), @"content": _deviceDetail.firmwareVersion}];
    }

    if (_deviceDetail.createDatetime && ![_deviceDetail.createDatetime isEmptyString]) {
        [_dataArray addObject:@{@"title": NSLocalizedString(@"등록일시", @"등록일시"), @"content": _deviceDetail.createDatetime}];
    }

    if (_deviceDetail.deviceProtocolType && ![_deviceDetail.deviceProtocolType isEmptyString]) {
        [_dataArray addObject:@{@"title": NSLocalizedString(@"연결타입", @"연결타입"), @"content": _deviceDetail.deviceProtocolType}];
    }
    
    if (_deviceDetail.relateDevice && _deviceDetail.relateDevice.count) {

        NSString *title = nil;
        if ([_deviceDetail.deviceType isEqualToString:ksDeviceTypeSlave]) {
            title = NSLocalizedString(@"상위 장치", @"상위 장치");
        } else if ([_deviceDetail.deviceType isEqualToString:ksDeviceTypeControllerPrimary]) {
            title = NSLocalizedString(@"하위 장치", @"하위 장치");
        }
        
        NSMutableString *rdeviceString = [[NSMutableString alloc] init];
        for (DeviceModel *rdevice in _deviceDetail.relateDevice) {
            NSString *prefix = [rdeviceString isEmptyString] ? ksEmptyString : @", ";
            [rdeviceString appendFormat:@"%@%@", prefix, rdevice.deviceName];
        }
        
        [_dataArray addObject:@{@"title": title, @"content": rdeviceString}];
    }
}

#pragma mark - Main Logic



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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}


- (CGFloat)calculateContent:(NSString *)content {
    if ([content isEmptyString]) {
        return 0.0f;
    }

    CGFloat width = IPHONE_WIDTH - 90;
    UIFont *lfont = [UIFont boldSystemFontOfSize:kUIFontSize04];
    CGFloat nheight = [CommonUtil getSizeFromString:content font:lfont width:width].height;
    nheight = nheight < 22 ? 22.0f : fabs(nheight) + 7.0f; //adjust padding
    return nheight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = kfThingsInfoTableViewCellHeight;

    NSDictionary *dic = _dataArray[indexPath.row];
    CGFloat contentHeight = [self calculateContent:dic[@"content"]];
    height = 42.0f + contentHeight; //(title.height + padding)

    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"CellIdentifier";

    ThingsInfoTableViewCell *cell = (ThingsInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[ThingsInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *dic = _dataArray[indexPath.row];
    cell.lblTitle.text = dic[@"title"];
    cell.lblContent.text = dic[@"content"];

    return cell;
}

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


- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


#pragma mark - UI Events



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

@end
