//
//  HomeVC.m
//  Gilliatte
//
//  Created by Lead on 11/08/16.
//  Copyright © 2016 Lead infosoft. All rights reserved.
//

#import "HomeVC.h"
#import "SlideMenuController.h"
#import "EnterDailyReportVC.h"
#import "AFNetworking.h"
#import "ApiUtillity.h"
#import "Reachability.h"

@interface HomeVC (){
    NSMutableArray *UserDataArray;
}

@end

@implementation HomeVC

#pragma mark- ViewDidload
- (void)viewDidLoad {
    [super viewDidLoad];
    //_txt_UserName.text = @"demo@gmail.com";
    //_txt_Password.text = @"demo";
    
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self setNeedsStatusBarAppearanceUpdate];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
}

-(void)viewWillAppear:(BOOL)animated{
    NSData *GetUserData;
    if ((GetUserData = [[NSUserDefaults standardUserDefaults] objectForKey:@"USER_PROFILE"])) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        EnterDailyReportVC *VC = (EnterDailyReportVC *)[storyboard instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
        [self.navigationController pushViewController:VC animated:true];
    }
    [[self navigationController] setNavigationBarHidden:YES animated:false];
}

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

#pragma mark- All Methode
- (UIStatusBarStyle) preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark- ButtonAction
- (IBAction)btn_Login_Handler:(id)sender
{
    _btn_Login.userInteractionEnabled = false;
    
    NSString *Token;
    NSString *UUID;
    
    if ((Token = [[NSUserDefaults standardUserDefaults] objectForKey:@"PUSH_TOKEN"]) && (UUID = [[NSUserDefaults standardUserDefaults] objectForKey:@"UUID"])) {
    }

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager.requestSerializer setTimeoutInterval:150.0];

    
    NSDictionary *params = @{@"email": _txt_UserName.text,@"password":_txt_Password.text,@"dev_type":@"1",@"dev_token":Token,@"code":UUID};
    
    [manager POST:@"http://p4bdesigns.com/Gilliatteweb/app/api3/login_user.php?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (responseObject != nil) {
            UserDataArray = [[NSMutableArray alloc]init];
            NSMutableArray *TempArray;
            if ((TempArray = [responseObject valueForKey:@"success"])) {
                UserDataArray = TempArray;
                NSLog(@"%@",UserDataArray);
                NSData *SaveUserData = [NSKeyedArchiver archivedDataWithRootObject:UserDataArray];
                [[NSUserDefaults standardUserDefaults] setObject:SaveUserData forKey:@"USER_PROFILE"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
//                FOR LOGIN
                if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    EnterDailyReportVC *VC = (EnterDailyReportVC *)[storyboard instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
                    [self.navigationController pushViewController:VC animated:true];
                }
                else{
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStory_iPad" bundle:nil];
                    EnterDailyReportVC *VC = (EnterDailyReportVC *)[storyboard instantiateViewControllerWithIdentifier:@"EnterDailyReportVC"];
                    [self.navigationController pushViewController:VC animated:true];
                }
            }
            else{
                UIAlertView *alert = [[UIAlertView alloc]init];
                alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Wrong email address or password" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
                [alert show];
                _btn_Login.userInteractionEnabled = true;
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        _btn_Login.userInteractionEnabled = true;
        NSLog(@"Error: %@", error);
        NSInteger statusCode = error.code;
        NSLog(@"%d",(int)statusCode);
        if(statusCode == -1001) {
            // request timed out
            NSLog(@"request timed out");
        }else if (statusCode == -1009 || statusCode == -1004) {
            // no internet connectivity
            NSLog(@"No Internet Connection");
            //No Internet Connection
            UIAlertView *alert = [[UIAlertView alloc]init];
            alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"No Internet Connection" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
    
    //FOR TEST VIEWCONTROLLER
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    ViewControllerTEST *VC = (ViewControllerTEST *)[storyboard instantiateViewControllerWithIdentifier:@"ViewControllerTEST"];
//    [self.navigationController pushViewController:VC animated:true];
}

@end