//
//  RCTBaiduMap.m
//  RCTBaiduMap
//
//  Created by lovebing on 4/17/2016.
//  Copyright © 2016 lovebing.org. All rights reserved.
//

#import "RCTBaiduMapView.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>

@implementation RCTBaiduMapView {
    BMKMapView* _mapView;
    BMKLocationService* _locService;
    BMKPointAnnotation* _annotation;
    BMKPointAnnotation* lockedScreenAnnotation;
    NSMutableArray* _annotations;
    NSDictionary* _option;
}

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView{
    
}

-(void)setZoom:(float)zoom {
    self.zoomLevel = zoom;
}

-(void)setCenterLatLng:(NSDictionary *)LatLngObj {
    double lat = [RCTConvert double:LatLngObj[@"lat"]];
    double lng = [RCTConvert double:LatLngObj[@"lng"]];
    CLLocationCoordinate2D point = CLLocationCoordinate2DMake(lat, lng);
    self.centerCoordinate = point;
}

-(void)setLockedScreenAnnotation:(BOOL *)hasLockedScreenAnnotation{
    if(hasLockedScreenAnnotation){
        BMKPointAnnotation* lockedScreenAnnotation;
        if (lockedScreenAnnotation == nil) {
            lockedScreenAnnotation = [[BMKPointAnnotation alloc]init];
            lockedScreenAnnotation.isLockedToScreen = YES;
            lockedScreenAnnotation.screenPointToLock = CGPointMake(100, 100);
            lockedScreenAnnotation.title = @"我是固定屏幕的标注";
        }
        [self addAnnotation:lockedScreenAnnotation];
    }
}

-(void)setMarker:(NSDictionary *)option {
    NSLog(@"setMarker");
    _option = option;
    if(option != nil) {
        if(_annotation == nil) {
            _annotation = [[BMKPointAnnotation alloc]init];
            [self addMarker:_annotation option:option];
        }
        else {
            [self updateMarker:_annotation option:option];
        }
    }
}

-(void)setMyLocationEnabled:(BOOL)myLocationEnabled{
    if(myLocationEnabled){
        _locService = [[BMKLocationService alloc]init];
        _locService.delegate = self;
        [_locService startUserLocationService];
        self.showsUserLocation = YES;//显示定位图层
        self.userTrackingMode = BMKUserTrackingModeNone;//设置定位的状态
    }
}

-(void)setMarkers:(NSArray *)markers {
    int markersCount = [markers count];
    
    [self removeAnnotations:_annotations];
    _annotations = [[NSMutableArray alloc] init];

//    if(_annotations == nil) {
//        _annotations = [[NSMutableArray alloc] init];
//    }

    if(markers != nil) {
        for (int i = 0; i < markersCount; i++)  {
            NSDictionary *option = [markers objectAtIndex:i];
            
            BMKPointAnnotation *annotation = nil;
            if(i < [_annotations count]) {
                annotation = [_annotations objectAtIndex:i];
            }
            if(annotation == nil) {
                annotation = [[BMKPointAnnotation alloc]init];
                [self addMarker:annotation option:option];
                [_annotations addObject:annotation];
            }
            else {
                [self updateMarker:annotation option:option];
            }
        }
        
        int _annotationsCount = [_annotations count];
        
//        NSString *smarkersCount = [NSString stringWithFormat:@"%d", markersCount];
//        NSString *sannotationsCount = [NSString stringWithFormat:@"%d", _annotationsCount];
//        NSLog(smarkersCount);
//        NSLog(sannotationsCount);
        
        if(markersCount < _annotationsCount) {
            int start = _annotationsCount - 1;
            for(int i = start; i >= markersCount; i--) {
                BMKPointAnnotation *annotation = [_annotations objectAtIndex:i];
                [self removeAnnotation:annotation];
                [_annotations removeObject:annotation];
            }
        }
    }
}

-(CLLocationCoordinate2D)getCoorFromMarkerOption:(NSDictionary *)option {
    double lat = [RCTConvert double:option[@"latitude"]];
    double lng = [RCTConvert double:option[@"longitude"]];
    CLLocationCoordinate2D coor;
    coor.latitude = lat;
    coor.longitude = lng;
    return coor;
}

-(void)addMarker:(BMKPointAnnotation *)annotation option:(NSDictionary *)option {
    [self updateMarker:annotation option:option];
    [self addAnnotation:annotation];
}

-(void)updateMarker:(BMKPointAnnotation *)annotation option:(NSDictionary *)option {
    CLLocationCoordinate2D coor = [self getCoorFromMarkerOption:option];
    NSString *title = [RCTConvert NSString:option[@"title"]];
    if(title.length == 0) {
        title = nil;
    }
    annotation.coordinate = coor;
    BOOL isResult =[title compare:annotation.title];
    if (isResult) {
        annotation.title = title;
    }
}

/**
 *在地图View将要启动定位时，会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
    NSLog(@"start locate");
}

/**
 *用户方向更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    [self updateLocationData:userLocation];
    NSLog(@"heading is %@",userLocation.heading);
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    [self updateLocationData:userLocation];
}

@end
