//
//  VFYSuggestionPopup.m
//  Verif-y
//
//  Created by MTN on 19/10/20.
//

#import "VFYSuggestionPopup.h"
#import "Utilities.h"

@interface VFYSuggestionPopup() {
    
}

@property (nonatomic, retain) VFYVerifyApp *sharedInstance;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtCancel;
@property (weak, nonatomic) IBOutlet UIView *BodyView;
@property (weak, nonatomic) IBOutlet UILabel *LbTitle;
@property (weak, nonatomic) IBOutlet UILabel *LbOriginalName;
@property (weak, nonatomic) IBOutlet UILabel *LbSuggestedName;
@property (weak, nonatomic) IBOutlet UILabel *LbOrgNameValue;
@property (weak, nonatomic) IBOutlet UILabel *LbSgtFrstNameValue;
@property (weak, nonatomic) IBOutlet UILabel *LbSgtScnNameValue;
@property (nonatomic, weak) IBOutlet UIStackView *stackOriginalName;
@property (nonatomic, weak) IBOutlet UIStackView *stackSgtScnName;
@property (nonatomic, weak) IBOutlet UIStackView *stackSgtFrstName;

@end

@implementation VFYSuggestionPopup

@synthesize popupSelf, callMethodFlag;
@synthesize accOwnerFrstName, accOwnerLstName, crdOwnerFrstName, crdOwnerLstName;
@synthesize extCardOwnerName, extFirstName, extLastName;

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
        self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.sharedInstance = [VFYVerifyApp sharedInstance];
    [self setLocalizationText];
    //[self CustomTitle];
    [self setBrandingColor];
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    if(callMethodFlag == CardContinue || callMethodFlag == CardExtract){
        self.LbOrgNameValue.text = [extCardOwnerName capitalizedString];
        self.stackSgtScnName.hidden = true; //suggetion second option in case of card
        if([extCardOwnerName length] == 0){ // hide original option when no extract value
            self.LbOriginalName.hidden = true;
            self.stackOriginalName.hidden = true;
        }
    } else if(callMethodFlag ==  IDExtract || callMethodFlag == IDContinue){
        self.LbOrgNameValue.text = [[NSString stringWithFormat:@"%@ %@",extFirstName,extLastName] capitalizedString];
        if([extFirstName length] == 0 && [extLastName length] == 0){ // hide original option when no extract value
            self.LbOriginalName.hidden = true;
            self.stackOriginalName.hidden = true;
        }
        if([crdOwnerFrstName length] == 0 && [crdOwnerLstName length] == 0){ //hide second suggested option
            self.stackSgtScnName.hidden = true;
        }
        if([accOwnerFrstName length] == 0 && [accOwnerLstName length] == 0){ //hide first suggested option
            self.stackSgtFrstName.hidden = true;
        }
    }
    self.LbSgtFrstNameValue.text = [[NSString stringWithFormat:@"%@ %@",accOwnerFrstName,accOwnerLstName] capitalizedString];
    self.LbSgtScnNameValue.text = [[NSString stringWithFormat:@"%@ %@",crdOwnerFrstName,crdOwnerLstName] capitalizedString];
}

/**
  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.BodyView setBackgroundColor:[Utilities colorFromColorString:self.sharedInstance.bodyColor]];
    [self.LbTitle setTextColor:[Utilities colorFromColorString:self.sharedInstance.bodyTitleColor]];
    
    /* Set button style */
    [self.LbBtCancel animateOnPress:FALSE borderColor:[Utilities colorFromColorString:[self.sharedInstance buttonBorderColor]] backGroundColor:[Utilities colorFromColorString:[self.sharedInstance buttonBackgroundColor]] textColor:[Utilities colorFromColorString:[self.sharedInstance buttonTextColor]]];
    /* set the corner */
    self.BodyView.layer.cornerRadius = 30;
    self.BodyView.layer.borderWidth = 1.0;
    self.BodyView.layer.borderColor = [[UIColor grayColor] CGColor];
}

/**
   Add localization text on labels
*/
- (void) setLocalizationText {
    self.LbTitle.text = NSLocalizedStringWithDefaultValue(@"mb_name_suggestion_title", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Not sure that we got your name right. Here's a suggestion: ", nil);
    self.LbOriginalName.text = NSLocalizedStringWithDefaultValue(@"mb_name_suggestion_orignial_name", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Original name:", nil);
    self.LbSuggestedName.text = NSLocalizedStringWithDefaultValue(@"mb_name_suggestion_suggested_name", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Suggested name:", nil);
    [self.LbBtCancel setTitle:NSLocalizedStringWithDefaultValue(@"mb_name_suggestion_cancel_button", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Cancel", nil) forState:UIControlStateNormal];
}

/**
  Continue button to start scanning
*/
- (IBAction)BTNCancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    [popupSelf callBackFromPopupClose:callMethodFlag PassValue:nil];
}

- (IBAction)radioButtonSelected:(id)sender {
    switch ([sender tag ]) {
        case OriginalName:
            {
                [self dismissViewControllerAnimated:YES completion:nil];
                NSMutableDictionary *orgName = [NSMutableDictionary dictionary];
                if(callMethodFlag == CardContinue || callMethodFlag == CardExtract){ // this will incase of card
                    [orgName setValue:self.LbOrgNameValue.text forKey:@"cardownername"];
                    [popupSelf callBackFromPopupClose:callMethodFlag PassValue:orgName];
                } else if(callMethodFlag ==  IDExtract || callMethodFlag == IDContinue){ // this will in case of id
                    [orgName setValue:extFirstName forKey:@"firstname"];
                    [orgName setValue:extLastName forKey:@"lastname"];
                    [popupSelf callBackFromPopupClose:callMethodFlag PassValue:orgName];
                }
                break;
            }
        case SuggestedName:
            {
                [self dismissViewControllerAnimated:YES completion:nil];
                //[popupSelf callBackFromPopupClose:callMethodFlag PassValue:self.LbSgtNameValue.text];
                NSMutableDictionary *sgtName = [NSMutableDictionary dictionary];
                if(callMethodFlag == CardContinue || callMethodFlag == CardExtract){ // this will incase of card
                    [sgtName setValue:self.LbSgtFrstNameValue.text forKey:@"cardownername"];
                    [popupSelf callBackFromPopupClose:callMethodFlag PassValue:sgtName];
                } else if(callMethodFlag ==  IDExtract || callMethodFlag == IDContinue){ // this will in case of id
                    [sgtName setValue:accOwnerFrstName forKey:@"firstname"];
                    [sgtName setValue:accOwnerLstName forKey:@"lastname"];
                    [popupSelf callBackFromPopupClose:callMethodFlag PassValue:sgtName];
                }
                break;
            }
        case ScnSuggestedName:
            {
                [self dismissViewControllerAnimated:YES completion:nil];
                NSMutableDictionary *sgtName = [NSMutableDictionary dictionary];
                if(callMethodFlag ==  IDExtract || callMethodFlag == IDContinue){ // this will in case of id
                    [sgtName setValue:crdOwnerFrstName forKey:@"firstname"];
                    [sgtName setValue:crdOwnerLstName forKey:@"lastname"];
                    [popupSelf callBackFromPopupClose:callMethodFlag PassValue:sgtName];
                }
                break;
            }
        default:
            break;
    }
}

@end
