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

#import "DrugInfoViewController.h"

@implementation DrugInfoViewController

@synthesize drugName;

@synthesize arrayResult,arrayDrugInteractions,arrayDrugName,arrayDrugOverDose,arrayDrugPrecautions,arrayDrugUses,arrayDrugWarnings,arrayTblDrugInfo,arrayDrugSideEffects,wwDrugInfo;

- (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
    arrayTblDrugInfo = [[NSMutableArray alloc]initWithArray:[req getList:[NSString stringWithFormat:@"select * from tbldruginfo where Name = '%@'",self.drugName]]];
    
    arrayDrugInteractions = [[NSMutableArray alloc]init];
    arrayDrugName = [[NSMutableArray alloc]init];
    arrayDrugOverDose = [[NSMutableArray alloc]init];
    arrayDrugPrecautions = [[NSMutableArray alloc]init];
    arrayDrugSideEffects = [[NSMutableArray alloc]init];
    arrayDrugUses = [[NSMutableArray alloc]init];
    arrayDrugWarnings = [[NSMutableArray alloc]init];
    
    NSString *DrugInteractions;
    NSString *DrugOverDose ;
    NSString *DrugPrecautions;
    NSString *DrugSideEffects;
    NSString *DrugUses ;
    NSString *DrugWarnings;
    
    //Convert Individual Array.Each String Resepresented First Column of Table
    for (NSDictionary *dict in arrayTblDrugInfo) {
        DrugInteractions = [[NSString alloc]initWithData:[Base64 decode:[dict objectForKey:@"Interactions"]] encoding:NSUTF8StringEncoding];
        
        DrugOverDose =[[NSString alloc]initWithData:[Base64 decode:[dict objectForKey:@"OverDose"]] encoding:NSUTF8StringEncoding];
        
        DrugPrecautions = [[NSString alloc]initWithData:[Base64 decode:[dict objectForKey:@"Precautions"]] encoding:NSUTF8StringEncoding];
        
        DrugSideEffects  = [[NSString alloc]initWithData:[Base64 decode:[dict objectForKey:@"SideEffects"]] encoding:NSUTF8StringEncoding];
        
        DrugUses = [[NSString alloc]initWithData:[Base64 decode:[dict objectForKey:@"Uses"]] encoding:NSUTF8StringEncoding];
        
        DrugWarnings = [[NSString alloc]initWithData:[Base64 decode:[dict objectForKey:@"Warnings"]] encoding:NSUTF8StringEncoding];
    }
    
    ////Generate Dynamic WebPage
    NSString *htmlFilePath = [[NSBundle mainBundle]pathForResource:@"htmlFileDrug" ofType:@"html"];
    
    NSMutableString* htmlString = [NSMutableString stringWithString:[[NSString alloc]initWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:nil]];
    
    htmlString =[NSMutableString stringWithString:[htmlString stringByReplacingOccurrencesOfString:@"uses_insertData" withString:DrugUses]];
    
    htmlString =[NSMutableString stringWithString:[htmlString stringByReplacingOccurrencesOfString:@"warnings_insertData" withString:DrugWarnings]];
    
    htmlString =[NSMutableString stringWithString:[htmlString stringByReplacingOccurrencesOfString:@"interactions_insertData" withString:DrugInteractions]];
    
    htmlString =[NSMutableString stringWithString:[htmlString stringByReplacingOccurrencesOfString:@"overdose_insertData" withString:DrugOverDose]];
    
    htmlString =[NSMutableString stringWithString:[htmlString stringByReplacingOccurrencesOfString:@"precautions_insertData" withString:DrugPrecautions]];

    htmlString =[NSMutableString stringWithString:[htmlString stringByReplacingOccurrencesOfString:@"SideEffects_insertData" withString:DrugSideEffects]];
    
    [wwDrugInfo loadHTMLString:htmlString baseURL:nil];
    
    //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:drugName];
    
    //Add Background
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wallpaper1.png"]];
}

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

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

@end
