//
//  SettingsViewController.m
//  SiiLibSample
//

#import "SettingsViewController.h"
#import "SampleValue.h"
#import "SampleFunctions.h"

@interface SettingsViewController () <UITableViewDelegate, UITableViewDataSource> {
    NSString *_sendTimeout;
    NSString *_receiveTimeout;
    NSString *_socketKeepingTime;
    NSString *_codePage;
    NSString *_internationalCharacter;
}

@property (strong, nonatomic) IBOutlet UITableView *_tableView;

@end

@implementation SettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self._tableView.delegate = (id <UITableViewDelegate>)self;
    self._tableView.dataSource = (id <UITableViewDataSource>)self;
    [self getValues];
}

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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section < SIZE_OF_SECTION_TITLE_TABLE) {
        return SECTION_TITLE_TABLE[section];
    } else {
        return @"";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    cell.textLabel.text = @"";
    cell.textLabel.numberOfLines = 0;
    NSString *sectionTitle = SECTION_TITLE_TABLE[indexPath.section];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if ([sectionTitle isEqualToString:SETTINGS_SEND_TIMEOUT_SECTION]) {
        cell.textLabel.text = _sendTimeout;
    } else if ([sectionTitle isEqualToString:SETTINGS_RECEIVE_TIMEOUT_SECTION]) {
        cell.textLabel.text = _receiveTimeout;
    } else if ([sectionTitle isEqualToString:SETTINGS_SOCKET_KEEP_TIME_SECTION]) {
        cell.textLabel.text = _socketKeepingTime;
    } else if ([sectionTitle isEqualToString:SETTINGS_CODEPAGE_SECTION]) {
        cell.textLabel.text = _codePage;
    } else if ([sectionTitle isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_SECTION]) {
        cell.textLabel.text = _internationalCharacter;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionTitle = SECTION_TITLE_TABLE[indexPath.section];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:sectionTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
    if ([sectionTitle isEqualToString:SETTINGS_SEND_TIMEOUT_SECTION] || [sectionTitle isEqualToString:SETTINGS_RECEIVE_TIMEOUT_SECTION] || [sectionTitle isEqualToString:SETTINGS_SOCKET_KEEP_TIME_SECTION]){
        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.placeholder = @"Input timeout value";
            textField.keyboardType = UIKeyboardTypeNumberPad;
        }];
        [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSString *settingValue = alertController.textFields.firstObject.text;
            // Set input value to SDK and get return value for display.
            if ([sectionTitle isEqualToString:SETTINGS_SEND_TIMEOUT_SECTION]) {
                self->_sendTimeout = [NSString stringWithFormat:@"%ld", (long)[SampleFunctions set_sendTimeoutSample:[settingValue integerValue]]];
            } else if ([sectionTitle isEqualToString:SETTINGS_RECEIVE_TIMEOUT_SECTION]){
                self->_receiveTimeout = [NSString stringWithFormat:@"%ld", (long)[SampleFunctions set_receiveTimeoutSample:[settingValue integerValue]]];
            } else if ([sectionTitle isEqualToString:SETTINGS_SOCKET_KEEP_TIME_SECTION]){
                self->_socketKeepingTime = [NSString stringWithFormat:@"%ld", (long)[SampleFunctions set_socketKeepingTimeSample:[settingValue integerValue]]];
            }
            [self._tableView reloadData];
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]];
        [self presentViewController:alertController animated:YES completion:nil];
    } else if ([sectionTitle isEqualToString:SETTINGS_CODEPAGE_SECTION]) {
        NSString *item = @"";
        for(NSInteger i = 0; i < SIZE_OF_CODEPAGE_TABLE; i++) {
            item = CODEPAGE_TABLE[i];
            [alertController addAction:[UIAlertAction actionWithTitle:item style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                // Set selected value to SDK and get return value for display.
                self->_codePage = [self convertCodePageIntegerToString:[SampleFunctions set_codePageSample:[self convertCodePageStringToInteger:item]]];
                [self._tableView reloadData];
            }]];
        }
        [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]];
        [self presentViewController:alertController animated:YES completion:nil];
    } else if ([sectionTitle isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_SECTION]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:sectionTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
        NSString *item = @"";
        for(NSInteger i = 0; i < SIZE_OF_INTERNATIONAL_CHARACTER_TABLE; i++) {
            item = INTERNATIONAL_CHARACTER_TABLE[i];
            [alertController addAction:[UIAlertAction actionWithTitle:item style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                // Set selected value to SDK and get return value for display.
                self->_internationalCharacter = [self convertInternationalCharacterIntegerToString:[SampleFunctions set_internationalCharacterSample:[self convertInternationalCharacterStringToInteger:item]]];
                [self._tableView reloadData];
            }]];
        }
        [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

-(void)getValues{
    self->_sendTimeout = [NSString stringWithFormat:@"%ld", (long)[SampleFunctions get_sendTimeoutSample]];
    self->_receiveTimeout = [NSString stringWithFormat:@"%ld", (long)[SampleFunctions get_receiveTimeoutSample]];
    self->_socketKeepingTime = [NSString stringWithFormat:@"%ld", (long)[SampleFunctions get_socketKeepingTimeSample]];
    self->_codePage = [self convertCodePageIntegerToString:[SampleFunctions get_codePageSample]];
    self->_internationalCharacter = [self convertInternationalCharacterIntegerToString:[SampleFunctions get_internationalCharacterSample]];
}

- (NSString *)convertInternationalCharacterIntegerToString:(NSInteger)internationalCharacterCode {
    if(internationalCharacterCode == SII_PM_COUNTRY_USA) {
        return SETTINGS_INTERNATIONAL_CHARACTER_USA;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_FRANCE) {
        return SETTINGS_INTERNATIONAL_CHARACTER_FRANCE;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_GERMANY) {
        return SETTINGS_INTERNATIONAL_CHARACTER_GERMANY;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_ENGLAND) {
        return SETTINGS_INTERNATIONAL_CHARACTER_ENGLAND;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_DENMARK_1) {
        return SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_1;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_SWEDEN) {
        return SETTINGS_INTERNATIONAL_CHARACTER_SWEDEN;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_ITALY) {
        return SETTINGS_INTERNATIONAL_CHARACTER_ITALY;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_SPAIN) {
        return SETTINGS_INTERNATIONAL_CHARACTER_SPAIN;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_JAPAN) {
        return SETTINGS_INTERNATIONAL_CHARACTER_JAPAN;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_NORWAY) {
        return SETTINGS_INTERNATIONAL_CHARACTER_NORWAY;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_DENMARK_2) {
        return SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_2;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_SPAIN_2) {
        return SETTINGS_INTERNATIONAL_CHARACTER_SPAIN_2;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_LATIN_AMERICA) {
        return SETTINGS_INTERNATIONAL_CHARACTER_LATIN_AMERICA;
    } else if (internationalCharacterCode == SII_PM_COUNTRY_ARABIA) {
        return SETTINGS_INTERNATIONAL_CHARACTER_ARABIA;
    } else {
        return @"internationalCharacterCode is invalid";
    }
}

- (NSInteger)convertInternationalCharacterStringToInteger:(NSString*)internationalCharacterString{
    NSInteger internationalCharacterCode = -1;
    if([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_USA]) {
        internationalCharacterCode = SII_PM_COUNTRY_USA;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_FRANCE]) {
        internationalCharacterCode = SII_PM_COUNTRY_FRANCE;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_GERMANY]) {
        internationalCharacterCode = SII_PM_COUNTRY_GERMANY;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_ENGLAND]) {
        internationalCharacterCode = SII_PM_COUNTRY_ENGLAND;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_1]) {
        internationalCharacterCode = SII_PM_COUNTRY_DENMARK_1;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_SWEDEN]) {
        internationalCharacterCode = SII_PM_COUNTRY_SWEDEN;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_ITALY]) {
        internationalCharacterCode = SII_PM_COUNTRY_ITALY;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_SPAIN]) {
        internationalCharacterCode = SII_PM_COUNTRY_SPAIN;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_JAPAN]) {
        internationalCharacterCode = SII_PM_COUNTRY_JAPAN;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_NORWAY]) {
        internationalCharacterCode = SII_PM_COUNTRY_NORWAY;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_DENMARK_2]) {
        internationalCharacterCode = SII_PM_COUNTRY_DENMARK_2;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_SPAIN_2]) {
        internationalCharacterCode = SII_PM_COUNTRY_SPAIN_2;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_LATIN_AMERICA]) {
        internationalCharacterCode = SII_PM_COUNTRY_LATIN_AMERICA;
    } else if ([internationalCharacterString isEqualToString:SETTINGS_INTERNATIONAL_CHARACTER_ARABIA]) {
        internationalCharacterCode = SII_PM_COUNTRY_ARABIA;
    }
    return internationalCharacterCode;
}

