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

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

#import <MobileCoreServices/MobileCoreServices.h>

@interface VFYCardFailFrontWrapper() <UIImagePickerControllerDelegate>
{

}

@property (weak, nonatomic) IBOutlet UILabel *CardTitle;
@property (weak, nonatomic) IBOutlet VFYButton *BTNContinue;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (nonatomic, strong) NSBundle *bundlePath;

@end

@implementation VFYCardFailFrontWrapper

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

/**
  Add localization text on labels
*/
- (void) setLocalizationText {
   self.CardTitle.text = NSLocalizedStringWithDefaultValue(@"mb_cardFailed_front_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 {
    /*ResultModelCard *cardResult = [[ResultModelCard alloc] init];
    cardResult.isSuccessful = false;
    cardResult.documentFrontImage = @"frontside of card";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:VFY_MAIN_STORYBOARD bundle:self.bundlePath];
    VFYCardFailBackWrapper *vc = [storyboard instantiateViewControllerWithIdentifier:VFY_CARDFAIL_BACK_STORYBOARD];
    vc.modalCard = cardResult;
    [self.navigationController pushViewController:vc animated:YES];*/
    UIImagePickerController* imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = (id)self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void) openImagePreviewController:(UIImage *) image {
   ResultModelCard *cardResult = [[ResultModelCard alloc] init];
   cardResult.isSuccessful = false;
   NSString *base64String = [Utilities encodeToBase64String:image];
   cardResult.documentFrontImage = base64String;
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:VFY_MAIN_STORYBOARD bundle:self.bundlePath];
   VFYCardFailBackWrapper *vc = [storyboard instantiateViewControllerWithIdentifier:VFY_CARDFAIL_BACK_STORYBOARD];
   vc.modalCard = cardResult;
   vc.cardFailDurationStart = self.cardFailDurationStart;
   [self.navigationController pushViewController:vc animated:YES];
}

#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

