//
//  ConnectToServer.m
//  AppyIslam
//
//  Created by Bhavesh virani on 17/06/14.
//  Copyright (c) 2014 Haresh. All rights reserved.
//

#import "ConnectToServer.h"
#import "ApiUtillity.h"

@implementation ConnectToServer
@synthesize command,receivedData_get;
-(void)CallGetWithUrl:(NSString *)url {
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url1 = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120];
    NSURLConnection *registerURLConnection = [NSURLConnection connectionWithRequest:request delegate:self];
    
    if (registerURLConnection)
    {
        receivedData=[[NSMutableData alloc]init];
    }
    
}
-(void)CallPostWithUrl:(NSString *)url andDataDictionary:(NSMutableDictionary*)dictionary{
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];
    for (id key in dictionary)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@",[dictionary valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:body];
    NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    
    NSLog(@"URL dotsale:--->%@",url);
    [request setURL:[NSURL URLWithString:url]];
    
    
    NSURLConnection *registerURLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    
    if (registerURLConnection)
    {
        receivedData=[[NSMutableData alloc]init];
    }
    
}



//-(void) callPostWithUrlForProfile:(NSString *)url andDataDictionary:(NSMutableDictionary*)dictionary andImage:(UIImage *)image
//{
//    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
//    [request setHTTPShouldHandleCookies:NO];
//    [request setTimeoutInterval:300];
//    [request setHTTPMethod:@"POST"];
//
//    NSString *boundary = @"---------------------------14737809831466499882746641449";
//
//    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
//    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
//
//    NSMutableData *body = [NSMutableData data];
//
//
//    if (image != nil)
//    {
//        NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
//        NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
//        NSString *photoID = [NSString stringWithFormat:@"%@",timeStampObj];
//
//        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"%@\"; filename=\"%@.png\"\r\n",@"profile_image",@"bhavesh"] dataUsingEncoding:NSUTF8StringEncoding]];
//        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//
//        [body appendData:[NSData dataWithData:UIImageJPEGRepresentation(image ,0.13)]];
//        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//    }
//
//
//
//    NSLog(@"%@",image);
//
//    for (id key in dictionary)
//    {
//        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
//        [body appendData:[[NSString stringWithFormat:@"%@",[dictionary valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
//        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//    }
//
//    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//
//    [request setHTTPBody:body];
//
//
//    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
//    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//
//    [request setURL:[NSURL URLWithString:url]];
//
//
//
//
//
//    NSURLConnection * editconn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
//
//    if (editconn)
//    {
//        receivedData=[[NSMutableData alloc]init];
//    }
//
//    [editconn start];
//
//
//
//}
//


-(void)CallPostWithUrlForProfile:(NSString *)url andDataDictionary:(NSMutableDictionary*)dictionary andImages:(NSMutableArray *)Arr;
{
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];
    
    
    if (Arr.count)
    {
        NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
        NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
        NSString *photoID = [NSString stringWithFormat:@"%@",timeStampObj];
        
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // [body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"%@\"; filename=\"%@.png\"\r\n",@"profile_image",@"bhavesh"] dataUsingEncoding:NSUTF8StringEncoding]];
        
        
        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_image\"; filename=\"receipt.jpg\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        
        for (NSString *str in Arr) {
            NSData *data = [[NSData alloc]initWithBase64EncodedString:str options:NSDataBase64DecodingIgnoreUnknownCharacters];
            [body appendData:data];
            [body appendData:[@",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }
    
    for (id key in dictionary)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@",[dictionary valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:body];
    
    
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    
    [request setURL:[NSURL URLWithString:url]];
    
    
    
    
    
    NSURLConnection * editconn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    
    if (editconn)
    {
        receivedData=[[NSMutableData alloc]init];
    }
    
    [editconn start];
    
    
    
}

-(void)CallPostWithUrl:(NSString *)url andDataDictionary:(NSMutableDictionary*)dictionary andImage:(UIImage *)image andCarImage:(UIImage *)image2
{
    
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:300];
    [request setHTTPMethod:@"POST"];
    
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    
    NSMutableData *body = [NSMutableData data];
    
    
    if (image != nil)
    {
        NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
        NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
        NSString *photoID = [NSString stringWithFormat:@"%@",timeStampObj];
        
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // [body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"%@\"; filename=\"%@.png\"\r\n",@"profile_image",@"bhavesh"] dataUsingEncoding:NSUTF8StringEncoding]];
        
        
        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_image\"; filename=\"receipt.jpg\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[NSData dataWithData:UIImageJPEGRepresentation(image ,0.23)]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    if (image2 != nil) {
        NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
        NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
        NSString *photoID = [NSString stringWithFormat:@"%@",timeStampObj];
        
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        // [body appendData:[[NSString stringWithFormat:@"Content-Disposition: attachment; name=\"%@\"; filename=\"%@.png\"\r\n",@"profile_image",@"bhavesh"] dataUsingEncoding:NSUTF8StringEncoding]];
        
        
        [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"profile_image\"; filename=\"carReceipt.jpg\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        
        [body appendData:[NSData dataWithData:UIImageJPEGRepresentation(image2 ,0.23)]];
        [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        
    }
    
    NSLog(@"%@",image);
    
    for (id key in dictionary)
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@",[dictionary valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }
    
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    [request setHTTPBody:body];
    
    
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    
    [request setURL:[NSURL URLWithString:url]];
    
    
    
    
    
    NSURLConnection * editconn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    
    if (editconn)
    {
        receivedData=[[NSMutableData alloc]init];
    }
    
    [editconn start];
    
    
    
}


- (NSString *) urlEncode:(NSString *)str {
    NSString *string = str;
    return [string stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
}

-(NSString *)getString:(NSMutableDictionary *)Params{
    if (Params!=nil){
        NSString *postStr=@"";
        for(int i=0;i<[Params count];i++){
            NSString *key = [[[Params allKeys] objectAtIndex:i] description];
            postStr = [postStr stringByAppendingString:key];
            postStr = [postStr stringByAppendingString:@"="];
            postStr = [postStr stringByAppendingString:[Params objectForKey:key]];
            if(i < [Params count] - 1)
                postStr = [postStr stringByAppendingString:@"&"];
        }
        NSString *str=[NSString stringWithFormat:@"?%@",postStr];
        return [self urlEncode:str];
    }else{
        return @"";
    }
}

-(void)CallGetURL:(NSString *)url parameters:(NSMutableDictionary *)params{
    
    NSString *parStr =[self getString:params];
    NSString *requestUrl = [NSString stringWithFormat:@"%@%@", url,parStr];
    NSLog(@"URL dotsale:--->%@",requestUrl);
    
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestUrl]];
    [theRequest setHTTPMethod:@"GET"];
    NSURLConnection *theconnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    
    if (theconnection)
    {
        receivedData = [[NSMutableData alloc] init];
        receivedData = [NSMutableData data] ;
    }
    else
    {
    }
}


#pragma mark - connection delegates

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
    [receivedData setLength:0];
    
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    [receivedData appendData:data];
    
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERro BHavesh--> %@",error);
    [self.delegate onError];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
    NSError *error;
    NSMutableDictionary *responsedict=[NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&error];
    NSString *myString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    //  NSLog(@"ERro connectionDidFinishLoading--> %@",myString);
    //    if ([command isEqual:nil]) {
    //        command=@"";
    //    }
    
    [self.delegate onResultWithData:responsedict];
    
    
    
}


@end
