import Foundation
import UIKit
import CoreLocation
import BaiduMapAPI_Map

extension MapViewImpl {
    @objc public func clearMarkers() {
        for (_, marker) in self.markerOverlays {
            self.mapView.remove(marker)
        }
        self.markerOverlays.removeAll()
    }

    @objc public func addMarker(
        _ latitude: Double,
        _ longitude: Double,
        _ iconPath: String,
    ) -> Int {
        let marker = BMKIconMarker()
        marker.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.icon = UIImage(imageLiteralResourceName: iconPath)

        let overlayId = self.nextOverlayId
        self.nextOverlayId += 1
        NSLog("addMarker overlayId=%d lat=%f lng=%f", overlayId, latitude, longitude)

        self.mapView.add(marker)
        self.markerOverlays[overlayId] = marker
        return overlayId
    }
    
    @objc public func updateMarkerStyle(
        _ overlayId: Int,
        _ lat: Double,
        _ lng: Double,
        _ iconPath: String
    ) {
        guard let markerValue = self.markerOverlays[overlayId] else {
            return
        }
        
        markerValue.coordinate = CLLocationCoordinate2DMake(lat, lng)
        markerValue.icon = UIImage(imageLiteralResourceName: iconPath)
    }

    @objc public func setMarkerScale(_ overlayId: Int, _ scaleX: Double, _ scaleY: Double) {
        guard let marker = self.markerOverlays[overlayId] else {
            return
        }
        marker.scaleX = Float(scaleX)
        marker.scaleY = Float(scaleY)
    }

    @objc public func setMarkerAnchor(_ overlayId: Int, _ anchorX: Double, _ anchorY: Double) {
        guard let marker = self.markerOverlays[overlayId] else {
            return
        }
        marker.anchorX = Float(anchorX)
        marker.anchorY = Float(anchorY)
    }

    @objc public func setMarkerRotate(_ overlayId: Int, _ rotate: Double) {
        guard let marker = self.markerOverlays[overlayId] else {
            return
        }
        marker.rotateFeature = UInt(BMKRotateItem.rawValue)
        marker.rotate = Float(rotate)
    }

    @objc public func setMarkerCollision(_ overlayId: Int, _ joinCollision: Bool, _ priority: Int) {
        guard let marker = self.markerOverlays[overlayId] else {
            return
        }
        if joinCollision {
            marker.collisionBehavior = BMKCollisionWithAllLayersByPriority
        } else {
            marker.collisionBehavior = BMKCollisionNotCollide
        }
        marker.collisionPriority = Int32(priority)
    }

    @objc public func applyMarkerStyle(_ marker: BMKIconMarker, _ markerView: BMKIconMarkerView) {
        // BMKIconMarker 的 scale/anchor/rotate 直接设在 marker 模型上，
        // SDK 会在生成 view 时自动应用，无需在 view 的 transform 上额外处理。
    }

    @objc public func removeMarker(_ overlayId: Int) {
        self.removeRichViewFromMarker(overlayId)
        guard let marker = self.markerOverlays[overlayId] else {
            return
        }
        self.mapView.remove(marker)
        self.markerOverlays.removeValue(forKey: overlayId)
    }
}
