//
//  VFYIDFailGuideWrapper.m
//  Verif-y
//
//  Created by MTN on 02/09/20.
//

#import "VFYIDFailGuideWrapper.h"
#import "VFYVerifyApp.h"
#import "VFYIDCountryWrapper.h"
#import "VFYButton.h"
#import "VFYConstant.h"
#import "ResultModelIdFail.h"
#import "VFYAuditManager.h"

@interface VFYIDFailGuideWrapper ()

@property (weak, nonatomic) IBOutlet UILabel *LbFailTitle;
@property (weak, nonatomic) IBOutlet UILabel *LbFailSubTitle;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (nonatomic, retain) VFYVerifyApp *sharedInstance;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtGallery;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtCamera;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (nonatomic) BOOL appearFirstTime;
@property (nonatomic) CFTimeInterval idFailDurationStart;
@property (nonatomic, strong) NSBundle *bundlePath;

@end

@implementation VFYIDFailGuideWrapper

@synthesize callDirectlyFlag;

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
         self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.bundlePath = [NSBundle bundleForClass:[VFYVerifyApp class]];
    self.sharedInstance = [VFYVerifyApp sharedInstance];
    /** observer is use to receive values from Nth child to current view */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(idFailCallFromNthChild:) name:VFY_OBSERVER_IDFAIL object:nil];
    [self setLocalizationText];
    [self setBrandingColor];
    [self CustomTitle];
    //[self setCloseButton];
    
    self.idFailDurationStart = CACurrentMediaTime();
}

/**
  Add localization text on labels
*/
- (void) setLocalizationText
{
    self.LbFailTitle.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_userguidance_title", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Scan Failed...", nil);
    self.LbFailSubTitle.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_userguidance_subtitle", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Take a Picture of Your ID!", nil);
    [self.LbBtGallery setTitle:NSLocalizedStringWithDefaultValue(@"mb_identity_button_2", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Try again", nil) forState:UIControlStateNormal];

    [self.LbBtCamera setTitle:NSLocalizedStringWithDefaultValue(@"mb_identity_button_5", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Camera", nil) forState:UIControlStateNormal];
}

/**
  Add color to branding and corner button
*/
- (void) setBrandingColor
{
    [self.BodyScrollView setBackgroundColor:[Utilities colorFromColorString:self.sharedInstance.bodyColor]];
    /* Set color to title */
    [self.LbFailTitle setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTitleColor]];
    /* Set color to branding */
    [self.LbLogoPow setTextColor:[Utilities colorFromColorString:VFY_LOGO_POWER]];
    [self.LbLogoBy setTextColor:[Utilities colorFromColorString:VFY_LOGO_BY]];
    [self CustomTitle];
    /* Set button style */
    [self.LbBtGallery animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
    [self.LbBtCamera animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
}

/**
  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];
    }
}

 -(void)viewWillAppear:(BOOL)animated
{
  /*if(self.sharedInstance.failedContinuePressed){
      [self.idGuide callClose:IDFailedGuideWrapper];
  }*/
  [super viewWillAppear:animated];
}

/**
  Binding action to the buttons
*/
- (IBAction)BTNGallery:(id)sender {
    //[self PushView:false];
    /* Uncomment above code to active gallery upload functionality */
    
    [self callAudit];
    //[self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)BTNCamera:(id)sender {
    [self PushView:true];
}

/**
  Add push new viewcontroller
*/
- (void) PushView:(BOOL)type
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:VFY_MAIN_STORYBOARD bundle:self.bundlePath];
    VFYIDCountryWrapper *newView = [storyboard instantiateViewControllerWithIdentifier:VFY_IDFAILCOUNTRY_STORYBOARD];
    newView.selectionType = type;
    [self.navigationController pushViewController:newView animated:YES];
}

/**
  Add close button in navigation
*/
- (void) setCloseButton {
    UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(BtClose:)];
    self.navigationItem.leftBarButtonItem = flipButton;
}

/**
  Close button action
*/
-(IBAction) BtClose:(id)sender {
    
    //[self dismissViewControllerAnimated:YES completion:nil];
    if(callDirectlyFlag == Directly){
      [self.idGuide callClose:IDFailedGuideWrapperClose];
    } else {
      [self dismissViewControllerAnimated:YES completion:nil];
    }
}

/** Receive method use to handle call from nth child */
- (void) idFailCallFromNthChild:(NSNotification *)notify {
    ResultModelIdFail *idFail = (ResultModelIdFail*)notify.object;
    [self.idGuide callClose:IDFailedGuideWrapper PassValue:idFail];
}

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

@end
