import Foundation
import UIKit
import CoreLocation
import BaiduMapAPI_Map

extension MapViewImpl {
    @objc public func addLabel(
        _ latitude: Double,
        _ longitude: Double,
        _ text: String,
        _ fontSize: Int,
        _ fontColor: String,
        _ borderWidth: Int,
        _ borderColor: String,
        _ rotate: Float
    ) -> Int {
        let marker = BMKTextMarker()
        marker.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.text = text

        let style = BMKTextStyle()
        style.fontSize = Int32(fontSize)
        style.textColor = self.colorFromString(fontColor, UIColor.black)
        style.borderWidth = Int32(borderWidth)
        style.borderColor = self.colorFromString(borderColor, UIColor.white)
        marker.style = style
        marker.rotate = rotate

        let overlayId = self.nextOverlayId
        self.nextOverlayId += 1

        self.mapView.add(marker)
        self.labelOverlays[overlayId] = marker
        NSLog("addLabel overlayId=%d text=%@ lat=%f lng=%f", overlayId, text, latitude, longitude)
        return overlayId
    }

    @objc public func updateLabelStyle(
        _ overlayId: Int,
        _ latitude: Double,
        _ longitude: Double,
        _ text: String,
        _ fontSize: Int,
        _ fontColor: String,
        _ borderWidth: Int,
        _ borderColor: String,
        _ rotate: Float
    ) {
        guard let marker = self.labelOverlays[overlayId] else {
            return
        }
        marker.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        marker.text = text

        let style = BMKTextStyle()
        style.fontSize = Int32(fontSize)
        style.textColor = self.colorFromString(fontColor, UIColor.black)
        style.borderWidth = Int32(borderWidth)
        style.borderColor = self.colorFromString(borderColor, UIColor.white)
        marker.style = style
        marker.rotate = rotate
    }

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