import type { LngLat } from 'vue-tianditu2';
import { useCool } from '/@/cool';

export const MAP_TK = '51915011aa0997aa2ddd7e38504fa8bc';
export const MAP_LOAD = { v: '4.0', tk: MAP_TK };
export const DEFAULT_CENTER: LngLat = [121.280637, 32.125178];

export function pinIcon(color: string) {
	const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="28" height="40" viewBox="0 0 28 40"><path fill="${color}" stroke="#fff" stroke-width="1.5" d="M14 1C7.4 1 2 6.4 2 13c0 8.8 12 25 12 25s12-16.2 12-25C26 6.4 20.6 1 14 1z"/><circle cx="14" cy="13" r="5" fill="#fff"/></svg>`;
	return {
		iconUrl: 'data:image/svg+xml;charset=UTF-8,' + encodeURIComponent(svg),
		iconSize: [28, 40] as [number, number],
		iconAnchor: [14, 40] as [number, number]
	};
}

export const SITE_ICONS = {
	收集点: pinIcon('#16a34a'),
	压缩站: pinIcon('#f59e0b'),
	default: pinIcon('#3b82f6')
};

export function siteIcon(type: string) {
	if (type === '压缩站') return SITE_ICONS.压缩站;
	if (type === '收集点') return SITE_ICONS.收集点;
	return SITE_ICONS.default;
}

export const SORTER_ICON = pinIcon('#2563eb');
export const VEHICLE_ICON = pinIcon('#7c3aed');
export const PLAY_ICON = pinIcon('#ef4444');

export function useMapsApi() {
	const { service } = useCool();

	function call(name: string, data?: Record<string, any>) {
		const api = (service as any).maps?.live?.[name];
		if (typeof api === 'function') {
			return api.call((service as any).maps.live, data);
		}
		return service.request({
			url: `admin/maps/live/${name}`,
			method: 'POST',
			data
		});
	}

	return {
		sites: () => call('sites'),
		siteWeight: (data: { businessId: string; startTime: string; endTime: string }) => call('siteWeight', data),
		sorterOnline: () => call('sorterOnline'),
		sorterTrack: (data: { idCard: string; startTime: string; endTime: string }) => call('sorterTrack', data),
		sorterHealth: (idCard: string) => call('sorterHealth', { idCard }),
		vehicleOnline: () => call('vehicleOnline'),
		vehicleTrack: (data: { vehicleNo: string; startTime: string; endTime: string }) => call('vehicleTrack', data),
		vehicleEvents: (vehicleNo: string) => call('vehicleEvents', { vehicleNo })
	};
}

export async function fetchYsToken(service: any) {
	const res = await service.base.sys.param.stringbykey({ key: 'ys7.accessToken' });
	return (res as string) || '';
}

export function formatTime(v: any) {
	if (!v) return '-';
	const d = new Date(v);
	if (Number.isNaN(d.getTime())) return String(v);
	const p = (n: number) => String(n).padStart(2, '0');
	return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
}

export function dayRange(offsetEnd = 0, offsetStart = 0) {
	const end = new Date();
	end.setHours(23, 59, 59, 999);
	if (offsetEnd) end.setTime(end.getTime() + offsetEnd);
	const start = new Date();
	start.setHours(0, 0, 0, 0);
	if (offsetStart) start.setTime(start.getTime() + offsetStart);
	return { start, end };
}

export function lastHours(hours = 1) {
	const end = new Date();
	const start = new Date(end.getTime() - hours * 3600 * 1000);
	return { start, end };
}

export function toStamp(v: any) {
	const t = new Date(v).getTime();
	return Number.isFinite(t) ? t : 0;
}
