import Foundation
import UIKit
import CoreLocation
import BaiduMapAPI_Map
import ObjectiveC

extension MapViewImpl {
    @objc public func addMultiPoint(_ itemsData: [[String: Any]], _ iconPath: String, _ width: Int, _ height: Int, _ anchorX: Double, _ anchorY: Double) -> Int {
        let overlayId = self.nextOverlayId
        self.nextOverlayId += 1

        var items: [BMKMultiPointItem] = []
        for itemDict in itemsData {
            let item = BMKMultiPointItem()
            if let lat = itemDict["lat"] as? Double, let lng = itemDict["lng"] as? Double {
                item.coordinate = CLLocationCoordinate2DMake(lat, lng)
            }
            if let title = itemDict["title"] as? String {
                item.title = title
            }
            items.append(item)
        }

        let multiPoint = BMKMultiPointOverlay(multiPointItems: items)
        
        // 使用 objc_setAssociatedObject 存储配置
        objc_setAssociatedObject(multiPoint, UnsafeRawPointer(bitPattern: 5001)!, iconPath, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(multiPoint, UnsafeRawPointer(bitPattern: 5002)!, NSNumber(value: width), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(multiPoint, UnsafeRawPointer(bitPattern: 5003)!, NSNumber(value: height), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(multiPoint, UnsafeRawPointer(bitPattern: 5004)!, NSNumber(value: anchorX), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(multiPoint, UnsafeRawPointer(bitPattern: 5005)!, NSNumber(value: anchorY), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

        self.mapView.add(multiPoint)
        self.multiPointOverlays[overlayId] = multiPoint
        self.overlayVisibilities[overlayId] = true

        NSLog("addMultiPoint id=%d items=%lu width=%d height=%d", overlayId, items.count, width, height)
        return overlayId
    }

    @objc public func updateMultiPointStyle(_ overlayId: Int, _ itemsData: [[String: Any]], _ iconPath: String, _ width: Int, _ height: Int, _ anchorX: Double, _ anchorY: Double) {
        guard let multiPoint = self.multiPointOverlays[overlayId] else { return }

        var items: [BMKMultiPointItem] = []
        for itemDict in itemsData {
            let item = BMKMultiPointItem()
            if let lat = itemDict["lat"] as? Double, let lng = itemDict["lng"] as? Double {
                item.coordinate = CLLocationCoordinate2DMake(lat, lng)
            }
            if let title = itemDict["title"] as? String {
                item.title = title
            }
            items.append(item)
        }

        // 重新构建覆盖物（points 是 readonly）
        self.mapView.remove(multiPoint)
        
        let newMultiPoint = BMKMultiPointOverlay(multiPointItems: items)
        
        objc_setAssociatedObject(newMultiPoint, UnsafeRawPointer(bitPattern: 5001)!, iconPath, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(newMultiPoint, UnsafeRawPointer(bitPattern: 5002)!, NSNumber(value: width), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(newMultiPoint, UnsafeRawPointer(bitPattern: 5003)!, NSNumber(value: height), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(newMultiPoint, UnsafeRawPointer(bitPattern: 5004)!, NSNumber(value: anchorX), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(newMultiPoint, UnsafeRawPointer(bitPattern: 5005)!, NSNumber(value: anchorY), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

        self.mapView.add(newMultiPoint)
        self.multiPointOverlays[overlayId] = newMultiPoint

        NSLog("updateMultiPointStyle id=%d items=%lu width=%d height=%d", overlayId, items.count, width, height)
    }

    @objc public func removeMultiPoint(_ overlayId: Int) {
        guard let multiPoint = self.multiPointOverlays[overlayId] else { return }
        self.mapView.remove(multiPoint)
        self.multiPointOverlays.removeValue(forKey: overlayId)
        self.overlayVisibilities.removeValue(forKey: overlayId)
        self.overlayZIndexes.removeValue(forKey: overlayId)
        NSLog("removeMultiPoint id=%d", overlayId)
    }
}

