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

#import "VFYIDCountryWrapper.h"
#import "VFYFrontGuideWrapper.h"
#import "VFYButton.h"
#import "VFYConstant.h"
#import "ResultModelIdFail.h"

@interface VFYIDCountryWrapper ()

@property (weak, nonatomic) IBOutlet UILabel *LbChooseCountry;
@property (weak, nonatomic) IBOutlet UILabel *LbChooseType;
@property (weak, nonatomic) IBOutlet UILabel *LbIDInstruction;
@property (weak, nonatomic) IBOutlet VFYButton *LbBtContinue;
@property (nonatomic, retain) VFYVerifyApp *sharedInstance;
@property (weak, nonatomic) IBOutlet UITextField *tvCountry;
@property (weak, nonatomic) IBOutlet UITextField *tVDocumentType;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoPow;
@property (weak, nonatomic) IBOutlet UILabel *LbLogoBy;
@property (weak, nonatomic) IBOutlet UIScrollView *BodyScrollView;
@property (nonatomic, strong) UIPickerView *countryPickerView;
@property (nonatomic, strong) UIPickerView *documentPickerView;
@property (nonatomic, strong) NSArray *countryNames;
@property (nonatomic, strong) NSArray *documentType;
@property (strong, nonatomic) UIColor *fontColor;
@property (strong, nonatomic) UIColor *headerColor;
@property (strong, nonatomic) UIColor *backgroundColor;
@property (strong, nonatomic) UIColor *headerFontColor;
@property (strong, nonatomic) ResultModelIdFail *idFail;
@property (nonatomic) CFTimeInterval idFailDurationStart;
@property (nonatomic) BOOL countrySelected;
@property (nonatomic) BOOL documentTypeSelected;
@property (weak, nonatomic) IBOutlet UILabel *lblErrCountry;
@property (weak, nonatomic) IBOutlet UILabel *lblErrDocument;
@property (nonatomic, strong) NSBundle *bundlePath;

@end

@implementation VFYIDCountryWrapper

@synthesize selectionType;
@synthesize idFail;
@synthesize idFailDurationStart;

- (void)viewDidLoad {
    [super viewDidLoad];
    if (@available(iOS 13.0, *)) {
         self.view.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
    self.bundlePath = [NSBundle bundleForClass:[VFYVerifyApp class]];
    self.countrySelected = false;
    self.documentTypeSelected = false;
    self.sharedInstance = [VFYVerifyApp sharedInstance];
    self.fontColor = [Utilities colorFromColorString:self.sharedInstance.bodyTextColor];
    self.headerColor = [Utilities colorFromColorString:self.sharedInstance.headerColor];
    self.backgroundColor = [Utilities colorFromColorString:self.sharedInstance.bodyColor];
    self.headerFontColor = [Utilities colorFromColorString:self.sharedInstance.headerTextColor];
    
    [self setLocalizationText];
    [self addCountryPickerView];
    [self addDocumentPickerView];
    [self setCountry];
    self.documentType = @[@"Select", @"Passport", @"Driver's License", @"Identity Card"];
    [self CustomTitle];
    [self.BodyScrollView setBackgroundColor:[Utilities colorFromColorString:self.sharedInstance.bodyColor]];
    [self setBrandingColor];
    idFail = [[ResultModelIdFail alloc] init];
    //Id scan failed
    idFail.isSuccessful = false;
    idFailDurationStart = CACurrentMediaTime();
}

/**
  Add localization text on labels
*/
- (void) setLocalizationText
{
    self.LbChooseCountry.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_chooseCountry", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Choose issuing country/region", nil);
    self.LbChooseType.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_selectId", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Select ID type", nil);
    self.LbIDInstruction.text = NSLocalizedStringWithDefaultValue(@"mb_identityFailed_userguidance_msg_1", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Use a valid government-issued photo ID. A residence permit is also a valid identity card.", nil);
    [self.LbBtContinue setTitle:NSLocalizedStringWithDefaultValue(@"mb_identity_button_1", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Continue", nil) forState:UIControlStateNormal];
}

- (void) setBrandingColor
{
    [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]]];
}

