//
//  OrderSourceLeadCell.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 带客单选择列表 cell，对齐 Android `SourceLeadItem`。
final class OrderSourceLeadCell: UITableViewCell {
    static let reuseIdentifier = "OrderSourceLeadCell"

    var onCallPhone: ((String) -> Void)?
    var onImageTap: (([String], Int) -> Void)?
    var onSelect: (() -> Void)?

    private let cardView = UIView()
    private let tagLabel = UILabel()
    private let checkView = UIImageView()
    private let contentViewPanel = OrderSourceLeadContentView()
    private let actionButton = UIButton(type: .system)
    private var currentImages: [String] = []

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        setupUI()
    }

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

    func configure(lead: OrderSourceLead, selected: Bool) {
        currentImages = lead.images
        contentViewPanel.apply(lead: lead, compact: false, enablePhoneCall: true)
        contentViewPanel.onCallPhone = { [weak self] phone in self?.onCallPhone?(phone) }
        contentViewPanel.onImageTap = { [weak self] images, index in
            self?.onImageTap?(images, index)
        }

        checkView.isHidden = !selected
        cardView.layer.borderColor = (selected ? AppColor.primary : OrderTokens.sourceBorder).cgColor
        cardView.backgroundColor = selected ? AppColor.primaryLight.withAlphaComponent(0.4) : .white
        actionButton.setTitle(selected ? "当前已选择" : "选择此带客单", for: .normal)
        actionButton.setTitleColor(selected ? AppColor.primary : .white, for: .normal)
        actionButton.backgroundColor = selected ? AppColor.primaryLight : AppColor.primary
    }

    private func setupUI() {
        cardView.layer.cornerRadius = AppRadius.sm
        cardView.layer.borderWidth = 1
        contentView.addSubview(cardView)

        tagLabel.text = "带客单"
        tagLabel.font = .systemFont(ofSize: 13, weight: .medium)
        tagLabel.textColor = AppColor.primary
        tagLabel.backgroundColor = AppColor.primaryLight.withAlphaComponent(0.8)
        tagLabel.layer.cornerRadius = 4
        tagLabel.clipsToBounds = true
        tagLabel.textAlignment = .center

        checkView.image = UIImage(systemName: "checkmark")
        checkView.tintColor = AppColor.primary
        checkView.contentMode = .scaleAspectFit

        actionButton.titleLabel?.font = .systemFont(ofSize: 14)
        actionButton.layer.cornerRadius = 6
        actionButton.addTarget(self, action: #selector(selectTapped), for: .touchUpInside)

        cardView.addSubview(tagLabel)
        cardView.addSubview(checkView)
        cardView.addSubview(contentViewPanel)
        cardView.addSubview(actionButton)

        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(
                UIEdgeInsets(top: 0, left: AppSpacing.md, bottom: 12, right: AppSpacing.md)
            )
        }
        tagLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview().inset(12)
            make.height.equalTo(24)
            make.width.greaterThanOrEqualTo(56)
        }
        checkView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(12)
            make.centerY.equalTo(tagLabel)
            make.width.height.equalTo(20)
        }
        contentViewPanel.snp.makeConstraints { make in
            make.top.equalTo(tagLabel.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview().inset(12)
        }
        actionButton.snp.makeConstraints { make in
            make.top.equalTo(contentViewPanel.snp.bottom).offset(10)
            make.leading.trailing.bottom.equalToSuperview().inset(12)
            make.height.equalTo(40)
        }
    }

    @objc private func selectTapped() {
        onSelect?()
    }
}
