//
//  OrderHistoricalPhotosView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 历史拍摄缩略图行，对齐 Android multi_travel 区块。
final class OrderHistoricalPhotosView: UIView {

    var onTap: (() -> Void)?

    private let titleLabel = UILabel()
    private let divider = UIView()
    private let photosStack = UIStackView()
    private var imageViews: [UIImageView] = []
    private var overlayLabel: UILabel?

    override init(frame: CGRect) {
        super.init(frame: frame)
        divider.backgroundColor = OrderTokens.remarkDivider

        titleLabel.text = "历史拍摄信息:"
        titleLabel.font = .app(.body)
        titleLabel.textColor = OrderTokens.label563

        photosStack.axis = .horizontal
        photosStack.spacing = AppSpacing.xs
        photosStack.distribution = .fillEqually

        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        addGestureRecognizer(tap)

        addSubview(divider)
        addSubview(titleLabel)
        addSubview(photosStack)

        divider.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(1)
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(divider.snp.bottom).offset(AppSpacing.sm)
            make.leading.trailing.equalToSuperview()
        }
        photosStack.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(AppSpacing.xs)
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(72)
        }
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func apply(materials: [MultiTravelMaterialEntity]) {
        isHidden = materials.isEmpty
        photosStack.arrangedSubviews.forEach { $0.removeFromSuperview() }
        imageViews.removeAll()
        overlayLabel?.removeFromSuperview()
        overlayLabel = nil

        let displayItems = Array(materials.prefix(3))
        for (index, material) in displayItems.enumerated() {
            let container = UIView()
            container.backgroundColor = OrderTokens.filterBackground
            container.layer.cornerRadius = AppRadius.sm
            container.clipsToBounds = true

            let imageView = UIImageView()
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            let thumbURL = material.coverUrl.isEmpty ? material.fileUrl : material.coverUrl
            imageView.loadRemoteImage(urlString: thumbURL, placeholderColor: OrderTokens.filterBackground)
            container.addSubview(imageView)
            imageView.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            imageViews.append(imageView)
            photosStack.addArrangedSubview(container)

            if materials.count > 3, index == 2 {
                let overlay = UIView()
                overlay.backgroundColor = UIColor.black.withAlphaComponent(0.5)
                let label = UILabel()
                label.text = "查看更多"
                label.font = .systemFont(ofSize: 14, weight: .medium)
                label.textColor = .white
                label.textAlignment = .center
                overlay.addSubview(label)
                label.snp.makeConstraints { make in
                    make.center.equalToSuperview()
                }
                container.addSubview(overlay)
                overlay.snp.makeConstraints { make in
                    make.edges.equalToSuperview()
                }
                overlayLabel = label
            }
        }
    }

    @objc private func handleTap() {
        onTap?()
    }
}
