#import "DevListTableViewController.h"
#import "TestMainViewController.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 NSMutableArray<MPosDeviceInfo*>* devices;
@property (weak) IBOutlet NSProgressIndicator *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.devices = [NSMutableArray array];
    self.tableView.target = self;
    self.tableView.doubleAction = @selector(tableViewDoubleAction:);
}

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

-(void)setIsHiddenSpinner:(BOOL)isHidden {
    __weak DevListTableViewController* weakSelf  = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        __strong DevListTableViewController* strongSelf = weakSelf;
        [strongSelf.spinner setHidden:isHidden];
        if (isHidden) {
            [strongSelf.spinner stopAnimation:nil];
        }else{
            [strongSelf.spinner startAnimation:nil];
        }
    });
}

- (IBAction)onClose:(id)sender {
    [self tableViewDoubleAction:nil];
}

-(void)tableViewDoubleAction:(id)sender {
    [self dismissViewController:self];
}

#pragma mark - NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return _devices.count;
}

#pragma mark - NSTableViewDelegate
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    if(row > _devices.count) {
        return nil;
    }
    NSString* identifier = @"";
    NSString* text = @"";
    MPosDeviceInfo* device = _devices[row];
    if (tableColumn == tableView.tableColumns[0]) {
        identifier = @"Interface";
        switch(device.interfaceType) {
            case MPOS_INTERFACE_WIFI:
            case MPOS_INTERFACE_ETHERNET:
                text = @"Network";
                break;
            case MPOS_INTERFACE_BLUETOOTH:
                text = @"Bluetooth";
                break;
            case MPOS_INTERFACE_BLE:
                text = @"BLE";
                break;
            case MPOS_INTERFACE_USB:
                text = @"USB";
                break;
            default:
                text = @"Unknown";
                break;
        }
    } else if (tableColumn == tableView.tableColumns[1]) {
        identifier = @"Info";
        switch(device.interfaceType) {
            case MPOS_INTERFACE_WIFI:
            case MPOS_INTERFACE_ETHERNET:
                text = [NSString stringWithFormat:@"%@ (%@:%@)", [device.name length] > 0 ? device.name : @"Network Device", device.address, device.portNumber];
                break;
            case MPOS_INTERFACE_BLUETOOTH:
                text = [NSString stringWithFormat:@"%@ (%@)", device.name, device.macAddress];
                break;
            case MPOS_INTERFACE_BLE:
                text = [NSString stringWithFormat:@"%@ (%@)", [device.name length] > 0 ? device.name : @"BLE Device", device.macAddress];
                break;
            case MPOS_INTERFACE_USB:
                text = [NSString stringWithFormat:@"%@ (%@)", [device.name length] > 0 ? device.name : @"USB Device", device.address];
                break;
            default:
                text = @"Unknown";
                break;
        }
    }
    
    NSTableCellView* cell = [tableView makeViewWithIdentifier:identifier owner:nil];
    cell.textField.stringValue = text != nil ? text : @"";
    
    return cell;
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
    NSTableView* tbl = notification.object;
    if(tbl.selectedRow > _devices.count ||
       tbl.selectedRow < 0) {
        return;
    }
    AppDelegate* ad = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    MPosDeviceInfo* device = nil;
    
    device = _devices[tbl.selectedRow];
    
    if(device == nil) {
        return;
    }
    
    ad.conntectionType = device.interfaceType;
    ad.deviceName = device.name;
    ad.networkDevice = nil;
    switch(device.interfaceType) {
        case MPOS_INTERFACE_WIFI:
        case MPOS_INTERFACE_ETHERNET:
            ad.connectionString = [NSString stringWithFormat:@"%@:%@", device.address, device.portNumber];
            ad.networkDevice = device;
            break;
        case MPOS_INTERFACE_BLUETOOTH:
        case MPOS_INTERFACE_BLE:
            ad.connectionString = [NSString stringWithFormat:@"%@", device.macAddress];
            break;
        case MPOS_INTERFACE_USB:
            ad.connectionString = device.address;
            break;
        default:
            ad.connectionString = @"Unknown";
            break;
    }
}

@end
