//
//  SymCheckerBPViewController.m
//  MedicalHelp
//
//  Created by mac on 6/25/14.
//  Copyright (c) 2014 mac. All rights reserved.
//

#import "SymCheckerBPViewController.h"

@implementation SymCheckerBPViewController

@synthesize arraypkBPid, arrayBP, arrayTblBP, lblBPName, imgBPimage, indexBP ,indexBPImage;

- (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
    arrayTblBP = [[NSMutableArray alloc]initWithArray:[req getList:@"select * from tblbodypart"]];
    
    //Setup array Each Array Resepresented One Column of Table
    arrayBP = [[NSMutableArray alloc]init];
    arraypkBPid  = [[NSMutableArray alloc]init];
    
    //Convert Individual Array.Each Array Resepresented One Column of Table
    for (NSDictionary *dict in arrayTblBP) {
        [arrayBP addObject:[dict objectForKey:@"BodyPart"]];
        [arraypkBPid addObject:[dict objectForKey:@"pkBPid"]];
    }
    
    
    
    //add image to array
    indexBPImage = [[NSMutableArray alloc]initWithObjects:@"BPHead.jpg",@"BPNeck.jpg",@"BPChest.jpg",@"BPAbdomen.jpg",@"BPBack.jpg",@"BPPelvis.jpg",@"BPButtock.jpg",@"BPLegs.jpg",@"BPArm.jpg", nil];
    
    //
     imgBPimage.image = [UIImage imageNamed:[indexBPImage objectAtIndex:0]];
    
    //This is used for left and right button and swiping gestures.
    indexBP = 0;
    
    //add first Subbody part in to lable and image (In the format Index.SubBodyPart example: 1.nose ...)
    lblBPName.text = [NSString stringWithFormat:@"%d.%@",indexBP+1,[arrayBP objectAtIndex:indexBP]];

    
    //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:@"Synptom Checker"];
    
     //Add Background
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"wallpaper1.png"]];
    
}

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

- (IBAction)btnBackword:(id)sender {
    
    if (indexBP == 0) {
        indexBP = arrayBP.count - 1;
    }
    else
    {
        indexBP--;
    }
    imgBPimage.image = [UIImage imageNamed:[indexBPImage objectAtIndex:indexBP]];
    lblBPName.text = [NSString stringWithFormat:@"%@.%@",[arraypkBPid objectAtIndex:indexBP],[arrayBP objectAtIndex:indexBP]];
}

- (IBAction)btnForward:(id)sender {
    if (indexBP == arrayBP.count - 1) {
        indexBP = 0;
    }
    else
    {
        indexBP++;
    }
    
    imgBPimage.image = [UIImage imageNamed:[indexBPImage objectAtIndex:indexBP]];
    lblBPName.text = [NSString stringWithFormat:@"%@.%@",[arraypkBPid objectAtIndex:indexBP],[arrayBP objectAtIndex:indexBP]];
}

- (IBAction)btnBPNext:(id)sender {
    SymCheckerSBPViewController *subBodyPartViewController = [[SymCheckerSBPViewController alloc]init];
    
    subBodyPartViewController.indexfkBP = indexBP+1; //because index is 0 based and primary key is 1 based
    
    subBodyPartViewController.strBPName = lblBPName.text;
    
    [self.navigationController pushViewController:subBodyPartViewController animated:YES];
}

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

@end
