//
//  ThingsAddWallpadSubViewController.m
//  kneet
//
//  Created by Jason Lee on 6/30/15.
//  Copyright (c) 2015 ntels. All rights reserved.
//
#import "RequestHandler.h"
#import "JDObject.h"
#import "SelectButton.h"
#import "DeviceModel.h"

#import "ThingsAddWallpadSubViewController.h"
#import "CustomLabel.h"
#import "CustomButton.h"
#import "WallpadSubDevicePopupView.h"
#import "ThingsAddWallPadSubTypeViewController.h"
#import "ImageUtil.h"


@interface ThingsAddWallpadSubViewController () {
    UIImage *_bgCellImage1, *_bgCellImage2;
}
@end


#pragma mark - Class Definition

@implementation ThingsAddWallpadSubViewController


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

    [self initUI];
    [self prepareViewDidLoad];
}

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

    self.title = NSLocalizedString(@"새 장치 추가",nil);
}

- (void)initUI {
    
    

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

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];

    self.tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
    
    UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
    _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
    _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
    
    //Localization
    _lblTitle.text = NSLocalizedString(@"현재 등록된 COMMAX 월패드에\n더 많은 장치를 연결해보세요", @"현재 등록된 COMMAX 월패드에\n더 많은 장치를 연결해보세요");
    [_btnAddSubDevice setTitle:NSLocalizedString(@"장치추가", @"장치추가") forState:UIControlStateNormal];
    
    _lblManualDesc.text = NSLocalizedString(@"장치 종류별 연결방법은\n매뉴얼을 참고해주세요", @"장치 종류별 연결방법은\n매뉴얼을 참고해주세요");
    [_btnManual setTitle:NSLocalizedString(@"매뉴얼 보기", @"매뉴얼 보기") forState:UIControlStateNormal];
}

- (void)prepareViewDidLoad {
    
    _btnWallpads.dataDict = [[SortDictionary alloc] initWithDictionary:_wallpads];
    _btnWallpads.selectedValue = [_wallpads allKeys].firstObject;
}

#pragma mark - Main Logic

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
        [self btnAddSubDeviceTouched:nil];
    }
}


#pragma mark - UI Events

- (IBAction)btnAddSubDeviceTouched:(id)sender {
    
    WallpadSubDevicePopupView *popup = [[WallpadSubDevicePopupView alloc] initFromNib];
    popup.wallpadId = _btnWallpads.selectedValue;
    
    [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
    
        if (popup.foundedDevice) {            
            if (![popup.foundedDevice.deviceProfileId isEqualToString:ksDeviceProfileIdUnknown]) {//장치 타입이 있을 경우,
                //goto complete
                UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
                [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
                
            } else {//UNJDOWN 타입일 경우,
                //goto choose device type
                ThingsAddWallPadSubTypeViewController *wvc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddWallPadSubTypeViewController" storyboardName:@"Things"];
                wvc.device = popup.foundedDevice;
                [[JDFacade facade] presentViewControllerByPush:wvc pvc:self];
            }
        }
    }];
}

- (IBAction)btnManualTouched:(id)sender {
	
}


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

@end
