#import "DevListTableViewController.h"
#import "AppDelegate.h"

#ifdef MPOS_XCFRAMEWORK
@import frameworkMPosSDK;

#else
#import "MPosLookup.h"
#import "UDPSetting.h"

#endif

@interface DevListTableViewController ()
@property NSArray<MPosDeviceInfo*>* btDevices;
@property NSArray<MPosDeviceInfo*>* bleDevices;
@property NSArray<MPosDeviceInfo*>* networkDevices;
@property NSArray<MPosDeviceInfo*>* usbDevices;
@property UIActivityIndicatorView *spinner;
@end

@implementation DevListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.btDevices = [NSArray array];
    self.networkDevices = [NSArray array];
    self.bleDevices = [NSArray array];
    self.usbDevices = [NSArray array];
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"deviceListCell"];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    __weak DevListTableViewController* weakSelf  = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        __strong DevListTableViewController* strongSelf = weakSelf;
        [strongSelf showSpinnerAtFooter];
        MPosLookup* lookup = [MPosLookup new];
        strongSelf.btDevices = [lookup getBluetoothDevices];
//        strongSelf.networkDevices = [lookup getNetworkDevices];
        strongSelf.networkDevices = [UDPSetting getNetworkInfos];
        strongSelf.bleDevices = [lookup getBLEDevices];
        strongSelf.usbDevices = [lookup getUSBDevices];
        dispatch_async(dispatch_get_main_queue(),^{
            [strongSelf.tableView reloadData];
            [strongSelf hideSpinnerAtFooter];
        });
    });
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberofRow = 0;
    switch (section) {
        case 0: numberofRow = [_btDevices count];      break;
        case 1: numberofRow = [_networkDevices count]; break;
        case 2: numberofRow = [_bleDevices count];     break;
        case 3: numberofRow = [_usbDevices count];     break;
    }
    NSLog(@"numberofRow = %ld", (long)numberofRow);
    return numberofRow;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0: return @"Bluetooth classic";
        case 1: return @"Network";
        case 2: return @"BLE";
        case 3: return @"USB";
            break;
        default:
            return @"";
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@", [NSString stringWithFormat:@"cellForRowAtIndexPath (%ld)(%ld)",(long)indexPath.section, (long)indexPath.row]);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deviceListCell" forIndexPath:indexPath];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"deviceListCell"];
    MPosDeviceInfo* device = nil;
    if(indexPath.section == 0 /* Bluetooth */){
        if([_btDevices count]){
            device = _btDevices[indexPath.row];
            [cell textLabel].text = device.name;
            [cell detailTextLabel].text = [NSString stringWithFormat:@"  %@", device.serialNumber];
        }
    }else if(indexPath.section == 1 /* Network */){
        if([_networkDevices count]){
            device = _networkDevices[indexPath.row];
            [cell textLabel].text = [device.name length] > 0 ? device.name : @"Network Device";
            [cell detailTextLabel].text = [NSString stringWithFormat:@"  %@:%@", device.address, device.portNumber];
        }
    }else if(indexPath.section == 2 /* BLE */){
        if([_bleDevices count]){
            device = _bleDevices[indexPath.row];
            [cell textLabel].text = [device.name length] > 0 ? device.name : @"BLE Device";
            [cell detailTextLabel].text = [NSString stringWithFormat:@"  %@", device.macAddress];
        }
    }else if(indexPath.section == 3 /* USB */){
        if([_usbDevices count]){
            device = _usbDevices[indexPath.row];
            [cell textLabel].text = device.name;
            [cell detailTextLabel].text = [NSString stringWithFormat:@"  %@", device.serialNumber];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@", [NSString stringWithFormat:@"didSelectRowAtIndexPath (%ld)(%ld)",(long)indexPath.section, (long)indexPath.row]);
    AppDelegate* ad = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    MPosDeviceInfo* device = nil;
    ad.networkDevice = nil;
    if(indexPath.section == 0 /* Bluetooth*/){
        device = _btDevices[indexPath.row];
        ad.conntectionType = device.interfaceType;
        ad.deviceName = device.name;
        ad.connectionString = device.serialNumber;
    }else if(indexPath.section == 1 /* Network */){
        device = _networkDevices[indexPath.row];
        ad.conntectionType = device.interfaceType;
        ad.deviceName = _networkDevices[indexPath.row].name;
        ad.connectionString = [NSString stringWithFormat:@"%@:%@", device.address, device.portNumber];
        ad.networkDevice = device;
    }
    else if(indexPath.section == 2 /* BLE */){
        device = _bleDevices[indexPath.row];
        ad.conntectionType = device.interfaceType;
        ad.deviceName = _bleDevices[indexPath.row].name;
        ad.connectionString = [NSString stringWithFormat:@"%@", device.macAddress];
    }
    else if(indexPath.section == 3 /* USB */){
        device = _usbDevices[indexPath.row];
        ad.conntectionType = device.interfaceType;
        ad.deviceName = device.name;
        ad.connectionString = device.serialNumber;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) hideSpinnerAtFooter {
    __weak DevListTableViewController* weakSelf  = self;
    dispatch_async(dispatch_get_main_queue(),^{
        __strong DevListTableViewController* strongSelf = weakSelf;
        if(strongSelf.spinner)
            [strongSelf.spinner stopAnimating];
    });
}

-(void) showSpinnerAtFooter {
    __weak DevListTableViewController* weakSelf  = self;
    dispatch_async(dispatch_get_main_queue(),^{
        __strong DevListTableViewController* strongSelf = weakSelf;
        strongSelf.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        strongSelf.spinner.transform = CGAffineTransformMakeScale(1.5, 1.5);
        strongSelf.tableView.tableFooterView = self.spinner;
        [strongSelf.spinner startAnimating];
    });
}

@end
