//
//  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;



- (void)viewDidLoad {
	
	AgenceAppDelegate * appDelegate = (AgenceAppDelegate *)[[UIApplication sharedApplication] delegate];
		
	self.title = @"Carte";
	map.showsUserLocation = YES;
	map.mapType = MKMapTypeStandard;
	map.delegate = self;
	
	/*Region and Zoom*/
	MKCoordinateRegion region;
	MKCoordinateSpan span;
	span.latitudeDelta=4;
	span.longitudeDelta=4;
	
	CLLocationCoordinate2D location=map.userLocation.coordinate;
	
	//mettre latitude et longitude pour le simulateur..
	location.latitude=48.094796;
	location.longitude=-72.998413;
	
	//Si en temps reel dans iphone mettre les coordonnes actuelles
	//location = appDelegate.locationManager.location.coordinate;

	
	region.span=span;
	region.center=location;
	
	
	[map setRegion:region animated:TRUE];
	[map regionThatFits:region];
	
	//LOADER LES MARKERS
	for (int i=0; i < [appDelegate.list count]; i++) {
		//Loader l'immeuble
		Immeuble *unImmeuble = [[Immeuble alloc]init];
		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);
		[unImmeuble release];
	}
	
	
	
	//NSArray *array = [[NSArray alloc] initWithObjects:annotation,annotation2,annotation3,annotation4,annotation5,annotation6,annotation7];
	//[map addAnnotations:array];
    [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]init];
	detail.unImmeuble = (Immeuble *)view.annotation;
	[self.navigationController pushViewController:detail animated:YES];
}

- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
	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;
		pin.pinColor = MKPinAnnotationColorGreen;
		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
