/**
 * 坐标转换工具模块
 * 提供WGS84和GCJ-02坐标系之间的转换功能
 */
var CoordinateUtils = (function() {
    'use strict';
    
    var pi = 3.14159265358979324;
    var a = 6378245.0;
    var ee = 0.00669342162296594323;

    /**
     * 判断是否在国内，不在国内则不做偏移
     */
    function outOfChina(lon, lat) {
        if ((lon < 72.004 || lon > 137.8347) && (lat < 0.8293 || lat > 55.8271)) {
            return true;
        } else {
            return false;
        }
    }
    
    function transformLat(x, y) {
        var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    function transformLon(x, y) {
        var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }


    function transform(wgLat, wgLon) {
        var mars_point = {lon: 0, lat: 0};
        if (outOfChina(wgLon, wgLat)) {
            mars_point.lat = wgLat;
            mars_point.lon = wgLon;
            return mars_point;
        }
        var dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        var dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        var radLat = wgLat / 180.0 * pi;
        var magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        var sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        mars_point.lat = wgLat + dLat;
        mars_point.lon = wgLon + dLon;
        return mars_point;
    }


    function getMapCoordinates(lat, lon, mapType) {
        var coordinates;
        if (mapType === 'amap' || mapType === 'amap_satellite') {
            // 高德地图 WGS84 转换为 GCJ-02
            var gcjCoord = transform(lat, lon);
            coordinates = ol.proj.fromLonLat([gcjCoord.lon, gcjCoord.lat]);
        } else if (mapType.startsWith('google_')) {
            // Google地图使用WGS84坐标系
            coordinates = ol.proj.fromLonLat([lon, lat]);
        } else {
            // 天地图 CGCS2000，与WGS84实质一样
            coordinates = ol.proj.fromLonLat([lon, lat]);
        }
        return coordinates;
    }


    return {
        transform: transform,
        getMapCoordinates: getMapCoordinates,
        outOfChina: outOfChina
    };
})(); 