//
//  FirstAidTreatmentViewController.m
//  MedicalHelp
//
//  Created by mac on 7/12/14.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import "FirstAidTreatmentViewController.h"

@implementation FirstAidTreatmentViewController

@synthesize webViewPdf,dLink,progressStatus,indicatorStatus,lblLoadingStatus, receivedData, Currentconnection, expectedBytes,lblCancleStatus, fileName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self downloadWithNsurlconnection];
    
    [indicatorStatus startAnimating];
    
    progressStatus.progress = 0;
}

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

-(void)downloadWithNsurlconnection
{
    progressStatus.hidden = NO;
    
    NSURL *url = [NSURL URLWithString:dLink];
    
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    
    receivedData = [[NSMutableData alloc] initWithLength:0];
    
    Currentconnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
    if ([manager fileExistsAtPath:pdfPath] == YES) {
        
        [connection cancel];
        progressStatus.progress = 0;
        [indicatorStatus stopAnimating];
        
        [progressStatus setHidden:YES];
        [lblCancleStatus setHidden:YES];
        [lblLoadingStatus setHidden:YES];
        [indicatorStatus setHidden:YES];
        
        NSURL *url = [NSURL fileURLWithPath:pdfPath];
        
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        
        [webViewPdf loadRequest:req];
        
    }
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    progressStatus.hidden = NO;
    
    [receivedData setLength:0];
    
    expectedBytes = [response expectedContentLength];
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    
    [receivedData appendData:data];
    
    float progressive = (float)[receivedData length] / (float)expectedBytes;
    
    [progressStatus setProgress:progressive];
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
}

- (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    
    return nil;
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
    
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:fileName];
    
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
    [receivedData writeToFile:pdfPath atomically:YES];
    
    progressStatus.progress = 0;
    [indicatorStatus stopAnimating];
    
    [progressStatus setHidden:YES];
    [lblCancleStatus setHidden:YES];
    [lblLoadingStatus setHidden:YES];
    [indicatorStatus setHidden:YES];
    
    NSURL *url = [NSURL fileURLWithPath:pdfPath];
    
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    
    [webViewPdf loadRequest:req];
}
@end
