//
//  checkCustomerData.m
//  SweetFleet
//
//  Created by Claus Richter on 4/5/18.
//
//

#import "checkCustomerData.h"

#import "NSURLSession+SynchronousTask.h"

//#define NSLog(...)

@interface checkCustomerData ()

@end

@implementation checkCustomerData

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) checkCustomerDataFiles:  (NSString *)receivedinputCommunicationKey{
    
    //NSLog(@"checkCustomerDataFiles");
    // ***************************
    // get user data passowrd and username
    //__autoreleasing NSError* error = nil;
    NSMutableArray *arrayCustomerName   = [[NSMutableArray alloc] init];
    NSMutableArray *arrayUserName   = [[NSMutableArray alloc] init];
    NSMutableArray *arrayUserPassword   = [[NSMutableArray alloc] init];
    NSMutableArray *arrayUserFleetControlID   = [[NSMutableArray alloc] init];
    NSMutableArray *arrayUserExtras   = [[NSMutableArray alloc] init];
       
    
    NSString *urlWithConfigID =  @"https://www.gyronimoserver.com/api/flightpadusers/";
    NSURL *url = [NSURL URLWithString:urlWithConfigID];
    NSError *error = nil;
    NSURLResponse *response = nil;
    NSData *data = [[NSURLSession sharedSession] sendSynchronousDataTaskWithURL:url returningResponse:&response error:&error];        
    
    NSDictionary *dictionaryAll = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
    //NSLog(@"dictionaryAll %@", dictionaryAll);
    
    
    NSArray *itemsArray = [dictionaryAll valueForKey:@"data"];
    //NSLog(@"itemsArray %@", itemsArray);            
    
   //NSLog(@"2 checkCustomerDataFiles");
          
     for(int i =0; i<itemsArray.count; i++) {         
      
         NSDictionary *dictionaryItem = [itemsArray objectAtIndex:i];
         //NSLog(@"customer_name %@", [dictionaryItem valueForKey: @"customer_name"] );
         //NSLog(@"username %@", [dictionaryItem valueForKey: @"username"] );
         //NSLog(@"password %@", [dictionaryItem valueForKey: @"password"] );
         //NSLog(@"fc_com_key %@", [dictionaryItem valueForKey: @"fc_com_key"] );
         //NSLog(@"attributes %@", [dictionaryItem valueForKey: @"attributes"] );
         
         [arrayCustomerName              addObject: [dictionaryItem valueForKey: @"customer_name"] ];
         [arrayUserName                  addObject: [[dictionaryItem valueForKey: @"username"]  stringByReplacingOccurrencesOfString:@" " withString:@""]];
         [arrayUserPassword              addObject: [[dictionaryItem valueForKey: @"password"] stringByReplacingOccurrencesOfString:@" " withString:@""]];
         [arrayUserFleetControlID        addObject: [[dictionaryItem valueForKey: @"fc_com_key"] stringByReplacingOccurrencesOfString:@" " withString:@""]];
         
         if ([[dictionaryItem valueForKey: @"attributes"] isEqualToString:@""]) {
              [arrayUserExtras        addObject: @"none" ];     
         }else{            
              [arrayUserExtras        addObject: [[dictionaryItem valueForKey: @"attributes"] stringByReplacingOccurrencesOfString:@" " withString:@""]];
         }
         
         //NSLog(@">%@< >%@< >%@<",  [dictionaryItem valueForKey: @"customer_name"] ,  [dictionaryItem valueForKey: @"username"] ,  [dictionaryItem valueForKey: @"password"] );
     }

    
   
    //NSLog(@"3 checkCustomerDataFiles");
   
    
    NSString *key = @"userData_";
    [[NSUserDefaults standardUserDefaults]  setObject:arrayCustomerName     forKey:[key stringByAppendingString:@"arrayCustomerName"]];
    [[NSUserDefaults standardUserDefaults]  setObject:arrayUserName     forKey:[key stringByAppendingString:@"arrayUserName"]];
    [[NSUserDefaults standardUserDefaults]  setObject:arrayUserPassword     forKey:[key stringByAppendingString:@"arrayUserPassword"]];
    [[NSUserDefaults standardUserDefaults]  setObject:arrayUserFleetControlID     forKey:[key stringByAppendingString:@"arrayUserFleetControlID"]];
    [[NSUserDefaults standardUserDefaults]  setObject:arrayUserExtras     forKey:[key stringByAppendingString:@"arrayUserExtras"]];
    
    //NSLog(@"arrayCustomerName %@", arrayCustomerName);
    //NSLog(@"arrayUserPassword %@", arrayUserPassword);
    //NSLog(@"arrayUserFleetControlID %@", arrayUserFleetControlID);
    
    //NSLog(@"4 checkCustomerDataFiles");
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
