//
//  VFYCardFailWrapper.m
//  Verif-y
//
//  Created by MTN on 17/11/20.
//

#import "VFYCardFailWrapper.h"
#import "Utilities.h"
#import "VFYButton.h"
#import "VFYConstant.h"
#import "ResultModelCard.h"

#import "VFYCardFailFrontWrapper.h"
#import <QuartzCore/QuartzCore.h>
#import "VFYAuditManager.h"


@interface VFYCardFailWrapper()
{

}

@property (weak, nonatomic) IBOutlet UILabel *CardTitle;
@property (weak, nonatomic) IBOutlet UILabel *CardInstruction;
@property (weak, nonatomic) IBOutlet VFYButton *BTNContinue;
@property (weak, nonatomic) IBOutlet VFYButton *BTNTryAgain;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (weak, nonatomic) IBOutlet UINavigationBar *navigationBar;
@property (nonatomic) CFTimeInterval cardFailDurationStart;
@property (nonatomic, strong) NSBundle *bundlePath;

@end

@implementation VFYCardFailWrapper

@synthesize cardFailDurationStart;

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
        self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.bundlePath = [NSBundle bundleForClass:[VFYVerifyApp class]];
    self.sharedInstance = [VFYVerifyApp sharedInstance];
    [self setLocalizationText];
    [self CustomTitle];
    [self setBrandingColor];
    
    cardFailDurationStart = CACurrentMediaTime();
}

/**
  Add localization text on labels
*/
- (void) setLocalizationText {
  /* self.CardTitle.text = NSLocalizedStringWithDefaultValue(@"mb_card_userguidance_title", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"अपनी आईडी तैयार रखें!", nil);
   self.CardInstruction.text = NSLocalizedStringWithDefaultValue(@"mb_card_userguidance_subtitle", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"एक मान्य क्रेडिट / डेबिट कार्ड का उपयोग करें", nil);
   [self.BTNContinue setTitle:NSLocalizedStringWithDefaultValue(@"mb_card_button_1", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"जारी रखें", nil) forState:UIControlStateNormal]; */
   self.CardTitle.text = NSLocalizedStringWithDefaultValue(@"mb_cardFailed_oops_msg", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Oops!", nil);
   self.CardInstruction.text = NSLocalizedStringWithDefaultValue(@"mb_cardFailed_we_didn_t_quite_get_that", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"We didn't quite get that!", nil);
   [self.BTNContinue setTitle:NSLocalizedStringWithDefaultValue(@"mb_card_scan_button", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Continue", nil) forState:UIControlStateNormal];
   [self.BTNTryAgain setTitle:NSLocalizedStringWithDefaultValue(@"mb_cardFailed_try_again_button", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Continue", nil) forState:UIControlStateNormal];
}

/**
  Add Header title test or image
*/
-(void) CustomTitle
{
    if([self.sharedInstance.headerTitleImage length]>0){
       [Utilities customTitleImage:self.navigationItem Logo:self.sharedInstance.headerTitleImage];
    } else if([self.sharedInstance.headerTitleText length]){
        [Utilities customTitleText:self.navigationItem TitleText:self.sharedInstance.headerTitleText FontColor:self.sharedInstance.headerTextColor];
    }
}

/**
  Add color to branding and corner button
*/
- (void) setBrandingColor {
    /* Set page appearance*/
    [[UILabel appearance] setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTextColor]];
    [[UITextField appearance] setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTextColor]];
    [[UITextView appearance] setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTextColor]];
    [[UIButton appearance] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.BodyScrollView setBackgroundColor:[Utilities colorFromColorString:self.sharedInstance.bodyColor]];
    [self.CardTitle setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTitleColor]];
    
    /* Set color to the branding logo */
    [self.LbLogoPow setTextColor:[Utilities colorFromColorString:VFY_LOGO_POWER]];
    [self.LbLogoBy setTextColor:[Utilities colorFromColorString:VFY_LOGO_BY]];
    /* Set button style */
    [self.BTNContinue animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
    [self.BTNTryAgain animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
}

/**
  Continue button to start scanning
*/
- (IBAction)BTNContinue:(id)sender {
    /*
    ResultModelCard *cardResult = [[ResultModelCard alloc] init];
    cardResult.isSuccessful = false;
    [[NSNotificationCenter defaultCenter] postNotificationName:VFY_OBSERVER_CARDSUCCESS object:cardResult];*/
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:VFY_MAIN_STORYBOARD bundle:self.bundlePath];
    VFYCardFailFrontWrapper *vc = [storyboard instantiateViewControllerWithIdentifier:VFY_CARDFAIL_FRONT_STORYBOARD];
    vc.cardFailDurationStart = self.cardFailDurationStart;
    [self.navigationController pushViewController:vc animated:YES];
}

/**
  Continue button to start scanning
*/
- (IBAction)BTNTryAgain:(id)sender {
    [self callAudit];
    [self.navigationController popViewControllerAnimated:YES];
}

/**
    Call try again API
 */
- (void) callAudit {
    ResultModelCard *cardResult = [[ResultModelCard alloc] init];
    cardResult.isSuccessful = false;
    NSMutableDictionary *setting = [NSMutableDictionary dictionary];
    CFTimeInterval elapsedTime = CACurrentMediaTime() - self.cardFailDurationStart;
    if(elapsedTime) {
        NSString *duration = [Utilities stringFromTimeInterval:elapsedTime];
        [setting setValue:duration forKey:@"audit_event_duration"];
    } else {
        [setting setValue:@"" forKey:@"audit_event_duration"];
    }
    [VFYAuditManager cardTryAgainAudit:cardResult settings:setting completed:^(NSString *response) {
        [self.sharedInstance setPreviousEventId:[response integerValue]];
        NSLog(@"Card fail try again response %@",response);
    } failure:^(NSError *error){
        NSLog(@"Card fail try again response error : %@",error);
    }];
}

@end
 
