//
//  UiHelper.m
//  CoreSDKExample
//
//  Created by Dominik on 02/06/2017.
//
//
#import "UiHelper.h"

@implementation UiHelper{
    UIView* overlay;
    UIAlertView* loadingDialog;
}

+ (id)singleton {
    static UiHelper *singleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[self alloc] init];
    });
    return singleton;
}

- (void)toastMessage:(NSString*) message{
    [self hideLoading];
    UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:nil, nil];
    [toast show];
    int duration = 1; // duration in seconds
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [toast dismissWithClickedButtonIndex:0 animated:YES];     });
}


- (void)showWarning:(NSString*) message{
    [self hideLoading];
    UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:nil, nil];
    [toast show];
    int duration = 1; // duration in seconds
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [toast dismissWithClickedButtonIndex:0 animated:YES];     });
}

- (void)showError:(NSString*) message{
    [self hideLoading];
    UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
                                                    message:message
                                                   delegate:nil
                                          cancelButtonTitle:nil
                                          otherButtonTitles:nil, nil];
    [toast show];
    int duration = 1; // duration in seconds
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [toast dismissWithClickedButtonIndex:0 animated:YES];     });
}

- (void)showSuccess:(NSString*) title withMessage:(NSString*) message{
    UIAlertView *alertDialog = [[UIAlertView alloc] initWithTitle:title
                                                          message:message
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
    [alertDialog show];
}

-(void)showLoading:(UIView*)view isDeviceSale:(BOOL)isDeviceSale{
    loadingDialog = [[UIAlertView alloc] initWithTitle:@"Please wait..." message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
    if(isDeviceSale){
        loadingDialog.tag = 123;
    }
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicator startAnimating];
    [loadingDialog setValue:indicator forKey:@"accessoryView"];
    [loadingDialog show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == [alertView cancelButtonIndex] && alertView.tag == 123){
        [[WTPSTerminal singleton] cancelTransaction];
    }
}

-(void)hideLoading{
    if(loadingDialog != nil){
        [loadingDialog dismissWithClickedButtonIndex:0 animated:YES];
        loadingDialog = nil;
    }
}

-(void)updateMessage:(NSString*)message{
    if(loadingDialog != nil){
        [loadingDialog setTitle:message];
    }
}

-(BOOL)isProgressShowing{
    return loadingDialog != nil;
}

- (void) animateWrongInput:(UIView*)view{
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    CGFloat currentTx = view.transform.tx;
    
    animation.delegate = self;
    animation.duration = 0.5;
    animation.values = @[ @(currentTx), @(currentTx + 10), @(currentTx-8), @(currentTx + 8), @(currentTx -5), @(currentTx + 5), @(currentTx) ];
    animation.keyTimes = @[ @(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1) ];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [view.layer addAnimation:animation forKey:@"shake"];
}


-(void)showRequestAmountEdit:(CoreSale*)coreSale withViewController:(UIViewController*)view{
    [self hideLoading];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Amount"
                                                                   message:@"Please enter amount"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *submit = [UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       if (alert.textFields.count > 0) {
                                                           UITextField *textField = [alert.textFields firstObject];
                                                           coreSale.amount = [NSNumber numberWithDouble:[textField.text doubleValue]];
                                                           [[WTPSTerminal singleton] submitAmount:coreSale];
                                                       }
                                                   }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       
                                                   }];
    [alert addAction:cancel];
    [alert addAction:submit];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"amount"; // if needs
    }];
    
    [view presentViewController:alert animated:YES completion:nil];
    
}

@end