- (void) addCountryPickerView {
    self.countryPickerView = [[UIPickerView alloc] init];
    self.countryPickerView.delegate = self;
    self.countryPickerView.dataSource = self;
    self.tvCountry.inputView = self.countryPickerView;
    UIToolbar *toolBarCnty = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    [toolBarCnty setBarTintColor:self.headerColor];
    [toolBarCnty setTintColor:self.headerFontColor];
    UIBarButtonItem *doneCnty =[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissedCountryPicker)]; //When clicked "Done" the UIPickerView disappears.
    UIBarButtonItem *spaceCnty = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolBarCnty setItems:[NSArray arrayWithObjects:spaceCnty,doneCnty,nil]];
    [self.tvCountry setInputAccessoryView:toolBarCnty];
}

- (void) addDocumentPickerView {
    self.documentPickerView = [[UIPickerView alloc] init];
    self.documentPickerView.delegate = self;
    self.documentPickerView.dataSource = self;
    self.tVDocumentType.inputView = self.documentPickerView;
    UIToolbar *toolBarDoc = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    [toolBarDoc setBarTintColor:self.headerColor];
    [toolBarDoc setTintColor:self.headerFontColor];
    UIBarButtonItem *doneBtnDoc =[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dismissedDocumentPicker)]; //When clicked "Done" the UIPickerView disappears.
    UIBarButtonItem *spaceDoc = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolBarDoc setItems:[NSArray arrayWithObjects:spaceDoc,doneBtnDoc,nil]];
    [self.tVDocumentType setInputAccessoryView:toolBarDoc];
}

- (IBAction)BTNContinue:(id)sender {
    NSString *strMessage = [NSString string];
    if(!self.countrySelected && !self.documentTypeSelected){
        strMessage = NSLocalizedStringWithDefaultValue(@"mb_valdiationId_select_country", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Please select country", nil);
        [self displayMessage:self.lblErrCountry Message:strMessage Value:@""];
        strMessage = NSLocalizedStringWithDefaultValue(@"mb_valdiationId_select_document_type", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Please select id type", nil);
        [self displayMessage:self.lblErrDocument Message:strMessage Value:@""];
    } else if(!self.countrySelected){
        strMessage = NSLocalizedStringWithDefaultValue(@"mb_valdiationId_select_country", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Please select country", nil);
        [self displayMessage:self.lblErrCountry Message:strMessage Value:@""];
    } else if(!self.documentTypeSelected){
        strMessage = NSLocalizedStringWithDefaultValue(@"mb_valdiationId_select_document_type", [self.sharedInstance language], [self.sharedInstance languageBundlePath], @"Please select id type", nil);
        [self displayMessage:self.lblErrDocument Message:strMessage Value:@""];
    } else {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:VFY_MAIN_STORYBOARD bundle:self.bundlePath];
        VFYFrontGuideWrapper *newView = [storyboard instantiateViewControllerWithIdentifier:VFY_IDFRONTGUIDE_STORYBOARD];
        newView.selectionType = self.selectionType;
        newView.stepType = @"first";
        newView.idFail = idFail;
        newView.idFailDurationStart = idFailDurationStart;
        [self.navigationController pushViewController:newView animated:YES];
    }
}

/**
   Display the validation message
*/
- (void) displayMessage: (UILabel*) labelName Message:(NSString*)message Value:(NSString*) value {
    if ([value isEqualToString:@""]) {
        labelName.hidden = false;
        labelName.text = message;
        labelName.textColor = [UIColor redColor];
    } else {
        if(!labelName.hidden){
           labelName.hidden = true;
           labelName.text = @"";
        }
    }
}

-(void)dismissedCountryPicker{
    [self.tvCountry resignFirstResponder];
}

-(void)dismissedDocumentPicker{
    [self.tVDocumentType resignFirstResponder];
}

#pragma mark - UIPickerViewDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (pickerView == self.countryPickerView || pickerView == self.documentPickerView) {
        return 1;
    }
    return 0;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == self.countryPickerView) {
        return [self.countryNames count];
    } else if(pickerView == self.documentPickerView){
        return [self.documentType count];
    }
    return 0;
}

