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

#import "ApiUtillity.h"
#import "SVProgressHUD.h"
#import "Reachability.h"

@interface ApiUtillity ()

@end

@implementation ApiUtillity

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

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

//For Rounded Corner Shape
+ (UIView*)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andRadius:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity
{
    view.layer.cornerRadius = view.frame.size.height/2;
    view.layer.masksToBounds = true;
    view.layer.borderWidth = 2.0;
    view.layer.borderColor = [UIColor colorWithRed:243.0/255.0 green:13.0/255.0 blue:41.0/255.0 alpha:1.0f].CGColor;
    
    CGRect shadowFrame; // Modify this if needed
    shadowFrame.size.width = 0.f;
    shadowFrame.size.height = 0.f;
    shadowFrame.origin.x = 0.f;
    shadowFrame.origin.y = 0.f;
    UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame];
    shadow.userInteractionEnabled = NO; // Modify this if needed
    shadow.layer.shadowColor = color.CGColor;
    shadow.layer.shadowOffset = shadowOffset;
    shadow.layer.shadowRadius = shadowRadius;
    shadow.layer.masksToBounds = true;
    shadow.clipsToBounds = false;
    shadow.layer.shadowOpacity = shadowOpacity;
    [view.superview insertSubview:shadow belowSubview:view];
    [shadow addSubview:view];
    return shadow;
}

+ (void)setRoundedCornurRadius:(UIImageView*)CorurRadius SetColor:(UIColor*)color{
    
    CorurRadius.layer.masksToBounds = true;
    CorurRadius.layer.cornerRadius = CorurRadius.frame.size.height/2;
    CorurRadius.layer.shadowColor = color.CGColor;
    CorurRadius.layer.borderWidth = 2.0;
    CorurRadius.layer.borderColor = color.CGColor;
    CGRect shadowRect = CGRectInset(CorurRadius.bounds, 0, 4);  // inset top/bottom
    CorurRadius.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath];
    CorurRadius.layer.shadowColor = [[UIColor whiteColor] CGColor];
    CorurRadius.layer.shadowOffset = CGSizeMake(10.0f,10.0f);
    CorurRadius.layer.shadowOpacity = .8f;
    CorurRadius.layer.shadowRadius = 10.0f;
}

//For Navigation
+ (void)setTransculantNavigation:(UINavigationController *)navigation{
    [navigation.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor blackColor]}];
    navigation.navigationBarHidden =NO;
    NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    if ([[ver objectAtIndex:0] intValue] >= 7)
    {
        navigation.navigationBar.barTintColor = [UIColor blackColor];
        //        [UIColor colorWithRed:0.996 green:0.863 blue:0.153 alpha:1];
        navigation.navigationBar.translucent = NO;
    }else
    {
        navigation.navigationBar.tintColor = [UIColor whiteColor];
        //        [UIColor colorWithRed:0.996 green:0.863 blue:0.153 alpha:1];
    }
}

+ (void)setSVProgressHUDInfoView:(UIView *)view{
    [SVProgressHUD showErrorWithStatus:@"Request Timed Out"];
    [SVProgressHUD dismissWithDelay:2.0];
}

+ (void)setSVProgressHUDView:(UIView *)view{
    Reachability *myNetwork = [Reachability reachabilityWithHostname:@"google.com"];
    NetworkStatus myStatus = [myNetwork currentReachabilityStatus];
    
    NSLog(@"%ld",(long)myStatus);
    if (myStatus == 2) {
        NSLog(@"");
        
    }
    //    [SVProgressHUD show];
    [SVProgressHUD showWithStatus:@"Loading..."];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleLight];
//    [SVProgressHUD setFadeInAnimationDuration:1.0];
    
    //    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    //    hud.center = view.center;
    //    hud.backgroundColor = [UIColor whiteColor];
    //    hud.label.text = @"Loading...";
    //    [hud showAnimated:true];
    
    //    [MBProgressHUD showHUDAddedTo:view animated:YES];
}
+ (void)dissmissSVProgressHUDView:(UIView *)view{
    [SVProgressHUD dismiss];
}
+ (void)dissmissSVProgressHUDViewWithSuccess:(UIView *)view WithMessage:(NSString *)message{
    
    [SVProgressHUD showSuccessWithStatus:message];
    [SVProgressHUD dismissWithDelay:1];
    
}

