#import "MapViewController.h"
#import "Lang.h"
#import "SDK.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import "DrawUtils.h"
#import "NavigationItemSheet.h"
#import "UIConstants.h"
#import "UIToolkit.h"
#import "SMMapView.h"
#import "SMModernCalloutView.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <BlocksKit/A2DynamicDelegate.h>
#import <BlocksKit/NSObject+A2DynamicDelegate.h>
#import <libextobjc/EXTScope.h>
#import <PureLayout/PureLayout.h>

@interface MapViewController () <MKMapViewDelegate, SMCalloutViewDelegate>
@property (nonatomic, strong) NSArray<MapViewAnnotation *> *shownAnnotations;
@property (nonatomic, strong) MapViewAnnotation *selectedAnnotation;
@end

@implementation MapViewController

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (instancetype)init {
	if ((self = [super init])) {
		self.edgesForExtendedLayout = UIRectEdgeNone;
		// Custom initialization
	}
	return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[super viewDidLoad];

	if (self.shownAnnotations) {
		[self toggleMapView];
	}
}

#pragma mark - Protected

- (void)didSelectAnnotation:(MapViewAnnotation *)annotation {
	[self selectAnnotation:annotation];
}

#pragma mark - Public

- (void)addAnnotations:(NSArray<MapViewAnnotation *> *)annotations {
	self.shownAnnotations  = annotations;
}

- (void)addAnnotation:(MapViewAnnotation *)annotation {
	self.shownAnnotations  = @[annotation];
}

- (void)selectAnnotation:(MapViewAnnotation *)annotation {
	self.selectedAnnotation = annotation;
}

#pragma mark - Property

#pragma mark - Action

- (void)toggleMapView {
	[self.mapView showAnnotations:self.shownAnnotations animated:NO];
	if ([self.shownAnnotations containsObject:self.selectedAnnotation]) {
		[self.mapView selectAnnotation:self.selectedAnnotation animated:NO];
	}
}

- (void)showMapWithAnnotation:(MapViewAnnotation *)marker {
	MKMapItem *currentLocationItem = [MKMapItem mapItemForCurrentLocation];
	MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:marker.coordinate addressDictionary:nil];
	MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
	mapItem.name = marker.label;
	[MKMapItem openMapsWithItems:@[currentLocationItem,mapItem] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDefault,MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}

#pragma mark - Private

#pragma mark - Map view delegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
	if (![annotation isKindOfClass:[MapViewAnnotation class]]) {
		return nil;
	}
	static NSString *PinIdentifier = @"Pin";
	MKPinAnnotationView *pin = [mapView dequeueReusableAnnotationViewWithIdentifier:PinIdentifier];
	if (pin == nil) {
		pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:PinIdentifier];
	}
	pin.canShowCallout = NO;
	MapViewAnnotation *marker = annotation;
	pin.pinTintColor = marker.pinTintColor;
	pin.animatesDrop = YES;
	pin.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
	return pin;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
	if ([NSStringFromClass(view.class) isEqualToString:@"MKModernUserLocationView"]) {
		return;
	}
	if (![view.annotation isKindOfClass:[MapViewAnnotation class]]) {
		return;
	}
	MapViewAnnotation *marker = view.annotation;
	if (![self.selectedAnnotation isEqual:marker]) {
		[self didSelectAnnotation:marker];
	}
	//
	SMModernCalloutView *calloutView = ((SMMapView *) mapView).calloutView;
	[CocoaUtils configureCalloutView:calloutView annotation:marker];
	@weakify(self)
	UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
	button.backgroundColor = [UIWindow appearance].tintColor;
	button.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
	[button setTitle:IMLocalizedString(@"Directions") forState:UIControlStateNormal];
	[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[button sizeToFit];
	[DrawUtils addRoundedBorderToRect:button cornerRadius:5];
	[button bk_addEventHandler:^(id sender) {
		@strongify(self)
		[self showMapWithAnnotation: marker];
	} forControlEvents:UIControlEventTouchUpInside];
	calloutView.rightAccessoryView = button;
	[calloutView presentCalloutFromRect:view.bounds inView:view constrainedToView:mapView animated:YES];
}

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
	if ([NSStringFromClass(view.class) isEqualToString:@"MKModernUserLocationView"]) {
		return;
	}
	if (![view.annotation isKindOfClass:[MapViewAnnotation class]]) {
		return;
	}
	SMModernCalloutView *calloutView = ((SMMapView *) mapView).calloutView;
	[calloutView dismissCalloutAnimated:YES];
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views {
	for (MKAnnotationView *view in views) {
		if ([NSStringFromClass(view.class) isEqualToString:@"MKModernUserLocationView"]) {
			view.canShowCallout = NO;
		}
	}
}

@end
