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

#import "SearchMedicalDrugViewController.h"

@implementation SearchMedicalDrugViewController

@synthesize arrayTblDrug,arrayName,searchString,arrayResult,tblDrugList,searchBarDrug;

- (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
    arrayTblDrug = [[NSMutableArray alloc]initWithArray:[req getList:[NSString stringWithFormat:@"select * from tbldruginfo"]]];
    
    //
   // arrayName = [[NSMutableArray alloc]initWithObjects:@"adfg",@"bgdg",@"casd",@"ddesdf",@"edfsd",@"asdf", nil];
    
    arrayName = [[NSMutableArray alloc]init];
    
    //Convert Individual Array.Each Array Resepresented One Column of Table
    for (NSDictionary *dict in arrayTblDrug) {
        [arrayName addObject:[dict objectForKey:@"Name"]];
    }
    
    //Create Searchbar On Navigation Bar
    searchBarDrug = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
    
    searchBarDrug.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
    
    searchBarView.autoresizingMask = 0;
    
    searchBarDrug.delegate = self;
    
    [searchBarView addSubview:searchBarDrug];
    
    self.navigationItem.titleView = searchBarView;
    
    [self.searchBarDrug becomeFirstResponder];
    
    //UITapGestureRecognizer
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyboard)];
//    
//    [self.tblDrugList addGestureRecognizer:tap];
}

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *cellId = @"CellDefaultId002";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    
    cell.textLabel.text = [arrayResult objectAtIndex:indexPath.row];
    
    return cell;
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    searchString = [NSString stringWithFormat:@"*%@*",searchBar.text];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@",searchString]; 
    
    arrayResult = [NSMutableArray arrayWithArray:[arrayName filteredArrayUsingPredicate:predicate]];
    
    [tblDrugList reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //search string from array
    searchString = [NSString stringWithFormat:@"*%@*",searchText];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@",searchString];
    
    arrayResult = [NSMutableArray arrayWithArray:[arrayName filteredArrayUsingPredicate:predicate]];
    
    //then sort result array
//    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
//    
//    arrayResult=[NSMutableArray arrayWithArray:[arrayResult sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]];
    
    [tblDrugList reloadData];
}

-(void)hideKeyboard
{
    //Hide keyboard on single tap on mapView
    [self.searchBarDrug resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *drugName = [arrayResult objectAtIndex:indexPath.row];
    
    DrugInfoViewController *v1 = [[DrugInfoViewController alloc]init];
    
    v1.drugName = drugName;
    
    [self.searchBarDrug resignFirstResponder];
    
    [self.navigationController pushViewController:v1 animated:YES];
}

@end
