//
//  MapViewController.m
//  Agence
//
//  Created by Olivier Savard on 10-02-26.
//  Copyright 2010 OSInfo Informatique. All rights reserved.
//

#import "MapViewController.h"
#import "AgenceAppDelegate.h"
#import "Immeuble.h"
#import <MapKit/MapKit.h>
#import "DetailViewController.h"



@implementation MapViewController
@synthesize map;
@synthesize mapType;
@synthesize button;
@synthesize locate;




-(void)viewWillAppear:(BOOL)animated{
	NSLog(@"viewWillAppear map");
	
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	map.showsUserLocation = NO;
	map.showsUserLocation = YES;
	map.mapType = MKMapTypeStandard;
	map.delegate = self;
	
	/*Region and Zoom*/
	MKCoordinateRegion region;
	MKCoordinateSpan span;
	span.latitudeDelta=0.12;
	span.longitudeDelta=0.15;
	
	//attribuer le centre choisi
	CLLocationCoordinate2D location= appDelegate.locationCentre;
	
	region.span=span;
	region.center=location;
	
	[map setRegion:region animated:TRUE];
	[map regionThatFits:region];
	
	for (int i=0; i < [appDelegate.list count]; i++) {
		//Loader l'immeuble
		Immeuble *unImmeuble = [[[Immeuble alloc]init]autorelease];
		unImmeuble = [appDelegate.list objectAtIndex:i];
		locate.latitude = unImmeuble.latitude;
		locate.longitude = unImmeuble.longitude;
		unImmeuble.coordinate = locate;
		[map removeAnnotation:unImmeuble];
		NSLog(@"%i immeuble retirer de la carte..",i+1);
	}
	
	
	//LOADER LES MARKERS
	for (int i=0; i < [appDelegate.list count]; i++) {
		//Loader l'immeuble
		Immeuble *unImmeuble = [[[Immeuble alloc]init]autorelease];
		unImmeuble = [appDelegate.list objectAtIndex:i];
		locate.latitude = unImmeuble.latitude;
		locate.longitude = unImmeuble.longitude;
		unImmeuble.coordinate = locate;
		[map addAnnotation:unImmeuble];
		NSLog(@"%i immeuble ajouté a la carte..",i+1);
	}
	[map setCenterCoordinate:map.region.center animated:NO];
	[super viewWillAppear:YES];
}

- (void)viewDidLoad {
	NSLog(@"viewDidLoad map");
	[super viewDidLoad];
}


/*
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
	DetailViewController *detail = [[DetailViewController alloc]init];
	detail.unImmeuble = (Immeuble *)view.annotation;
	[self.navigationController pushViewController:detail animated:YES];
}
 */

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
	DetailViewController *detail = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
	detail.unImmeuble = (Immeuble *)view.annotation;
	[self.navigationController pushViewController:detail animated:YES];	 
}

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
	static NSString *placemarkIdentifier = @"Map Location Identifier";
	if ([annotation isKindOfClass:[Immeuble class]]) {
		MKPinAnnotationView *pin = (MKPinAnnotationView *) [theMapView dequeueReusableAnnotationViewWithIdentifier:placemarkIdentifier];
		if (pin == nil) {
			pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:placemarkIdentifier];
			
		}
		else {
			pin.annotation = annotation;
		}
		
		pin.enabled = YES;
		pin.animatesDrop = YES;
		NSMutableArray *array = [[NSMutableArray alloc] initWithArray:appDelegate.listeFavoris];
		if ([array indexOfObject:(Immeuble *)annotation] == NSNotFound )
		{
			NSLog(@"Pin immeuble pas dans favoris");
			pin.pinColor = MKPinAnnotationColorGreen;
		}
		else 
		{
			NSLog(@"Pin immeuble dans favoris");
			pin.pinColor = MKPinAnnotationColorRed;
		}
		[array release];
		
		pin.canShowCallout = YES;
		
		UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
		pin.rightCalloutAccessoryView = rightButton;
		
		
		//[self performSelector:@selector(openCallout:) withObject:annotation afterDelay:0.5];
		NSLog(@"Dans mapView...");
		return pin;
	}
	return nil;
}

/*
// 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 {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.map = nil;
}


- (void)dealloc {
	[map release];
    [super dealloc];
}

- (IBAction)changeType:(id)sender{
	if(mapType.selectedSegmentIndex==0){
		map.mapType=MKMapTypeStandard;
	}
	else if (mapType.selectedSegmentIndex==1){
		map.mapType=MKMapTypeSatellite;
	}
	else if (mapType.selectedSegmentIndex==2){
		map.mapType=MKMapTypeHybrid;
	}
}

-(IBAction)center{
	MKCoordinateRegion region;
	MKCoordinateSpan span;
	span.latitudeDelta=1;
	span.longitudeDelta=1;
	
	CLLocationCoordinate2D location=map.userLocation.coordinate;
	
	
	location.latitude=45.794796;
	location.longitude=-73.998413;
	region.span=span;
	region.center=location;
	
	[map setRegion:region animated:TRUE];
	[map regionThatFits:region];
}


@end
