//
//  MapAnnotationAnchorPointView.swift
//  map_mapkit_ios
//
//  Created by 诺诺诺的言 on 2025/7/15.
//

import UIKit
import MapKit

class MapAnnotationAnchorPointView: MKAnnotationView {
    
    static let identifier: String = "MapAnnotationAnchorPointView"

    var marker: ATMapMarker? {
        didSet {
            updateView()
        }
    }

    var centerContentView: UIView?
    

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.purple
        self.marker = annotation as? ATMapMarker
//        self.isEnabled = marker?.markerType.supportSelected ?? false
//        self.isHidden = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 视图更新与重用
    
    override func prepareForReuse() {
        super.prepareForReuse()
        // 清除当前所有子视图
        for subview in subviews {
            subview.removeFromSuperview()
        }
    }
    
    private func updateView() {
        // 确保在更新视图前移除所有子视图
        for subview in subviews {
            subview.removeFromSuperview()
        }
        
        
        guard marker != nil else { return }
        
        let supContentView = UIView();
        supContentView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        supContentView.backgroundColor = UIColor.clear
        addSubview(supContentView)
        
        if (marker?.markerType.isTracePassing ?? false) {
            let whiteView = UIView(frame: CGRect(x: 0, y: 0, width: 18, height: 18))
            whiteView.center = supContentView.center;
            whiteView.backgroundColor = UIColor.white
            whiteView.layer.cornerRadius = 9
            whiteView.layer.masksToBounds = true
            supContentView.addSubview(whiteView)
            //supContentView.addSubview(whiteView)
            
            centerContentView = UIView();
            centerContentView?.frame = CGRect(x: 2, y: 2, width: 14, height: 14)
            centerContentView?.backgroundColor = UIColor(hex: "#15CBA1") //UIColor(hex: "#15CBA1")
            centerContentView?.layer.cornerRadius = 7
            centerContentView?.layer.masksToBounds = true // 裁剪超出圆角部分的内容
            whiteView.addSubview(centerContentView ?? UIView())
        } else {
            let errorImage = UIImageView(frame: supContentView.bounds)
            errorImage.contentMode = .scaleAspectFit
            errorImage.image = readImageContentFrom(imageName: "com.shishi.dingwei_location_error.png")
            //errorImage.translatesAutoresizingMaskIntoConstraints = false
            supContentView.addSubview(errorImage)
        }
        
        supContentView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            supContentView.centerXAnchor.constraint(equalTo: centerXAnchor),
            supContentView.centerYAnchor.constraint(equalTo: centerYAnchor),
            supContentView.widthAnchor.constraint(equalToConstant: 30),
            supContentView.heightAnchor.constraint(equalToConstant: 30)
        ])
    }
    
    
    private func readImageContentFrom(imageName : String) -> UIImage {
        if let image = UIImage(named: imageName) {
            return image
        }
        
        // 尝试从插件资源束加载
        let bundleURL = Bundle(for: MapAnnotationView.self).url(forResource: "map_mapkit_ios_resources", withExtension: "bundle")
        if let bundleURL = bundleURL, let resourceBundle = Bundle(url: bundleURL) {
            return UIImage(named: imageName, in: resourceBundle, compatibleWith: nil) ?? UIImage()
        }
        
        return  UIImage()
    }
    
    
    // MARK: - 更新视图
    
    override func layoutSubviews() {
        super.layoutSubviews()
        print("layoutSubviewssfsfsdf----");
        updateCenterOffset()
    }
    
    private func updateCenterOffset() {
        let markerSize = CGSize(width: 30, height: 30)
        // 没有文本时，仅需要将图片底部对准坐标点
        centerOffset = CGPoint(x: 0, y: -markerSize.height/2)
    }
    
}
