//
//  VFYFailImagePreviewWrapper.m
//  Verif-y
//
//  Created by MTN on 13/09/20.
//

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

@interface VFYFailImagePreviewWrapper ()
@property (weak, nonatomic) IBOutlet UILabel *LbImagePreview;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtRetake;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtContinue;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (nonatomic, retain) VFYVerifyApp *sharedInstance;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (nonatomic, strong) NSBundle *bundlePath;

@end

@implementation VFYFailImagePreviewWrapper

@synthesize selectionType;
@synthesize imagePreview;
@synthesize imagePicker;
@synthesize passImage;

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
       self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.bundlePath = [NSBundle bundleForClass:[VFYVerifyApp class]];
    self.sharedInstance = [VFYVerifyApp sharedInstance];
    self.LbImagePreview.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_imagepreview", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Image Preview", nil);
    [self.LbBtRetake setTitle:NSLocalizedStringWithDefaultValue(@"mb_identity_button_2", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Retake", nil) forState:UIControlStateNormal];
    [self.LbBtContinue 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];
        self.idFail.frontimage = base64String;
    }
    [self CustomTitle];
    [self.BodyScrollView setBackgroundColor:[Utilities colorFromColorString:self.sharedInstance.bodyColor]];
    [self setBrandingColor];
}

- (void) setBrandingColor
{
    /* Set title label text color */
    [self.LbImagePreview setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTitleColor]];
    /* Set branding logo */
    [self.LbLogoPow setTextColor:[Utilities colorFromColorString:VFY_LOGO_POWER]];
    [self.LbLogoBy setTextColor:[Utilities colorFromColorString:VFY_LOGO_BY]];
    /* Set button style */
    [self.LbBtContinue animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
    /* Set button style */
    [self.LbBtRetake animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
}

- (IBAction)BTNRetake:(id)sender {
    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 {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:VFY_MAIN_STORYBOARD bundle:self.bundlePath];
    VFYFrontGuideWrapper *newView = [storyboard instantiateViewControllerWithIdentifier:VFY_IDFRONTGUIDE_STORYBOARD];
    newView.selectionType = self.selectionType;
    newView.idFail = self.idFail;
    newView.stepType = @"second";
    newView.idFailDurationStart = self.idFailDurationStart;
    [self.navigationController pushViewController:newView animated:YES];
}

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

#pragma mark UIImagePickerControllerDelegate
 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    // 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){
            self.imagePreview.image = picture;
            self.imagePicker = nil;
            NSString *base64String = [Utilities encodeToBase64String:picture];
            self.idFail.frontimage = base64String;
        }
    }
}

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

@end
