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

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

#import "CustomLabel.h"
#import "CustomButton.h"
#import "CustomImageView.h"

#import "ThingsAddWallpadSubViewController.h"
#import "ThingsAddViewController.h"
#import "ThingsAddStartViewController.h"

@implementation ThingsAddTableViewCell

@end

@interface ThingsAddViewController () <UITableViewDataSource, UITableViewDelegate> {
    NSArray *_addableDeviceList;
}
@end


#pragma mark - Class Definition

@implementation ThingsAddViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}

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

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

- (void)prepareViewDidLoad {

    _addableDeviceList = @[@{@"deviceName" : @"다원 스마트 플러그",
                             @"manufacturer" : @"다원",
                             @"nodeName" : @"스마트 플러그",
                             @"deviceKey": @"DAOWN"},
                           
                           @{@"deviceName" : @"다우앤텍 멀티 센서",
                             @"manufacturer" : @"다우엔텍",
                             @"nodeName" : @"멀티센서",
                             @"deviceKey": @"DAWOO"}];
    
}

#pragma mark - Main Logic


#pragma mark - UITableView DataSource & Delegate

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 220;
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ThingsAddTableViewCell *cell = (ThingsAddTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];

    NSDictionary *addableDevice = _addableDeviceList[indexPath.row];
    cell.lblDeviceName.text = addableDevice[@"deviceName"];
    cell.lblVendor.text = addableDevice[@"manufacturer"];
    
//    if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
//        [cell.btnAdd addTarget:self action:@selector(btnAddDeviceTouched:) forControlEvents:UIControlEventTouchUpInside];
//    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *addableDevice = _addableDeviceList[indexPath.row];
    
    ThingsAddStartViewController *vc = (ThingsAddStartViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddStartViewController" storyboardName:@"Things"];
    vc.addableDevice = addableDevice;

    vc.providesPresentationContextTransitionStyle = YES;
    vc.definesPresentationContext = YES;
    
    [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    
    [self presentViewController:vc animated:NO completion:nil];
}

#pragma mark - TableView Delegate
- (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
- (void)btnAddDeviceTouched:(id)sender {
    
}

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

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



@end
