#import "DeviceIdTableViewController.h"

#ifdef MPOS_XCFRAMEWORK
@import frameworkMPosSDK;

#else
#import "MPosControllerConfig.h"

#endif

#import "AppDelegate.h"

@interface DeviceIdTableViewController ()
@property UIActivityIndicatorView *spinner;
@property NSArray<NSNumber*>* deviceIdList;
@end

@implementation DeviceIdTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AppDelegate* ad = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    ad.deviceId = -1;
    [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:@"deviceIdCell"];
}

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

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_deviceIdList count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"Device ID";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deviceIdCell" forIndexPath:indexPath];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"deviceIdCell"];
    if([_deviceIdList count]){
        NSInteger value = [_deviceIdList[indexPath.row] integerValue];
        [cell detailTextLabel].text = [NSString stringWithFormat:@"  ID = %ld", (long)value];
        if(value > 0 && value < 10) [cell textLabel].text = @"Label Printer";
        else if(value == 10) [cell textLabel].text = @"POS Printer (internal)";
        else if(value > 10 && value < 20) [cell textLabel].text = @"POS Printer";
        else if(value >= 20 && value < 30) [cell textLabel].text = @"HID";
        else if(value >= 30 && value < 40) [cell textLabel].text = @"MSR";
        else if(value >= 40 && value < 50) [cell textLabel].text = @"Scanner";
        else if(value >= 60 && value < 69) [cell textLabel].text = @"RFID";
        else if(value >= 70 && value < 79) [cell textLabel].text = @"DALLAS KEY";
        else if(value >= 80 && value < 89) [cell textLabel].text = @"NFC";
        else if(value >= 100 && value < 110) [cell textLabel].text = @"Unregistered USB UART Device";
        else if(value >= 110 && value < 120) [cell textLabel].text = @"Customer Display";
        else if(value >= 120 && value < 130) [cell textLabel].text = @"TTY USB (USB to Serial)";
        else if(value >= 130 && value < 140) [cell textLabel].text = @"SCALE";
        else{
            [cell textLabel].text = @"Unknown Type Device";
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AppDelegate* ad = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    ad.deviceId = [_deviceIdList[indexPath.row] integerValue];
    [self.navigationController popViewControllerAnimated:YES];
}

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

-(void) showSpinnerAtFooter {
    __weak DeviceIdTableViewController* weakSelf  = self;
    dispatch_async(dispatch_get_main_queue(),^{
        __strong DeviceIdTableViewController* 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
