//
//  MapAmapMarkerNameContentView.swift
//  map_amap_ios
//
//  Created by 诺诺诺的言 on 2025/7/22.
//

import Foundation

class MapAnnotationMarkerTextView: UIView {
    // 常量定义
    private static let bubbleHeight: CGFloat = 24
    private static let triangleHeight: CGFloat = 6
    private static let minWidth: CGFloat = 60
    
    static var height: CGFloat {
        return bubbleHeight + triangleHeight
    }

    let text: String
    private var labelView: UILabel!
    private var backgroundView: UIView!
    private var triangleLayer: CAShapeLayer!

    init(text: String) {
        self.text = text
        super.init(frame: .zero)
        setupViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        createSubviews()
        setupConstraints()
    }
    
    private func createSubviews() {
        // 创建胶囊形状背景视图
        backgroundView = UIView()
        backgroundView.backgroundColor = .white
        backgroundView.layer.cornerRadius = 12  // 较大的圆角，实现胶囊形状
        backgroundView.layer.masksToBounds = false
        backgroundView.layer.shadowColor = UIColor.black.cgColor
        backgroundView.layer.shadowOpacity = 0.2
        backgroundView.layer.shadowOffset = CGSize(width: 0, height: 1)
        backgroundView.layer.shadowRadius = 2
        addSubview(backgroundView)
        
        // 创建标签视图（单行文本）
        labelView = UILabel()
        labelView.text = text
        labelView.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        labelView.textColor = .black
        labelView.backgroundColor = .clear
        labelView.textAlignment = .center
        labelView.lineBreakMode = .byTruncatingTail
        labelView.numberOfLines = 1  // 限制为单行
        backgroundView.addSubview(labelView)
        
        // 创建气泡三角形
        triangleLayer = CAShapeLayer()
        triangleLayer.fillColor = UIColor.white.cgColor
        
        let trianglePath = UIBezierPath()
        trianglePath.move(to: CGPoint(x: 0, y: 0))    // 左点
        trianglePath.addLine(to: CGPoint(x: 5, y: 6)) // 底点
        trianglePath.addLine(to: CGPoint(x: 10, y: 0)) // 右点
        trianglePath.close()
        
        triangleLayer.path = trianglePath.cgPath
        layer.addSublayer(triangleLayer)
    }
    
    private func setupConstraints() {
        // 启用AutoLayout
        translatesAutoresizingMaskIntoConstraints = false
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        labelView.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景视图约束
        NSLayoutConstraint.activate([
            backgroundView.topAnchor.constraint(equalTo: topAnchor),
            backgroundView.leadingAnchor.constraint(equalTo: leadingAnchor),
            backgroundView.trailingAnchor.constraint(equalTo: trailingAnchor),
            backgroundView.heightAnchor.constraint(equalToConstant: Self.bubbleHeight)
        ])
        
        // 标签约束
        NSLayoutConstraint.activate([
            labelView.topAnchor.constraint(equalTo: backgroundView.topAnchor),
            labelView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor, constant: 8),
            labelView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor, constant: -8),
            labelView.bottomAnchor.constraint(equalTo: backgroundView.bottomAnchor)
        ])
        
        // 设置整个视图的高度（包含三角形）和最小宽度
        NSLayoutConstraint.activate([
            heightAnchor.constraint(equalToConstant: Self.height),
            widthAnchor.constraint(greaterThanOrEqualToConstant: Self.minWidth)
        ])
        
        layoutIfNeeded()
        updateTrianglePosition()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updateTrianglePosition()
    }
    
    private func updateTrianglePosition() {
        // 更新三角形位置到底部中心
        let width = bounds.width
        triangleLayer.frame = CGRect(x: (width - 10) / 2, y: Self.bubbleHeight, width: 10, height: Self.triangleHeight)
    }
}
