//
//  VFYCardFailBackWrapper.m
//  Verif-y Neo
//
//  Created by MTN on 04/01/21.
//

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

#import <MobileCoreServices/MobileCoreServices.h>
#import "VFYAuditManager.h"

@interface VFYCardFailBackWrapper() <UIImagePickerControllerDelegate>
{

}

@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (weak, nonatomic) IBOutlet UILabel *CardTitle;
@property (weak, nonatomic) IBOutlet VFYButton *BTNContinue;
@property (nonatomic) CFTimeInterval elapsedTime;

@end

@implementation VFYCardFailBackWrapper

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

/**
  Add localization text on labels
*/
- (void) setLocalizationText {
   self.CardTitle.text = NSLocalizedStringWithDefaultValue(@"mb_cardFailed_back_of_card", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Front of card", nil);
   [self.BTNContinue setTitle:NSLocalizedStringWithDefaultValue(@"mb_card_scan_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]]];
}

/**
  Continue button to start scanning
*/
- (IBAction)BTNContinue:(id)sender {
    /*
    [[NSNotificationCenter defaultCenter] postNotificationName:VFY_OBSERVER_CARDSUCCESS object:cardResult];
    */
    //self.modalCard.documentBackImage = @"Backside of card";
    //[[NSNotificationCenter defaultCenter] postNotificationName:VFY_OBSERVER_CARDSUCCESS object:self.modalCard];
    UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = (id)self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void) openImagePreviewController:(UIImage *) image {
   self.elapsedTime = CACurrentMediaTime() - self.cardFailDurationStart;
   NSString *base64String = [Utilities encodeToBase64String:image];
   self.modalCard.documentBackImage = base64String;
   [self CallAudit];
   [[NSNotificationCenter defaultCenter] postNotificationName:VFY_OBSERVER_CARDSUCCESS object:self.modalCard];
}

- (void) CallAudit {
    NSMutableDictionary *setting = [NSMutableDictionary dictionary];
    if(self.elapsedTime) {
        NSString *duration = [Utilities stringFromTimeInterval:self.elapsedTime];
        [setting setValue:duration forKey:@"audit_event_duration"];
    } else {
        [setting setValue:@"" forKey:@"audit_event_duration"];
    }
    [VFYAuditManager cardFailAudit:self.modalCard settings:setting completed:^(NSString *response) {
        NSLog(@"Card fail response %@",response);
    } failure:^(NSError *error){
        NSLog(@"Card fail response error : %@",error);
    }];
}

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissViewControllerAnimated:YES completion:nil];
    NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
        UIImage* picture = nil;//[info objectForKey:UIImagePickerControllerEditedImage];
        if (!picture){
            picture = [info objectForKey:UIImagePickerControllerOriginalImage];
            [self openImagePreviewController:picture];
        }
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