#pragma mark - UIPickerViewDelegate

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (pickerView == self.countryPickerView) {
        return self.countryNames[row];
    } else if(pickerView == self.documentPickerView){
        return self.documentType[row];
    }
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == self.countryPickerView) {
        if(row != 0){
            self.countrySelected=true;
            self.tvCountry.text = self.countryNames[row];
            idFail.country = self.tvCountry.text;
            [self displayMessage:self.lblErrCountry Message:@"" Value:self.tvCountry.text];
        }
    }else if(pickerView == self.documentPickerView){
        if(row != 0){
            self.documentTypeSelected=true;
            self.tVDocumentType.text = self.documentType[row];
            idFail.documenttype = self.tVDocumentType.text;
            [self displayMessage:self.lblErrDocument Message:@"" Value:self.tVDocumentType.text];
        }
    }
}

-(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) setCountry
{
    // Country code
    self.countryNames = @[
    @"Select",@"Afghanistan", @"Albania", @"Algeria",
    @"American Samoa", @"Andorra",
    @"Angola", @"Anguilla", @"Antigua and Barbuda",
    @"Argentina", @"Armenia",
    @"Aruba", @"Ascension",
    @"Australia", @"Australian External Territories",
    @"Austria", @"Azerbaijan",
    @"Bahamas", @"Bahrain",
    @"Bangladesh", @"Barbados",
    @"Barbuda", @"Belarus",
    @"Belgium", @"Belize",
    @"Benin", @"Bermuda",
    @"Bhutan", @"Bolivia",
    @"Bosnia and Herzegovina",
    @"Botswana", @"Brazil",
    @"British Indian Ocean Territory",
    @"British Virgin Islands",
    @"Brunei", @"Bulgaria",
    @"Burkina Faso", @"Burundi",
    @"Cambodia", @"Cameroon",
    @"Canada", @"Cape Verde",
    @"Cayman Islands", @"Central African Republic",
    @"Chad", @"Chile", @"China",
    @"Christmas Island", @"Cocos-Keeling Islands",
    @"Colombia", @"Comoros",
    @"Congo", @"Congo, Dem. Rep. of (Zaire)",
    @"Cook Islands", @"Costa Rica",
    @"Ivory Coast", @"Croatia",
    @"Cuba", @"Curacao",
    @"Cyprus", @"Czech Republic",
    @"Denmark", @"Diego Garcia",
    @"Djibouti", @"Dominica",
    @"Dominican Republic", @"East Timor",
    @"Easter Island", @"Ecuador",
    @"Egypt", @"El Salvador",
    @"Equatorial Guinea", @"Eritrea",
    @"Estonia", @"Ethiopia",
    @"Falkland Islands", @"Faroe Islands",
    @"Fiji",
    @"Finland",
    @"France",
    @"French Antilles",
    @"French Guiana",
    @"French Polynesia",
    @"Gabon"                                        ,
    @"Gambia"                                       ,
    @"Georgia"                                      ,
    @"Germany"                                      ,
    @"Ghana"                                        ,
    @"Gibraltar"                                    ,
    @"Greece"                                       ,
    @"Greenland"                                    ,
    @"Grenada"                                      ,
    @"Guadeloupe"                                   ,
    @"Guam"                                         ,
    @"Guatemala"                                    ,
    @"Guinea"                                       ,
    @"Guinea-Bissau"                                ,
    @"Guyana"                                       ,
    @"Haiti"                                        ,
    @"Honduras"                                     ,
    @"Hong Kong SAR China"                          ,
    @"Hungary"                                      ,
    @"Iceland"                                      ,
    @"India"                                        ,
    @"Indonesia"                                    ,
    @"Iran"                                         ,
    @"Iraq"                                         ,
    @"Ireland"                                      ,
    @"Israel"                                       ,
    @"Italy"                                        ,
    @"Jamaica"                                      ,
    @"Japan"                                        ,
    @"Jordan"                                       ,
    @"Kazakhstan"                                   ,
    @"Kenya"                                        ,
    @"Kiribati"                                     ,
    @"North Korea"                                  ,
    @"South Korea"                                  ,
    @"Kuwait"                                       ,
    @"Kyrgyzstan"                                   ,
    @"Laos"                                         ,
    @"Latvia"                                      ,
    @"Lebanon"                                     ,
    @"Lesotho"                                      ,
    @"Liberia"                                     ,
    @"Libya"                                        ,
    @"Liechtenstein"                                ,
    @"Lithuania"                                    ,
    @"Luxembourg"                                   ,
    @"Macau SAR China"                              ,
    @"Macedonia"                                    ,
    @"Madagascar"                                   ,
    @"Malawi"                                       ,
    @"Malaysia"                                     ,
    @"Maldives"                                     ,
    @"Mali"                                         ,
    @"Malta"                                        ,
    @"Marshall Islands"                             ,
    @"Martinique"                                   ,
    @"Mauritania"                                   ,
    @"Mauritius"                                    ,
    @"Mayotte"                                      ,
    @"Mexico"                                       ,
    @"Micronesia"                                   ,
    @"Midway Island"                                ,
    @"Micronesia"                                   ,
    @"Moldova"                                      ,
    @"Monaco"                                       ,
    @"Mongolia"                                     ,
    @"Montenegro"                                   ,
    @"Montserrat"                                   ,
    @"Morocco"                                      ,
    @"Myanmar"                                      ,
    @"Namibia"                                      ,
    @"Nauru"                                        ,
    @"Nepal"                                        ,
    @"Netherlands"                                  ,
    @"Netherlands Antilles"                         ,
    @"Nevis"                                        ,
    @"New Caledonia"                                ,
    @"New Zealand"                                  ,
    @"Nicaragua"                                    ,
    @"Niger"                                        ,
    @"Nigeria"                                      ,
    @"Niue"                                         ,
    @"Norfolk Island"                               ,
    @"Northern Mariana Islands"                     ,
    @"Norway"                                       ,
    @"Oman"                                         ,
    @"Pakistan"                                     ,
    @"Palau"                                        ,
    @"Palestinian Territory"                        ,
    @"Panama"                                       ,
    @"Papua New Guinea"                             ,
    @"Paraguay"                                     ,
    @"Peru"                                         ,
    @"Philippines"                                  ,
    @"Poland"                                       ,
    @"Portugal"                                     ,
    @"Puerto Rico"                                  ,
    @"Puerto Rico"                                  ,
    @"Qatar"                                        ,
    @"Reunion"                                      ,
    @"Romania"                                      ,
    @"Russia"                                       ,
    @"Rwanda"                                       ,
    @"Samoa"                                        ,
    @"San Marino"                                   ,
    @"Saudi Arabia"                                 ,
    @"Senegal"                                      ,
    @"Serbia"                                       ,
    @"Seychelles"                                   ,
    @"Sierra Leone"                                 ,
    @"Singapore"                                    ,
    @"Slovakia"                                     ,
    @"Slovenia"                                     ,
    @"Solomon Islands"                              ,
    @"South Africa"                                 ,
    @"South Georgia and the South Sandwich Islands" ,
    @"Spain"                                        ,
    @"Sri Lanka"                                    ,
    @"Sudan"                                        ,
    @"Suriname"                                     ,
    @"Swaziland"                                    ,
    @"Sweden"                                       ,
    @"Switzerland"                                  ,
    @"Syria"                                        ,
    @"Taiwan"                                       ,
    @"Tajikistan"                                   ,
    @"Tanzania"                                     ,
    @"Thailand"                                     ,
    @"Timor Leste"                                  ,
    @"Togo"                                         ,
    @"Tokelau"                                      ,
    @"Tonga"                                        ,
    @"Trinidad and Tobago"                          ,
    @"Tunisia"                                      ,
    @"Turkey"                                       ,
    @"Turkmenistan"                                 ,
    @"Turks and Caicos Islands"                     ,
    @"Tuvalu"                                       ,
    @"Uganda"                                       ,
    @"Ukraine", @"United Arab Emirates",
    @"United Kingdom", @"United States",
    @"Uruguay", @"U.S. Virgin Islands",
    @"Uzbekistan", @"Vanuatu",
    @"Venezuela", @"Vietnam",
    @"Wake Island", @"Wallis and Futuna",
    @"Yemen", @"Zambia", @"Zanzibar",@"Zimbabwe"
    ];
}

@end