- (NSString*)convertCodePageIntegerToString:(NSInteger)codePageCode{
        if(codePageCode == SII_PM_CODE_PAGE_437) {
            return SETTINGS_CODEPAGE_437;
        } else if (codePageCode == SII_PM_CODE_PAGE_KATAKANA) {
            return SETTINGS_CODEPAGE_KATAKANA;
        } else if (codePageCode == SII_PM_CODE_PAGE_850) {
            return SETTINGS_CODEPAGE_850;
        } else if (codePageCode == SII_PM_CODE_PAGE_860) {
            return SETTINGS_CODEPAGE_860;
        } else if (codePageCode == SII_PM_CODE_PAGE_863) {
            return SETTINGS_CODEPAGE_863;
        } else if (codePageCode == SII_PM_CODE_PAGE_865) {
            return SETTINGS_CODEPAGE_865;
        } else if (codePageCode == SII_PM_CODE_PAGE_857) {
            return SETTINGS_CODEPAGE_857;
        } else if (codePageCode == SII_PM_CODE_PAGE_737) {
            return SETTINGS_CODEPAGE_737;
        } else if (codePageCode == SII_PM_CODE_PAGE_1252) {
            return SETTINGS_CODEPAGE_1252;
        } else if (codePageCode == SII_PM_CODE_PAGE_866) {
            return SETTINGS_CODEPAGE_866;
        } else if (codePageCode == SII_PM_CODE_PAGE_852) {
            return SETTINGS_CODEPAGE_852;
        } else if (codePageCode == SII_PM_CODE_PAGE_858) {
            return SETTINGS_CODEPAGE_858;
        } else if (codePageCode == SII_PM_CODE_PAGE_855) {
            return SETTINGS_CODEPAGE_855;
        } else if (codePageCode == SII_PM_CODE_PAGE_864) {
            return SETTINGS_CODEPAGE_864;
        } else if (codePageCode == SII_PM_CODE_PAGE_1250) {
            return SETTINGS_CODEPAGE_1250;
        } else if (codePageCode == SII_PM_CODE_PAGE_1251) {
            return SETTINGS_CODEPAGE_1251;
        } else if (codePageCode == SII_PM_CODE_PAGE_1253) {
            return SETTINGS_CODEPAGE_1253;
        } else if (codePageCode == SII_PM_CODE_PAGE_1254) {
            return SETTINGS_CODEPAGE_1254;
    } else {
        return @"Codepage is invalid";
    }
}

