//
//  CLLocationCoordinate2D+HandleTool.swift
//  map_amap_ios
//
//  Created by 诺诺诺的言 on 2025/7/22.
//

import Foundation
import CoreLocation

extension CLLocationCoordinate2D {
    /// 地球坐标 WGC-84 转为为火星坐标 GCJ-02（限中国境内）
    /// - CLLocationManager 获取的是地球坐标，但地图上显示的需要火星坐标
    /// - 通过 CLLocationManager 获取到中国境内地理坐标，若需要在地图上显示，则需要转换为火星坐标
    var wgc84ToGCJ02: CLLocationCoordinate2D {
        let earthRadius = 6378245.0
        let gcj_ee = 0.00669342162296594323

        var adjustedLatitude = transformedLatitude
        var adjustedLongitude = transformedLongitude

        let radLat = latitude / 180.0 * .pi
        let magic: Double = {
            let temp = sin(radLat)
            let result = 1 - gcj_ee * temp * temp
            return result
        }()
        let sqrtMagic = sqrt(magic)

        adjustedLatitude = (adjustedLatitude * 180.0) / ((earthRadius * (1.0 - gcj_ee)) / (magic * sqrtMagic) * .pi)
        adjustedLongitude = (adjustedLongitude * 180.0) / (earthRadius / sqrtMagic * cos(radLat) * .pi)

        let result = CLLocationCoordinate2D(
            latitude: latitude + adjustedLatitude,
            longitude: longitude + adjustedLongitude
        )

        return result
    }

    var gcj02ToWGS84: CLLocationCoordinate2D {
        let a = 6378245.0
        let ee = 0.00669342162296594323
        let gcjLat = latitude
        let gcjLon = longitude
        var dLat = transformLat(x: gcjLon - 105.0, y: gcjLat - 35.0)
        var dLon = transformLon(x: gcjLon - 105.0, y: gcjLat - 35.0)
        let radLat = gcjLat / 180.0 * Double.pi
        var magic = sin(radLat)
        magic = 1 - ee * magic * magic
        let sqrtMagic = sqrt(magic)
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Double.pi)
        dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * Double.pi)
        let mgLat = gcjLat + dLat
        let mgLon = gcjLon + dLon
        return CLLocationCoordinate2D(latitude: gcjLat * 2 - mgLat, longitude: gcjLon * 2 - mgLon)
    }
    
    private var transformedLatitude: Double {
        let x = longitude - 105.0
        let y = latitude - 35.0
        var result = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
        result += (20.0 * sin(6.0 * x * .pi) + 20.0 * sin(2.0 * x * .pi)) * 2.0 / 3.0
        result += (20.0 * sin(y * .pi) + 40.0 * sin(y / 3.0 * .pi)) * 2.0 / 3.0
        result += (160.0 * sin(y / 12.0 * .pi) + 320.0 * sin(y * .pi / 30.0)) * 2.0 / 3.0
        return result
    }

    private var transformedLongitude: Double {
        let x = longitude - 105.0
        let y = latitude - 35.0
        var result = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
        result += (20.0 * sin(6.0 * x * .pi) + 20.0 * sin(2.0 * x * .pi)) * 2.0 / 3.0
        result += (20.0 * sin(x * .pi) + 40.0 * sin(x / 3.0 * .pi)) * 2.0 / 3.0
        result += (150.0 * sin(x / 12.0 * .pi) + 300.0 * sin(x / 30.0 * .pi)) * 2.0 / 3.0
        return result
    }

    func transformLat(x: Double, y: Double) -> Double {
        var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(abs(x))
        ret += (20.0 * sin(6.0 * x * Double.pi) + 20.0 * sin(2.0 * x * Double.pi)) * 2.0 / 3.0
        ret += (20.0 * sin(y * Double.pi) + 40.0 * sin(y / 3.0 * Double.pi)) * 2.0 / 3.0
        ret += (160.0 * sin(y / 12.0 * Double.pi) + 320 * sin(y * Double.pi / 30.0)) * 2.0 / 3.0
        return ret
    }

    func transformLon(x: Double, y: Double) -> Double {
        var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(abs(x))
        ret += (20.0 * sin(6.0 * x * Double.pi) + 20.0 * sin(2.0 * x * Double.pi)) * 2.0 / 3.0
        ret += (20.0 * sin(x * Double.pi) + 40.0 * sin(x / 3.0 * Double.pi)) * 2.0 / 3.0
        ret += (150.0 * sin(x / 12.0 * Double.pi) + 300.0 * sin(x / 30.0 * Double.pi)) * 2.0 / 3.0
        return ret
    }
}
