//
//  VFYFailFinalPreviewWrapper.m
//  Verif-y
//
//  Created by MTN on 14/09/20.
//

#import "VFYFailFinalPreviewWrapper.h"
#import "VFYVerifyApp.h"
#import "VFYFrontGuideWrapper.h"
#import "VFYButton.h"
#import "VFYConstant.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "VFYAuditManager.h"

@interface VFYFailFinalPreviewWrapper ()
@property (weak, nonatomic) IBOutlet UILabel *LbFnImagePreview;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtFnRetake;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtFnContinue;
@property (weak, nonatomic) IBOutlet UIView *LoaderView;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (weak, nonatomic) IBOutlet UIView *RoundLoaderView;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (nonatomic, retain) VFYVerifyApp *sharedInstance;
@property (nonatomic) NSString* Source;
@property (nonatomic) NSString* Target;
@property (nonatomic) CFTimeInterval elapsedTime;

@end

@implementation VFYFailFinalPreviewWrapper

@synthesize selectionType;
@synthesize imagePreview;
@synthesize imagePicker;
@synthesize passImage;
@synthesize selectedView;
@synthesize idSuccess;

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
         self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.LoaderView.backgroundColor = [UIColor clearColor];
    self.sharedInstance = [VFYVerifyApp sharedInstance];
    self.LbFnImagePreview.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_imagepreview", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Image Preview", nil);
    [self.LbBtFnRetake setTitle:NSLocalizedStringWithDefaultValue(@"mb_identity_button_2", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Retake", nil) forState:UIControlStateNormal];
    [self.LbBtFnContinue setTitle:NSLocalizedStringWithDefaultValue(@"mb_identity_button_1", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Continue", nil) forState:UIControlStateNormal];
    if(self.passImage != nil){
       self.imagePreview.image = self.passImage;
       NSString *base64String = [Utilities encodeToBase64String:self.passImage];
       if( [self.idFail.documenttype caseInsensitiveCompare:VFY_DOCUMENTTYPE_PASSPORT] == NSOrderedSame ) {
           self.idFail.frontimage = base64String;
       } else {
           self.idFail.backimage = base64String;
       }
    }
    [self CustomTitle];
    [self.BodyScrollView setBackgroundColor:[Utilities colorFromColorString:self.sharedInstance.bodyColor]];
    [self setBrandingColor];
}

- (void) setBrandingColor {
    /* Set title label text color */
    [self.LbFnImagePreview setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTitleColor]];
    /* Set branding color */
    [self.LbLogoPow setTextColor:[Utilities colorFromColorString:VFY_LOGO_POWER]];
    [self.LbLogoBy setTextColor:[Utilities colorFromColorString:VFY_LOGO_BY]];
    /* Set button style */
    [self.LbBtFnContinue animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
    [self.LbBtFnRetake animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
    /* Set rounded corner of loderview */
    self.RoundLoaderView.layer.cornerRadius = 8.0f;
    self.RoundLoaderView.layer.masksToBounds = true;
}

- (IBAction)BTNRetake:(id)sender {
    if(selectedView == SelfieCase){
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        self.imagePicker = [[UIImagePickerController alloc] init];
        self.imagePicker.delegate = self;
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }
}
- (IBAction)BTNContinue:(id)sender {
    
    if (selectedView == FailedCase){
        [self callAudit];
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:VFY_OBSERVER_IDFAIL object:self.idFail];
    }
}

-(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];
    }
}

- (void) callAudit {
    NSMutableDictionary *setting = [NSMutableDictionary dictionary];
    self.elapsedTime = CACurrentMediaTime() - self.idFailDurationStart;
    if(self.elapsedTime) {
        NSString *duration = [Utilities stringFromTimeInterval:self.elapsedTime];
        [setting setValue:duration forKey:@"audit_event_duration"];
    } else {
        [setting setValue:@"" forKey:@"audit_event_duration"];
    }
    [VFYAuditManager idFailAudit:self.idFail 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:^{
        // MediaType can be kUTTypeImage or kUTTypeMovie.
        NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];// NSString* videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
        if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
            //UIImage* picture = [info objectForKey:UIImagePickerControllerEditedImage];
            UIImage* picture = [info objectForKey:UIImagePickerControllerOriginalImage];
            if (picture){
                //NSString *temp = [self encodeToBase64String:picture];
                //picture = [self decodeBase64ToImage:temp];
                self.imagePreview.image = picture;
                self.imagePicker = nil;
                if (self.selectedView == FailedCase){
                    NSLog(@"Come from failed case");
                    NSString *base64String = [Utilities encodeToBase64String:picture];
                    if( [self.idFail.documenttype caseInsensitiveCompare:VFY_DOCUMENTTYPE_PASSPORT] == NSOrderedSame ) {
                        self.idFail.frontimage = base64String;
                    } else {
                        self.idFail.backimage = base64String;
                    }
                }
             }
          }
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.imagePicker = nil;
}

@end
