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

#import "DetailViewController.h"
#import "AgenceAppDelegate.h"
#import "AsyncImageView.h"


@implementation DetailViewController
@synthesize unImmeuble;
@synthesize styleLabel;
@synthesize adresseLabel;
@synthesize prixLabel;
@synthesize mainImageView;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

-(void)ajouterFavoris{
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSMutableArray *array = [[NSMutableArray alloc] initWithArray:appDelegate.listeFavoris];
	
	if ([array indexOfObject:unImmeuble] == NSNotFound) //SI L'IMMEUBLE N'EST PAS DANS LES FAVORIS
	{
		//AJOUT DANS FAVORIS
		[array addObject:unImmeuble];
		//self.navigationItem.rightBarButtonItem.title = @"Retirer";
		self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"favorite_remove.png"];
	}
	else 
	{
		//RETRAIT DE FAVORIS
		[array removeObjectIdenticalTo:unImmeuble];
		//self.navigationItem.rightBarButtonItem.title = @"Ajouter";
		self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"favorite_add.png"];
	}
	appDelegate.listeFavoris = array;
	[array release];
	
	//MISE A JOUR CLICKER DES FAVORIS
	appDelegate.tabBarItem.badgeValue = ([appDelegate.listeFavoris count] > 0) ? [NSString stringWithFormat:@"%i", [appDelegate.listeFavoris count]] : nil ;}


-(void)viewWillAppear:(BOOL)animated {
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSMutableArray *array = [[NSMutableArray alloc] initWithArray:appDelegate.listeFavoris];
	
	self.navigationItem.rightBarButtonItem.enabled = YES;
	
	if ([array indexOfObject:unImmeuble] == NSNotFound) //SI L'IMMEUBLE N'EST PAS DANS LES FAVORIS
	{
		//self.navigationItem.rightBarButtonItem.title = @"Ajouter";
		self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"favorite_add.png"];
	}
	else 
	{
		//self.navigationItem.rightBarButtonItem.title = @"Retirer";
		self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"favorite_remove.png"];
	}
	[array release];
	
	//INFOS GENERALES
	styleLabel.text = unImmeuble.style;
	adresseLabel.text = unImmeuble.adresse;
	prixLabel.text = [NSString stringWithFormat:@"%i $",unImmeuble.prix];
	
	//LOAD DE L'IMAGE DE FACON ASYNCHRONE
	CGRect frame;
	frame.size.width=280; frame.size.height=180;
	frame.origin.x=0; frame.origin.y=0;
	AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frame]autorelease];
	asyncImage.tag = 999;
	NSString *stringUrl = [[NSString alloc] initWithFormat:@"http://www.web-k.org/os/imgmaisons/%i.jpg",unImmeuble.immeubleID];
	NSURL *url = [NSURL URLWithString:stringUrl];
	[asyncImage loadImageFromURL:url];
	[self.mainImageView addSubview:asyncImage];
	[stringUrl release];
	
}



- (void)viewDidLoad {
	//CREATION DU BOUTON DE NAVIGATION DE DROITE (FAVORIS)
	UIBarButtonItem *button = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"favorite_add.png"] 
								style:UIBarButtonItemStyleBordered target:self action:@selector(ajouterFavoris)]autorelease];
	self.navigationItem.rightBarButtonItem = button;
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	NSMutableArray *array = [[NSMutableArray alloc] initWithArray:appDelegate.listeFavoris];
	
	if ([array indexOfObject:unImmeuble] == NSNotFound)
	{
		//self.navigationItem.rightBarButtonItem.title = @"Ajouter";
		self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"favorite_add.png"];
	}
	else 
	{
		//self.navigationItem.rightBarButtonItem.title = @"Retirer";
		self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"favorite_remove.png"];
	}
	//MISE A JOUR CLICKER DES FAVORIS
	appDelegate.tabBarItem.badgeValue = ([appDelegate.listeFavoris count] > 0) ? [NSString stringWithFormat:@"%i", [appDelegate.listeFavoris count]] : nil ;

	[array release];
	[super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    self.unImmeuble = nil;
	self.prixLabel = nil;
	self.styleLabel = nil;
	self.adresseLabel = nil;
	self.mainImageView = nil;
	[super viewDidUnload];
}


- (void)dealloc {
	[unImmeuble release];
	[prixLabel release];
	[styleLabel release];
	[adresseLabel release];
	[mainImageView release];
    [super dealloc];
}


@end
