#import "AppSettingsViewController.h"

@interface AppSettingsViewController() <TerminalHealthListener>

@property (weak, nonatomic) IBOutlet UITextField *terminalID;
@property (weak, nonatomic) IBOutlet UITextField *secret;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (weak, nonatomic) IBOutlet UIButton *modeButton;
@property (weak, nonatomic) IBOutlet UIButton *healthCheckButton;
@property (weak, nonatomic) IBOutlet UIButton *offlineModeButton;
@property (weak, nonatomic) IBOutlet UIButton *closeBatchButton;
@property (weak, nonatomic) IBOutlet UIButton *logLevelButton;

@end


@implementation AppSettingsViewController
{
    UIColor* color;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    NSString* terminalId = [Util getFromSettings:@"terminalId"];
    NSString* secret = [Util getFromSettings:@"secret"];
    if(terminalId != nil && secret != nil){
        [_terminalID setText:terminalId];
        [_secret setText:secret];
    }
    color = UIColorFromRGB(0xC11D03);
    [self setTheme];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];
    
    [self.view addGestureRecognizer:tap];
}

-(void)dismissKeyboard {
    [_terminalID resignFirstResponder];
    [_secret resignFirstResponder];
}

- (void) setTheme{
    [_saveButton setBackgroundColor:color];
    [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_modeButton setBackgroundColor:color];
    [_modeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_logLevelButton setBackgroundColor:color];
    [_logLevelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_healthCheckButton setBackgroundColor:color];
    [_healthCheckButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_offlineModeButton setBackgroundColor:color];
    [_offlineModeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_closeBatchButton setBackgroundColor:color];
    [_closeBatchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.navigationController.navigationBar.barTintColor = color;
}

- (void) hideCloseBatchButton:(BOOL) hidden{
    [_closeBatchButton setHidden:hidden];
}

- (void) back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)modeHandle:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Mode"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"LIVE", @"TEST", @"DEV", nil];
    actionSheet.tag = 20;
    [actionSheet showInView:self.view];
}

- (IBAction)healthCheckHandle:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Health Check"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"ENABLE", @"DISABLE", nil];
    actionSheet.tag = 30;
    [actionSheet showInView:self.view];
}

- (IBAction)offlineodeHandle:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Offline Mode"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"ENABLE", @"DISABLE", nil];
    actionSheet.tag = 40;
    [actionSheet showInView:self.view];
}

- (IBAction)closeBatchHandle:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Close Transactions Batch"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"CLOSE", nil];
    actionSheet.tag = 50;
    [actionSheet showInView:self.view];
}

- (IBAction)logLevelHandle:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Log Level"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"FULL", @"INFO", @"ERROR", @"NONE", nil];
    actionSheet.tag = 60;
    [actionSheet showInView:self.view];
}

- (IBAction)saveHandle:(id)sender {
    NSString* terminalId = [_terminalID text];
    NSString* secret = [_secret text];
    if([RestConnector isPayconexSDK]){
        terminalId = @"220614969941";
        secret = @"0e8d38a08f2d4919a5a27ec83a6e8d76";
    }
    [Util saveToSettings:@"terminalId" withValue:terminalId];
    [Util saveToSettings:@"secret" withValue:secret];
    if((terminalId == nil || [terminalId length] == 0) || (secret == nil || [secret length] == 0)){
        [[WTPSTerminal singleton] initWithConfiguration];
    } else {
        [[WTPSTerminal singleton] initWithConfiguration:terminalId withSecret:secret];
    }
    [self back];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (actionSheet.tag == 20) {
        switch (buttonIndex) {
            case 0:
                NSLog(@"Request URL pressed.");
                [Util saveToSettings:@"mode" withValue:@"LIVE"];
                [[WTPSTerminal singleton] setMode:LIVE];
                break;
                
            case 1:
                NSLog(@"Transactions pressed.");
                [Util saveToSettings:@"mode" withValue:@"TEST"];
                [[WTPSTerminal singleton] setMode:TEST];
                break;
                
            case 2:
                NSLog(@"Transactions pressed.");
                [Util saveToSettings:@"mode" withValue:@"DEV"];
                [[WTPSTerminal singleton] setMode:DEV];
                break;
            default:
                break;
        }
    } else if (actionSheet.tag == 30) {
        switch (buttonIndex) {
            case 0:
                NSLog(@"ENABLE health check");
                [TerminalHealth startTerminalHealthCheck:[WTPSTerminal singleton] withListener:self];
                break;
            case 1:
                NSLog(@"DISABLE health check");
                [TerminalHealth stopTerminalHealthCheck:[WTPSTerminal singleton] withListener:self];
                
                break;
            default:
                break;
        }
    } else if (actionSheet.tag == 40) {
        switch (buttonIndex) {
            case 0:
                NSLog(@"ENABLE offline mode");
                [[WTPSTerminal singleton] setOfflineMode:YES];
                break;
            case 1:
                NSLog(@"DISABLE offline mode");
                [[WTPSTerminal singleton] setOfflineMode:NO];
                break;
            default:
                break;
        }
    } else if (actionSheet.tag == 50) {
        switch (buttonIndex) {
            case 0:
                NSLog(@"CLOSE transactions batch");
                [[WTPSTerminal singleton] closeTransactionsBatch];
                break;
            default:
                break;
        }
    } else if (actionSheet.tag == 60) {
        switch (buttonIndex) {
            case 0:
                if ([[WTPSTerminal singleton] getMode] == LIVE) {
                    [[UiHelper singleton] showError:@"Log level FULL is not supported in LIVE mode"];
                } else {
                    [Util saveToSettings:@"logLevel" withValue:@"FULL"];
                    [[WTPSTerminal singleton] setLogLevel:LEVEL_FULL];
                }
                break;
            case 1:
                [Util saveToSettings:@"logLevel" withValue:@"INFO"];
                [[WTPSTerminal singleton] setLogLevel:LEVEL_INFO];
                break;
            case 2:
                [Util saveToSettings:@"logLevel" withValue:@"ERROR"];
                [[WTPSTerminal singleton] setLogLevel:LEVEL_ERROR];
                break;
            case 3:
                [Util saveToSettings:@"logLevel" withValue:@"NONE"];
                [[WTPSTerminal singleton] setLogLevel:LEVEL_NONE];
                break;
        }
    }
}

-(void)onHealthCheckStarted{
    NSLog(@"onHealthCheckStarted");
}

-(void)onHealthCheckStopped{
    NSLog(@"onHealthCheckStopped");
}

-(void)onError:(CoreError)error{
    NSLog(@"EonError");
}
    
@end
