import { showModelMessage } from "./tools.js";
import * as api from "./api.js";
import config from "./config.js";

const imgPath=config.imgPath;
export const sdkKey = 'BECBZ-EJIEQ-LUU5N-B5ISQ-3TLMZ-BXFLG';

export const getMap = (mapid, instance) => {
	return uni.createMapContext(mapid, {
		this: instance.proxy
	});
}
//获取当前定位
export const getLoalcation = (oMap, success, fail) => {
	uni.getLocation({
		type: 'gcj02', // 返回可以用于 wx.openLocation 的经纬度
		geocode: true,
		success(res) {
			const {
				latitude,
				longitude
			} = res;
			moveToLocation(oMap, longitude, latitude);
			// if (markers) { //定位
			// 	const arrData = markers.value || [];
			// 	let index = arrData.findIndex((item) => {
			// 		return item.id == 0;
			// 	});
			// 	index = index == -1 ? arrData.length : index;
			// 	arrData[index] = addMarker(0, longitude, latitude, "dw.png");
			// 	markers.value = arrData;
			// }
			if (success) success({
				latitude,
				longitude
			});
		},
		fail(res) {
			if (fail) fail(res);
		}
	});
}

//逆解析地址
export const reverseGeocoder = (qqmapsdk, longitude, latitude, success, fail) => {
	qqmapsdk.reverseGeocoder({
		location: {
			latitude: latitude,
			longitude: longitude
		},
		success: (res) => {
			if (success) success(res.result);
		},
		fail: (res) => {
			if (fail) success(fail);
		}
	})
}

//添加点
export const addMarker = (id, longitude, latitude, icon) => {
	return {
		id: id,
		latitude,
		longitude,
		width: 30,
		height: 30,
		zIndex: "100",
		iconPath: imgPath+"static/map/" + icon
	}
}

//添加线
export const addLine = (lx, arrPolygonsPoits) => {
	let scolor = "";
	switch (lx) {
		case "add":
			scolor = "#e50413";
			break;
		case "edit":
			scolor = "#e58b04";
			break;
		default:
			scolor = "#0473e5";
			break
	}
	return {
		points: arrPolygonsPoits,
		color: scolor,
		width: 3
	}
}

//添加面
export const addPolygon = (scolor, fcolor, arrPolygonsPoits) => {
	/* let scolor = "",
		fcolor = "";
	switch (lx) {
		case "add":
			scolor = "#e50413";
			fcolor = "#f79bac73";
			break;
		case "edit":
			scolor = "#e58b04";
			fcolor = "#e58b0440";
			break;
		default:
			scolor = "#0473e5";
			fcolor = "#0473e540";
			break
	} */
	return {
		points: arrPolygonsPoits,
		fillColor: fcolor,
		strokeWidth: 3,
		strokeColor: scolor,
		zIndex: 11
	}
}

//添加圆
export const addCirle = (scolor, fcolor, longitude, latitude, radius) => {
	/* let scolor = "",
		fcolor = "";
	switch (lx) {
		case "add":
			scolor = "#e50413";
			fcolor = "#f79bac73";
			break;
		case "edit":
			scolor = "#e58b04";
			fcolor = "#e58b0440";
			break;
		default:
			scolor = "#0473e5";
			fcolor = "#0473e540";
			break
	} */
	return {
		latitude,
		longitude,
		radius,
		color: scolor,
		fillColor: fcolor,
		strokeWidth: 3
	}
}

//绘制多边形
export const drawPolygons = (obj, e) => {
	/* const {
		detail: {
			latitude,
			longitude
		}
	} = e;
	let {
		markers,
		polylines,
		polygons,
		arrPolygonsPoits,
		datacicle,
		circles
	} = obj
	if (datacicle) { //移除圆
		obj.datacicle = null;
		obj.circles.splice(circles.length - 1, 1);
	}
	const len = arrPolygonsPoits.length;
	arrPolygonsPoits.push({
		latitude,
		longitude
	});
	switch (len) {
		case 0:
			const marker = addMarker("add", 10000, latitude, longitude);
			markers.push(marker);
			break;
		case 1:
			polylines.push(addLine("add", arrPolygonsPoits));
			break;
		case 2:
			obj.polylines = [];
			polygons.push(addPolygon("add", arrPolygonsPoits));
			break;
		default:
			polygons[polygons.length - 1] = addPolygon("add", arrPolygonsPoits);
			console.log("333333333333", JSON.stringify(arrPolygonsPoits));
			break;
	} */
}

//移至中心点
export const moveToLocation=(oMap, longitude, latitude)=>{
	const point = {
		latitude,
		longitude
	}
	oMap.moveToLocation(point);
}

//站点、停车区
export const getRegionData = (arrRegionID, callback) => {
	if (!arrRegionID || arrRegionID.length == 0) return;
	const arrMethods = [];
	arrRegionID.map(regionId => {
		const params = {
			regionId
		};
		arrMethods.push(api.callOperateApi("ebikeRegion/getRegion", params, "get"));
	});

	Promise.all(arrMethods).then(res => {
		const arrCircles = [];
		const arrPolygons = [];
		const arrData = [];
		res.map(item => {
			const {
				code,
				message,
				data:arrRegion
			} = item;
			if (code != 200) return;
			arrRegion.map(data=>{
				const {
					regionId,
					shapeType,
					radius,
					points
				} = data;
				arrData.push(data);
				const scolor = "#58d4c0";
				const fcolor = "#c1f5f1";
				if (shapeType == 1) {
					const {
						longitude: lng,
						latitude: lat
					} = points[0];
					arrCircles.push(addCirle(scolor, fcolor, lng, lat, radius));
				} else if (shapeType == 2) {
					const arrPoint = points.map(p => {
						return {
							longitude: p.longitude,
							latitude: p.latitude
						}
					})
					arrPolygons.push(addPolygon(scolor, fcolor, arrPoint));
				}
			})
		})
		if (callback) callback({
			arrData,
			arrCircles,
			arrPolygons
		});
	})
}

//获取车辆数据
export const getBikeData = (arrRegionID, callback) => {
	if (!arrRegionID || arrRegionID.length == 0) return;
	const arrMethods = [];
	arrRegionID.map(regionId => {
		const params = {
			regionId
		};
		arrMethods.push(api.callOperateApi("ebikeRegion/getOperationBike", params, "get"));
	});
	Promise.all(arrMethods).then(arrRes => {
		const arrPoints=[];
		const arrData=[];
		arrRes.map(res=>{
			const {code,message,data}=res;
			if(code!=200){
				showModelMessage(message);
			}
			else{
				data.map(item=>{
					const{bikeId,longitude,latitude,state}=item;
					const id=arrPoints.length;
					let icon="bike.png";
					
					arrPoints.push(addMarker(id,longitude,latitude,icon));
				})
			}
		});
		callback({arrData,arrPoints});
	})
}
