//
//  FiltreViewController.m
//  Agence
//
//  Created by Olivier Savard on 11-01-22.
//  Copyright 2011 OSInfo Informatique. All rights reserved.
//

#import "FiltreViewController.h"
#import "ListeViewController.h"
#import "AgenceAppDelegate.h"
#import "PickerViewController.h"


@implementation FiltreViewController
@synthesize listGroupOne;
@synthesize listGroupTwo;
@synthesize lastIndexPath;
@synthesize appliquerButton;
@synthesize table;
@synthesize masterView;
@synthesize scrollView;


-(IBAction)appliquer{
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	if (self.lastIndexPath != nil)
	{
		appDelegate.lastIndexPathFiltre = self.lastIndexPath;
		appDelegate.reload = YES;
		[self.navigationController popToRootViewControllerAnimated:YES];  //ViewWillAppear du root Controller sera appellé ce qui n'est pas le cas lorsque
	}
	
}

-(IBAction)reInitialiser{
	NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
	self.lastIndexPath = path;
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	appDelegate.montantMin = nil;
	appDelegate.montantMax = nil;
	NSArray *groupTwo = [[NSArray alloc] initWithObjects: @"Montant minimum:", @"Montant maximum:", nil];
	self.listGroupTwo = groupTwo;	
	[self.table reloadData];
}

- (void)viewDidLoad {
    NSArray *groupOne = [[NSArray alloc] initWithObjects:@"Tous les types de propriétés",
                      @"Chalet", @"Condo"  @"Maison", @"Bord de lac",
                      @"Sur pente de ski",nil];
	
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSString *titreMin = (appDelegate.montantMin != nil) ? [NSString stringWithFormat:@"Montant minimum: %@ $",appDelegate.montantMin] : @"Montant minimum:" ;
	NSString *titreMax = (appDelegate.montantMax != nil) ? [NSString stringWithFormat:@"Montant maximum: %@ $",appDelegate.montantMax] : @"Montant maximum:" ;																
	NSArray *groupTwo = [[NSArray alloc] initWithObjects: titreMin, titreMax, nil];
	
	
    self.listGroupOne = groupOne;
	self.listGroupTwo = groupTwo;
	
	//CREATION DU BOUTON DE NAVIGATION DE DROITE (APPLIQUER FILTRE)
	UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:@"Appliquer" style:UIBarButtonItemStylePlain target:self action:@selector(appliquer)];
	self.navigationItem.rightBarButtonItem = button;
	
	[scrollView addSubview:masterView];
	scrollView.contentSize = [masterView sizeThatFits:CGSizeZero];
	
	[button release];
	[groupOne release];
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated{
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
	if (appDelegate.lastIndexPathFiltre == nil){
		self.lastIndexPath = path;
	}
	else
		self.lastIndexPath = appDelegate.lastIndexPathFiltre ;
	
	NSString *titreMin = (appDelegate.montantMin != nil) ? [NSString stringWithFormat:@"Montant minimum: %@ $",appDelegate.montantMin] : @"Montant minimum:" ;
	NSString *titreMax = (appDelegate.montantMax != nil) ? [NSString stringWithFormat:@"Montant maximum: %@ $",appDelegate.montantMax] : @"Montant maximum:" ;																
	NSArray *groupTwo = [[NSArray alloc] initWithObjects: titreMin, titreMax, nil];
	self.listGroupTwo = groupTwo;
	[self.table reloadData];
	
	//DEBUG
	NSLog(@"Montant minimum dans appDelegate: %@",appDelegate.montantMin);
	NSLog(@"Montant maximum dans appDelegate: %@",appDelegate.montantMax);
}

- (void)viewDidUnload {
    self.listGroupOne = nil;
	self.listGroupTwo = nil;
	self.lastIndexPath = nil;
	self.appliquerButton = nil;
	self.table = nil;
	self.masterView = nil;
	self.scrollView = nil;
    [super viewDidUnload];
}
- (void)dealloc {
    [listGroupOne release];
	[listGroupTwo release];
    [lastIndexPath release];
	[appliquerButton release];
	[table release];
	[masterView release];
	[scrollView release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if (section == 0) 
		return @"Types de propriétés";
	else 
		return @"Prix";
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section == 0)
		return [listGroupOne count];
	else return [listGroupTwo count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
	static NSString *DetailCellIdentifier = @"DetailCellIdentifier";
    
    UITableViewCell *cellGroupOne = [tableView dequeueReusableCellWithIdentifier:
                             CheckMarkCellIdentifier];
	UITableViewCell *cellGroupTwo = [tableView dequeueReusableCellWithIdentifier:
									 DetailCellIdentifier];
	
    if (cellGroupOne == nil) {
        cellGroupOne = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CheckMarkCellIdentifier] autorelease];
    }
	if (cellGroupTwo == nil) {
        cellGroupTwo = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:DetailCellIdentifier] autorelease];
    }
	NSUInteger section = [indexPath section];
	switch (section) {
		case 0:
		{
			NSUInteger row = [indexPath row];
			NSUInteger oldRow = [lastIndexPath row];
			cellGroupOne.textLabel.text = [listGroupOne objectAtIndex:row];
			cellGroupOne.accessoryType = (row == oldRow && lastIndexPath != nil) ? 
			UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
			return cellGroupOne;
			break;
		}
		case 1:
		{
			NSUInteger row = [indexPath row];
			cellGroupTwo.textLabel.text = [listGroupTwo objectAtIndex:row];
			cellGroupTwo.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			return cellGroupTwo;
			break;
		}
		default:
			break;
	}
}

#pragma mark -
#pragma mark Table Delegate Methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	int section = indexPath.section;
	int newRow = indexPath.row;
	int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1 ;
	
	switch (section) {
		case 0:  //section du tableau type de propriété
		{
			if (newRow != oldRow) 
				{
				UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
				newCell.accessoryType = UITableViewCellAccessoryCheckmark;
				UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
				oldCell.accessoryType = UITableViewCellAccessoryNone;
				lastIndexPath = indexPath;
				}
			[tableView deselectRowAtIndexPath:indexPath animated:YES];
			break;
		}
		case 1:  //section du tableau montants minimums et maximums
		{
			PickerViewController *pickerViewController = [[PickerViewController alloc]initWithNibName:@"PickerViewController" bundle:nil];
			pickerViewController.title = (indexPath.row == 0) ? @"Montant minimum" : @"Montant maximum";
			[self.navigationController pushViewController:pickerViewController animated:YES];
			[pickerViewController release];
			[tableView deselectRowAtIndexPath:indexPath animated:YES];
			break;
		}
		default:
			break;
	}
}



@end
