#import "DeviceIdTableViewController.h"

#ifdef MPOS_XCFRAMEWORK
@import frameworkMPosSDK;

#else
#import "MPosControllerConfig.h"

#endif

#import "AppDelegate.h"
#import "TestMainViewController.h"

@interface DeviceIdTableViewController ()
@property NSArray<NSNumber*>* deviceIdList;
@property (weak) IBOutlet NSProgressIndicator *spinner;

@end

@implementation DeviceIdTableViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.target = self;
    self.tableView.doubleAction = @selector(tableViewDoubleAction:);
}

-(void)viewDidAppear {
    [super viewDidAppear];
    __weak DeviceIdTableViewController* weakSelf  = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        __strong DeviceIdTableViewController* strongSelf = weakSelf;
        [strongSelf setIsHiddenSpinner:NO];
    });
    MPosControllerConfig* config = [MPosControllerConfig new];
    AppDelegate* ad = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    [config selectInterface:ad.conntectionType address:ad.connectionString];
    if([config openService] == RES_SUCCESS){
        [config searchDevices:^(NSArray<NSNumber*>* list) {
            self.deviceIdList = list;
        }];
    }
    [config closeService:1];
    [self.tableView reloadData];
    [self setIsHiddenSpinner:YES];
}

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

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

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

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

#pragma mark - NSTableViewDelegate
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSString* identifier = @"";
    NSString* text = @"";
    NSNumber* deviceId = _deviceIdList[row];
    if (tableColumn == tableView.tableColumns[0]) {
        identifier = @"DeviceId";
        text = [NSString stringWithFormat:@"%ld", deviceId.integerValue];
    } else if (tableColumn == tableView.tableColumns[1]) {
        identifier = @"Category";
        NSInteger value = deviceId.integerValue;
        if(value > 0 && value < 10) text = @"Label Printer";
        else if(value == 10) text = @"POS Printer (internal)";
        else if(value > 10 && value < 20) text = @"POS Printer";
        else if(value >= 20 && value < 30) text = @"HID";
        else if(value >= 30 && value < 40) text = @"MSR";
        else if(value >= 40 && value < 50) text = @"Scanner";
        else if(value >= 60 && value < 69) text = @"RFID";
        else if(value >= 70 && value < 79) text = @"DALLAS KEY";
        else if(value >= 80 && value < 89) text = @"NFC";
        else if(value >= 100 && value < 110) text = @"Unregistered USB UART Device";
        else if(value >= 110 && value < 120) text = @"Customer Display";
        else if(value >= 120 && value < 130) text = @"TTY USB (USB to Serial)";
        else if(value >= 130 && value < 140) text = @"SCALE";
        else{
            text = @"Unknown Type Device";
        }
    }
    
    NSTableCellView* cell = [tableView makeViewWithIdentifier:identifier owner:nil];
    cell.textField.stringValue = text;
    
    return cell;
}

- (void)tableViewSelectionDidChange:(NSNotification *)notification {
    NSTableView* tbl = notification.object;
    if(tbl.selectedRow > _deviceIdList.count ||
       tbl.selectedRow < 0) {
        return;
    }
    AppDelegate* ad = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    
    NSNumber* deviceId = _deviceIdList[tbl.selectedRow];
    
    if(deviceId == nil) {
        return;
    }
    ad.deviceId = deviceId.integerValue;
}

@end
