//
//  PrinterInformationViewController.m
//  SIIMpaPrinterSample
//

#import "PrinterInformationViewController.h"
#import "PrinterManager.h"
#import "ValueManager.h"

@interface PrinterInformationViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PrinterInformationViewController {
    UIView *_indicatorView;
    NSArray *_tableData;
    NSInteger _currentValueIndex;    
}

static NSUInteger const SIZE_OF_PRINTER_INFO_TABLE = 34;
static NSString *const PRINTER_INFO_TABLE[] = {
    KEY_TYPE_NV_MEM_CAP,
    KEY_TYPE_NV_MEM_REM_CAP,
    KEY_TYPE_NV_MEM_KEYCODE_LIST,
    KEY_TYPE_REM_USER_MEM_CAP,
    KEY_TYPE_REM_USER_MEM_CAP_DEFRAG,
    KEY_TYPE_FUNC_SET_RESP,
    KEY_TYPE_PRN_ID_MODEL,
    KEY_TYPE_PRN_ID_TYPE,
    KEY_TYPE_PRN_ID_ROM_VER,
    KEY_TYPE_PRN_ID_FIRM_VER_MAIN,
    KEY_TYPE_PRN_ID_MFR,
    KEY_TYPE_PRN_ID_MODEL_NAME,
    KEY_TYPE_PRN_ID_FIRM_VER_BOOT,
    KEY_TYPE_PRN_ID_FIRM_CHECKSUM_BOOT,
    KEY_TYPE_PRN_ID_FIRM_CHECKSUM_MAIN,
    KEY_TYPE_PRN_ID_FIRM_CHECKSUM,
    KEY_TYPE_MAINT_NUM_FEED_LINE,
    KEY_TYPE_MAINT_NUM_HEAD_ACTIVE,
    KEY_TYPE_MAINT_DRIVE_TIME,
    KEY_TYPE_MAINT_NUM_FEED_LINE_INTEGRATION,
    KEY_TYPE_MAINT_NUM_HEAD_ACTIVE_INTEGRATION,
    KEY_TYPE_MAINT_DRIVE_TIME_INTEGRATION,
    KEY_TYPE_HFONT_24_CHECKSUM,
    KEY_TYPE_HFONT_24_ID,
    KEY_TYPE_HFONT_24_INT_CHAR,
    KEY_TYPE_HFONT_16_CHECKSUM,
    KEY_TYPE_HFONT_16_ID,
    KEY_TYPE_HFONT_16_INT_CHAR,
    KEY_TYPE_FFONT_LANG,
    KEY_TYPE_FFONT_STANDARD,
    KEY_TYPE_FFONT_COMPANY,
    KEY_TYPE_FFONT_CHECKSUM,
    KEY_TYPE_FFONT_DATA_SIZE,
    KEY_TYPE_FFONT_ROM_ID
};

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.delegate = (id <UITableViewDelegate>)self;
    self.tableView.dataSource = (id <UITableViewDataSource>)self;
    [self initTableList];
    [self initIndicatorView];
}

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"Select PrinterInformation";
    } else {
        return @"";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [_tableData objectAtIndex:indexPath.row];
    cell.textLabel.numberOfLines = 0;
    [cell.textLabel setFont:[cell.textLabel.font fontWithSize:14.0]];
    if (indexPath.row == _currentValueIndex) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (NSInteger i = 0; i < _tableData.count; i++) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    _currentValueIndex = indexPath.row;
}

#pragma mark - Button Action
- (IBAction)okButtonOnClicked:(id)sender {
    if (0 <= _currentValueIndex && _currentValueIndex < SIZE_OF_PRINTER_INFO_TABLE) {
        [self startLoadingView];
        dispatch_async([PrinterManager getDispatchQueue], ^{
            NSString *result = [PrinterManager getPrinterInformation:PRINTER_INFO_TABLE[self->_currentValueIndex]];
            [self stopLoadingView];
            if (result != nil) {
                [self showMessageBox:PRINTER_INFO_TABLE[self->_currentValueIndex] message:result];
            } else {
                NSString *message = [NSString stringWithFormat:@"Attempt to get %@ failed.\n ErrorCode : %@", PRINTER_INFO_TABLE[self->_currentValueIndex], [PrinterManager getErrorCodeText]];
                [self showMessageBox:@"" message:message];
            }
        });
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (IBAction)closeButtonOnClicked:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Private
- (void)initTableList {
    NSMutableArray *valueData = [[NSMutableArray alloc] init];
    for (int i = 0; i < SIZE_OF_PRINTER_INFO_TABLE; i++) {
        [valueData addObject:PRINTER_INFO_TABLE[i]];
    }
    _tableData = valueData;
    _currentValueIndex = 0;
    [self.tableView reloadData];
}

- (void)initIndicatorView {
    _indicatorView = [[NSBundle mainBundle] loadNibNamed:@"IndicatorView" owner:self options:nil][0];
    _indicatorView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    _indicatorView.hidden = YES;
    [self.view addSubview:_indicatorView];
}

- (void)startLoadingView {
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_indicatorView.hidden = NO;
    });
}

- (void)stopLoadingView {
    dispatch_async(dispatch_get_main_queue(), ^{
        self->_indicatorView.hidden = YES;
    });
}

- (void)showMessageBox:(NSString *)title message:(NSString *)message {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController =  [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
    });
}

@end
