import Foundation
import UIKit
import CoreLocation
import BaiduMapAPI_Map
import ObjectiveC

extension MapViewImpl {
    @objc public func clearPolygons() {
        for (_, polygon) in self.polygonOverlays {
            self.mapView.remove(polygon)
        }
        self.polygonOverlays.removeAll()
    }

    @objc public func addPolygon(
        _ latitudes: [NSNumber],
        _ longitudes: [NSNumber],
        _ strokeColor: String,
        _ fillColor: String,
        _ strokeWidth: Double
    ) -> Int {
        let count = min(latitudes.count, longitudes.count)
        if count < 3 {
            return -1
        }
        NSLog("addPolygon count=%d", count)

        var coordinates: [CLLocationCoordinate2D] = []
        coordinates.reserveCapacity(count)

        for index in 0..<count {
            let latitude = latitudes[index].doubleValue
            let longitude = longitudes[index].doubleValue
            coordinates.append(CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
        }

        let polygon = BMKPolygon(coordinates: &coordinates, count: UInt(count))
        if let polygonValue = polygon {
            let overlayId = self.nextOverlayId
            self.nextOverlayId += 1

            objc_setAssociatedObject(polygonValue, UnsafeRawPointer(bitPattern: 2001)!, strokeColor, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            objc_setAssociatedObject(polygonValue, UnsafeRawPointer(bitPattern: 2002)!, fillColor, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            objc_setAssociatedObject(polygonValue, UnsafeRawPointer(bitPattern: 2003)!, NSNumber(value: strokeWidth), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)

            self.mapView.add(polygonValue)
            self.polygonOverlays[overlayId] = polygonValue
            return overlayId
        }
        return -1
    }

    @objc public func updatePolygonStyle(
        _ overlayId: Int,
        _ latitudes: [NSNumber],
        _ longitudes: [NSNumber],
        _ strokeColor: String,
        _ fillColor: String,
        _ strokeWidth: Double
    ) {
        guard let polygonValue = self.polygonOverlays[overlayId] else {
            return
        }

        objc_setAssociatedObject(polygonValue, UnsafeRawPointer(bitPattern: 2001)!, strokeColor, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(polygonValue, UnsafeRawPointer(bitPattern: 2002)!, fillColor, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        objc_setAssociatedObject(polygonValue, UnsafeRawPointer(bitPattern: 2003)!, NSNumber(value: strokeWidth), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        
        let count = min(latitudes.count, longitudes.count)
        NSLog("addPolygon count=%d", count)
        if count < 3 {
            return
        }
        
        var coordinates: [CLLocationCoordinate2D] = []
        coordinates.reserveCapacity(count)

        for index in 0..<count {
            let latitude = latitudes[index].doubleValue
            let longitude = longitudes[index].doubleValue
            coordinates.append(CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
        }
        polygonValue.setPolygonWithCoordinates(&coordinates, count: UInt(count))

        if let polygonView = self.mapView.view(for: polygonValue) as? BMKPolygonView {
            polygonView.strokeColor = self.colorFromString(strokeColor, UIColor.systemBlue)
            polygonView.fillColor = self.colorFromString(fillColor, UIColor.systemBlue.withAlphaComponent(0.2))
            polygonView.lineWidth = CGFloat(strokeWidth)
        }
    }

    @objc public func removePolygon(_ overlayId: Int) {
        guard let polygonValue = self.polygonOverlays[overlayId] else {
            return
        }
        self.mapView.remove(polygonValue)
        self.polygonOverlays.removeValue(forKey: overlayId)
    }
}