+ (void)checkForNetwork:(UIView *)view
{
    Reachability *myNetwork = [Reachability reachabilityWithHostname:@"google.com"];
    NetworkStatus myStatus = [myNetwork currentReachabilityStatus];
    
    switch (myStatus) {
        case NotReachable:{
            NSLog(@"There's no internet connection at all. Display error message now.");
            
            UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Internet" message:@"No Internet Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
        }
            break;
            
        case ReachableViaWWAN:{
            NSLog(@"We have a 3G connection");
            UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Internet" message:@"3G connection..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
        }
            break;
            
        case ReachableViaWiFi:{
            NSLog(@"We have WiFi.");
            UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Internet" message:@"WiFi connection..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [Alert show];
        }
            break;
            
        default:
            break;
    }
}


+ (NSInteger) getProjectNumber{
    NSMutableArray *ProjectArray;
    NSString *TempProjectID;
    NSUInteger TempproID;
    
    ProjectArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"ProjectArray"];
    if ((TempProjectID = [[NSUserDefaults standardUserDefaults]objectForKey:@"PROJECT_ID"])){
        
        NSLog(@"%@",ProjectArray);
        NSLog(@"%lu",(unsigned long)TempproID);
        
        NSString *GetNumber;
        NSUInteger i;
        for (i = 0; i < ProjectArray.count; i++) {
            GetNumber = [[ProjectArray valueForKey:@"project_id"]objectAtIndex:i];
            if ([GetNumber isEqualToString:TempProjectID]) {
                NSLog(@"%lu",(unsigned long)i);
                TempproID = i;
            }
        }
    }
    
    NSLog(@"%lu",(unsigned long)TempproID);
    return TempproID;
}

+ (NSInteger) getJobNumber{
    NSMutableArray *ProjectArray;
    NSMutableArray *JobArray;
    NSString *TempJobID,*TempProjectID;
    NSUInteger TempjobID = 0;
    
    TempProjectID = [[NSUserDefaults standardUserDefaults]objectForKey:@"PROJECT_ID"];
    ProjectArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"ProjectArray"];
    int i;
    for (i=0; i<ProjectArray.count; i++) {
        NSString *proID;
        proID = [[ProjectArray objectAtIndex:i] valueForKey:@"project_id"];
        if (proID == TempProjectID) {
            JobArray = [[ProjectArray objectAtIndex:i]valueForKey:@"jobs"];
            
            if ((TempJobID = [[NSUserDefaults standardUserDefaults]objectForKey:@"JOB_ID"])){
                NSLog(@"%@",JobArray);
                NSLog(@"%lu",(unsigned long)TempjobID);
                
                NSString *GetNumber;
                int i;
                for (i = 0; i < JobArray.count; i++) {
                    GetNumber = [[JobArray valueForKey:@"job_id"]objectAtIndex:i];
                    if ([GetNumber isEqualToString:TempJobID]) {
                        NSLog(@"%d",i);
                        TempjobID = i;
                    }
                }
            }
        }
    }
    
    NSLog(@"%lu",(unsigned long)TempjobID);
    return TempjobID;
}

+ (NSInteger) getSubNumber{
    NSMutableArray *SubArray;
    NSString *TempSubID;
    NSUInteger TempsubID = 0;
    
    SubArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"SubArray"];
//    TempSubID = [[NSUserDefaults standardUserDefaults]objectForKey:@"SUB_ID"];
    
    if ((TempSubID = [[NSUserDefaults standardUserDefaults]objectForKey:@"SUB_ID"])){
        int y;
        for (y=0; y<SubArray.count; y++) {
            NSString *subID;
            subID = [[SubArray objectAtIndex:y] valueForKey:@"id"];
            if ([subID isEqualToString:TempSubID]) {
                TempsubID = y;
            }
        }
    }
    return TempsubID;
}


+ (NSInteger) getEmployeeNumber{
    NSMutableArray *EmployeeArray;
    NSString *TempEmployeeID;
    NSUInteger TempempID = 0;
    
    EmployeeArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"EmployeeArray"];
//    TempEmployeeID = [[NSUserDefaults standardUserDefaults]objectForKey:@"EMPLOYEE_ID"];
    
    if ((TempEmployeeID = [[NSUserDefaults standardUserDefaults]objectForKey:@"EMPLOYEE_ID"])){
        int x;
        for (x=0; x<EmployeeArray.count; x++) {
            NSString *empID;
            empID = [[EmployeeArray objectAtIndex:x] valueForKey:@"id"];
            if ([empID isEqualToString:TempEmployeeID]) {
                TempempID = x;
            }
        }
    }
    return TempempID;
}


@end
