//
//  FirstAidTableViewController.m
//  MedicalHelp
//
//  Created by Tops on 7/16/14.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import "FirstAidTableViewController.h"

@implementation FirstAidTableViewController

@synthesize tblFAid,arraytblFAid,arrayUrl,arrayFileDetails;

- (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.
    
    RequestHandler *req = [[RequestHandler alloc]init];
    
    //Fire query and
    //Recieve Whole table in Array -->Dictonary
    arraytblFAid = [[NSMutableArray alloc]initWithArray:[req getList:@"SELECT * FROM tblfirstaidurl"]];
    
    //Setup array Each Array Resepresented One Column of Table
    
    arrayUrl = [[NSMutableArray alloc]init];
    
    //Convert Individual Array.Each Array Resepresented One Column of Table
    for (NSDictionary *dict in arraytblFAid) {
        [arrayUrl addObject:[[NSString alloc] initWithData:[Base64 decode:[dict objectForKey:@"FirstAidURL"]] encoding:NSUTF8StringEncoding]];
    }
    
    //Get File Details
    arrayFileDetails = [[NSMutableArray alloc]init];
    
    [self getFileDetails];
    
    //Add bar button item to top
    UIBarButtonItem *btnHome = [[UIBarButtonItem alloc]initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(goHome)];
    
    [self.navigationItem setRightBarButtonItem:btnHome];
    
    //and set title
    [self.navigationItem setTitle:@"First Aid Guide"];
    
    //Add Background
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wallpaper1.png"]];
    
    //Add Background in tableview
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wallpaper1.png"]];
    
    [tempImageView setFrame:self.tblFAid.frame];
    
    self.tblFAid.backgroundView = tempImageView;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
        NSLog(@"File detail is %@",arrayFileDetails);
}

-(void)goHome
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    UITableViewCell *cell = nil;
    
    cell = [[[NSBundle mainBundle]loadNibNamed:@"FirstAidTableCell" owner:self options:nil] objectAtIndex:0];
    
    UILabel *fileName = (UILabel *)[cell viewWithTag:101];
    UILabel *fileSize = (UILabel *)[cell viewWithTag:102];
    UILabel *fileUrl = (UILabel *)[cell viewWithTag:103];
    
    NSArray *arraySub = [arrayFileDetails objectAtIndex:indexPath.row];
    
    fileName.text = [arraySub objectAtIndex:0];
    fileSize.text = [self getSuffixInSize:[[arraySub objectAtIndex:1] floatValue]];
    fileUrl.text = [arraySub objectAtIndex:2];
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arrayFileDetails.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstAidTreatmentViewController *v1 =[[FirstAidTreatmentViewController alloc]init];
    
    v1.dLink = [[self.arrayFileDetails objectAtIndex:indexPath.row] objectAtIndex:2];
    v1.fileName = [[self.arrayFileDetails objectAtIndex:indexPath.row] objectAtIndex:0];
    
    [self.navigationController pushViewController:v1 animated:YES];
}

-(void)getFileDetails
{
    for (NSUInteger i = 0; i<arrayUrl.count; i++) {
        NSString *addressUrl = [arrayUrl objectAtIndex:i];
        
        NSURL *url = [NSURL URLWithString:addressUrl];
        
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
        
        NSURLConnection* currentConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    }
}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    
    NSString *filename = [[[[connection currentRequest] URL] path] lastPathComponent];
    
    NSString *url = [NSString stringWithFormat:@"%@",[[connection currentRequest] URL]];
    
    float fileSize = [response expectedContentLength];
    
    NSNumber *fileSizeN = [[NSNumber alloc]initWithFloat:fileSize];
    
    NSArray *arrayFileDeatils = [[NSArray alloc]initWithObjects:filename,fileSizeN,url, nil];
    
    [arrayFileDetails addObject:arrayFileDeatils];
    
    [tblFAid reloadData];
    
    [connection cancel];
    
}

-(NSString *)getSuffixInSize:(float)fileSize
{
    NSString *suffix;
    
    if (fileSize>=0 && fileSize<1024) {
        suffix = [NSString stringWithFormat:@"%.2f %@",fileSize,@"Bytes"];
    }
    else if (fileSize>=1024 && fileSize<1024*1024)
    {
        suffix = [NSString stringWithFormat:@"%.2f %@",fileSize/(1024),@"Kb"];
    }
    else if (fileSize>=1024*1024 && fileSize<1024*1024*1024)
    {
       suffix = [NSString stringWithFormat:@"%.2f %@",fileSize/(1024*1024),@"Mb"];
    }
    else if (fileSize>=1024*1024*1024 && fileSize<1024*1024*1024*1024)
    {
       suffix = [NSString stringWithFormat:@"%.2f %@",fileSize/(1024*1024*1024),@"Gb"];
    }
    
    return suffix;
}


@end
