//
//  OnlineVehiclesEntryView.swift
//  OrderScheduling
//
//  Created by 中道 on 2025/10/13.
//

import UIKit
import SnapKit

/// 弹窗：实时车辆在线情况
final class OnlineVehiclesEntryView: UIView {

    // MARK: - Public
    /// 点击“已知晓”回调
    var onConfirm: (() -> Void)?

    // MARK: - UI
    private let pinImageView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "online_entry_pin"))
        iv.contentMode = .scaleAspectFit
        return iv
    }()

    private let backgroundImageView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "online_entry_background"))
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    private let cardView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 16
        v.layer.shadowColor = UIColor.black.cgColor
        v.layer.shadowOpacity = 0.12
        v.layer.shadowRadius = 10
        v.layer.shadowOffset = CGSize(width: 0, height: 6)
        return v
    }()

    private let titleLabel: UILabel = {
        let l = UILabel()
        l.text = "实时车辆在线情况"
        l.textAlignment = .center
        l.font = .boldSystemFont(ofSize: 16)
        l.textColor = UIColor(hex: "#040404")
        return l
    }()

    /// 富文本正文
    private let contentLabel: UILabel = {
        let l = UILabel()
        l.numberOfLines = 0
        l.textAlignment = .left
        l.textColor = UIColor(hex: "#4C5361")
        l.font = .dd_systemFont(ofSize: 14, weight: .medium)
        return l
    }()

    private let confirmButton: UIButton = {
        let b = UIButton(type: .system)
        b.setTitle("已知晓", for: .normal)
        b.setTitleColor(.white, for: .normal)
        b.titleLabel?.font = .boldSystemFont(ofSize: 16)
        b.backgroundColor = UIColor(hex: "#3174CE")
        b.layer.cornerRadius = 24
        b.layer.masksToBounds = true
        return b
    }()

    // MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        buildUI()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        buildUI()
    }
    
    convenience init(content: String?) {
        self.init(frame: .zero)
        let attribute = NSMutableAttributedString(string: content ?? "",attributes: [.foregroundColor: UIColor(hex: "#4C5361"),.font : UIFont.boldSystemFont(ofSize: 16)])
        attribute.append(NSMutableAttributedString(string: "\n\n为获取更多案件",attributes: [.foregroundColor: UIColor(hex: "#CE5F31"),.font : UIFont.boldSystemFont(ofSize: 21)]))
        attribute.append(NSMutableAttributedString(string: "\n请提醒在班的师傅登录APP并合理保持在线。",attributes: [.foregroundColor: UIColor(hex: "#4C5361"),.font : UIFont.boldSystemFont(ofSize: 16)]))
        contentLabel.attributedText = attribute
        confirmButton.addTarget(self, action: #selector(confirmClick), for: .touchUpInside)
    }
    
    @objc func confirmClick() {
        onConfirm?()
    }
    
    func buildUI() {
        backgroundColor = .clear

        addSubview(cardView)
        cardView.addSubview(backgroundImageView)
        cardView.addSubview(pinImageView)
        cardView.addSubview(titleLabel)
        cardView.addSubview(contentLabel)
        cardView.addSubview(confirmButton)

        cardView.snp.makeConstraints { make in
            make.centerY.centerX.equalToSuperview()
            make.left.right.equalToSuperview().inset(30)
        }

        backgroundImageView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
        }
        
        pinImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(cardView.snp.top)
            make.size.equalTo(CGSize(width: 88, height: 88))
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(pinImageView.snp.bottom).offset(20)
            make.left.right.equalTo(cardView).inset(20)
        }

        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.right.equalTo(titleLabel)
        }

        confirmButton.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(18)
            make.left.right.equalTo(titleLabel)
            make.height.equalTo(48)
            make.bottom.equalTo(cardView.snp.bottom).inset(20)
        }
    }
}
