//
//  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 sliderPrixMin;
@synthesize sliderPrixMax;
@synthesize prixMin;
@synthesize prixMax;



-(IBAction)sliderMinChanged:(id)sender {
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	UISlider *slider = (UISlider *)sender;
	int progressAsInt = (int)(slider.value);
	int progressAsIntMax = (int)(sliderPrixMax.value);
	if (progressAsInt >= progressAsIntMax - 99){
		[sliderPrixMax setValue: progressAsInt + 100];
		[self sliderMaxChanged:sliderPrixMax];
	}
	if (progressAsInt >= 4900) {
		[sliderPrixMin setValue:4900];
		prixMin.text = @"4900 $";
	}
	else
	{
		//ARRONDIR
		NSString *str = [NSString stringWithFormat:@"%.0f", (float)(progressAsInt/100)];
		int newInt = [str intValue] * 100;
		NSString *newText = [NSString stringWithFormat:@"%i $",newInt];
		prixMin.text = newText;
		appDelegate.montantMin = newText;
	}
}

-(IBAction)sliderMaxChanged:(id)sender {
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	UISlider *slider = (UISlider *)sender;
	int progressAsInt = (int)(slider.value);
	int progressAsIntMin = (int)(sliderPrixMin.value);
	if (progressAsInt <= progressAsIntMin + 100){
		[sliderPrixMin setValue:progressAsInt - 100];
		[self sliderMinChanged:sliderPrixMin];
	}
	if (progressAsInt < 600) {
		[sliderPrixMax setValue:600];
		prixMax.text = @"600 $";
	}
	else
	{
		//ARRONDIR
		NSString *str = [NSString stringWithFormat:@"%.0f", (float)(progressAsInt/100)];
		int newInt = [str intValue] * 100;
		NSString *newText = [NSString stringWithFormat:@"%i $",newInt];
		prixMax.text = newText;
		appDelegate.montantMax = newText;
	}
}


-(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;
	[sliderPrixMin setValue:0];
	prixMin.text = @"0 $";
	[sliderPrixMax setValue:600];
	prixMax.text = @"600 $";
	
	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", 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;
	[button release];
	*/
	[groupOne release];
    [super viewDidLoad];
}

-(void)viewDidDisappear:(BOOL)animated{
	[self.navigationController popToRootViewControllerAnimated:YES];
}


-(void)viewWillAppear:(BOOL)animated{
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSLog(@"MIN %@",appDelegate.montantMin);
	NSLog(@"MAX %@",appDelegate.montantMax);
	//LIRE LE MONTANT MINIMUM ET MAXIMUM DANS APP DELEGATE
	if (appDelegate.montantMin != nil){
		[sliderPrixMin setValue:([appDelegate.montantMin stringByReplacingOccurrencesOfString:@"$" withString:@""]).floatValue];
		prixMin.text = appDelegate.montantMin;
	}
	if (appDelegate.montantMax != nil){
		[sliderPrixMax setValue:([appDelegate.montantMax stringByReplacingOccurrencesOfString:@"$" withString:@""]).floatValue];
		prixMax.text = appDelegate.montantMax;
	}
	
	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);
}




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

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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 35;
}

-(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 {
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	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];
			NSLog(@"indexPath row %i",lastIndexPath.row);
			if (self.lastIndexPath != nil)
			{
				appDelegate.lastIndexPathFiltre = self.lastIndexPath;
				appDelegate.reload = 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;
	}
}

- (void)viewDidUnload {
    self.listGroupOne = nil;
	self.listGroupTwo = nil;
	self.lastIndexPath = nil;
	self.appliquerButton = nil;
	self.table = nil;
	self.sliderPrixMin = nil;
	self.sliderPrixMax = nil;
	self.prixMin = nil;
	self.prixMax = nil;
    [super viewDidUnload];
}
- (void)dealloc {
    [listGroupOne release];
	[listGroupTwo release];
    [lastIndexPath release];
	[appliquerButton release];
	[table release];
	[sliderPrixMin release];
	[sliderPrixMax release];
	[prixMin release];
	[prixMax release];
	
    [super dealloc];
}



@end