- (NSInteger)convertCodePageStringToInteger:(NSString*)codePageString{
    NSInteger codePageCode = -1;
    if([codePageString isEqualToString:SETTINGS_CODEPAGE_437]) {
        codePageCode = SII_PM_CODE_PAGE_437;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_KATAKANA]) {
        codePageCode = SII_PM_CODE_PAGE_KATAKANA;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_850]) {
        codePageCode = SII_PM_CODE_PAGE_850;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_860]) {
        codePageCode = SII_PM_CODE_PAGE_860;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_863]) {
        codePageCode = SII_PM_CODE_PAGE_863;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_865]) {
        codePageCode = SII_PM_CODE_PAGE_865;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_857]) {
        codePageCode = SII_PM_CODE_PAGE_857;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_737]) {
        codePageCode = SII_PM_CODE_PAGE_737;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_1252]) {
        codePageCode = SII_PM_CODE_PAGE_1252;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_866]) {
        codePageCode = SII_PM_CODE_PAGE_866;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_852]) {
        codePageCode = SII_PM_CODE_PAGE_852;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_858]) {
        codePageCode = SII_PM_CODE_PAGE_858;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_855]) {
        codePageCode = SII_PM_CODE_PAGE_855;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_864]) {
        codePageCode = SII_PM_CODE_PAGE_864;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_1250]) {
        codePageCode = SII_PM_CODE_PAGE_1250;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_1251]) {
        codePageCode = SII_PM_CODE_PAGE_1251;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_1253]) {
        codePageCode = SII_PM_CODE_PAGE_1253;
    } else if ([codePageString isEqualToString:SETTINGS_CODEPAGE_1254]) {
        codePageCode = SII_PM_CODE_PAGE_1254;
    }
    return codePageCode;
}

@end
