//
//  BMKShowMapPage.m
//  BMKObjectiveCDemo
//
//  Created by Baidu RD on 2018/3/5.
//  Copyright © 2018年 Baidu. All rights reserved.
//

#import "BMKShowMapPage.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#define kHeight_SegMentBackgroud  60
#define kHeight_BottomControlView  60


#define widthScale ([UIScreen mainScreen].bounds.size.width/375.0f)
#define heightScale ([UIScreen mainScreen].bounds.size.height/667.0f)
#define BMKMapVersion [NSString stringWithFormat:@"百度地图iOS SDK %@", BMKGetMapApiVersion()]
//屏幕宽度
#define KScreenWidth  ([UIScreen mainScreen].bounds.size.width)
//屏幕高度
#define KScreenHeight ([UIScreen mainScreen].bounds.size.height)
//状态栏高度
#define KStatuesBarHeight  ([UIApplication sharedApplication].statusBarFrame.size.height)
//导航栏高度
#define KNavigationBarHeight 44.0
//导航栏高度+状态栏高度
#define kViewTopHeight (KStatuesBarHeight + KNavigationBarHeight)
//iphoneX适配差值
#define KiPhoneXSafeAreaDValue ([[UIApplication sharedApplication] statusBarFrame].size.height>20?34:0)

#define COLOR(rgbValue)     [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

#define kHeight_SegMentBackgroud  60


//开发者通过此delegate获取mapView的回调方法
@interface BMKShowMapPage ()<BMKMapViewDelegate>
@property (nonatomic, strong) BMKMapView *mapView; //当前界面的mapView
@property (nonatomic, strong) UISegmentedControl *mapSegmentControl;
@property (nonatomic, strong) UIView *bottomControlView;
@property (nonatomic, strong) UISwitch *customizationMapSwitch;
@property (nonatomic, strong) UILabel *customizationMapLabel;
@end

@implementation BMKShowMapPage

#pragma mark - View life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self createMapView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:NO];
    //当mapView即将被显示的时候调用，恢复之前存储的mapView状态
    [_mapView viewWillAppear];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:NO];
    //当mapView即将被隐藏的时候调用，存储当前mapView的状态
    [_mapView viewWillDisappear];
}

#pragma mark - Config UI
- (void)configUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"显示地图";
    [self.view addSubview:self.mapSegmentControl];
    [self.view addSubview:self.bottomControlView];
    [self.bottomControlView addSubview:self.customizationMapSwitch];
    [self.bottomControlView addSubview:self.customizationMapLabel];
}

- (void)createMapView {
    //将mapView添加到当前视图中
    [self.view addSubview:self.mapView];
    //设置mapView的代理
    _mapView.delegate = self;
}

#pragma mark - Responding events
- (void)segmentControlDidChangeValue:(id)sender {
    NSUInteger selectedIndex = [_mapSegmentControl selectedSegmentIndex];
    switch (selectedIndex) {
        case 1:
            //设置当前地图类型为卫星地图
            [_mapView setMapType:BMKMapTypeSatellite];
            break;
        case 2:
            //设置当前地图类型为空白地图
            [_mapView setMapType:BMKMapTypeNone];
            break;
        default:
            //设置当前地图类型为标准地图
            [_mapView setMapType:BMKMapTypeStandard];
            break;
    }
}

- (void)switchDidChangeValue:(UISwitch *)sender {
    if (sender.on == YES) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"c67be9c4904e2e2b423215733163a601" ofType:@"sty"];
           //设置个性化地图样式
        //[_mapView setCustomMapStylePath:path];
        //开启个性化地图样式
        //[_mapView setCustomMapStyleEnable:YES];
    } else {
        //关闭个性化地图样式
        //[_mapView setCustomMapStyleEnable:NO];
    }
}

#pragma mark - Lazy loading
- (BMKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, kHeight_SegMentBackgroud, KScreenWidth, KScreenHeight - kViewTopHeight - kHeight_SegMentBackgroud - kHeight_BottomControlView - KiPhoneXSafeAreaDValue)];
    }
    return _mapView;
}

- (UISegmentedControl *)mapSegmentControl {
    if (!_mapSegmentControl) {
        NSArray *array = [[NSArray alloc] initWithObjects:@"标准地图",@"卫星地图",@"空白地图",nil];
        _mapSegmentControl = [[UISegmentedControl alloc] initWithItems:array];
       
        [_mapSegmentControl setTitle:@"标准地图" forSegmentAtIndex:0];
        [_mapSegmentControl setTitle:@"卫星地图" forSegmentAtIndex:1];
        [_mapSegmentControl setTitle:@"空白地图" forSegmentAtIndex:2];
        _mapSegmentControl.selectedSegmentIndex = 0;
        [_mapSegmentControl addTarget:self action:@selector(segmentControlDidChangeValue:) forControlEvents:UIControlEventValueChanged];
    }
    return _mapSegmentControl;
}





@end
