//
//  OrderSourcePersonCell.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 获客员选择列表 cell，对齐 Android `SourcePersonItem`。
final class OrderSourcePersonCell: UITableViewCell {
    static let reuseIdentifier = "OrderSourcePersonCell"

    var onCallPhone: ((String) -> Void)?

    private let cardView = UIView()
    private let nameLabel = UILabel()
    private let phoneLabel = UILabel()
    private let callButton = UIButton(type: .system)
    private let chevronView = UIImageView()
    private var personPhone = ""

    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(person: OrderSourcePerson, selected: Bool) {
        nameLabel.text = person.name
        phoneLabel.text = person.phone
        personPhone = person.phone
        cardView.layer.borderColor = (selected ? AppColor.primary : OrderTokens.sourceBorder).cgColor
        cardView.backgroundColor = selected ? AppColor.primaryLight.withAlphaComponent(0.5) : .white
    }

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

        nameLabel.font = .systemFont(ofSize: 16, weight: .medium)
        nameLabel.textColor = AppColor.text333
        phoneLabel.font = .systemFont(ofSize: 14)
        phoneLabel.textColor = AppColor.text666

        callButton.backgroundColor = OrderTokens.phoneIconBackground
        callButton.layer.cornerRadius = 16
        callButton.tintColor = AppColor.primary
        callButton.setImage(UIImage(systemName: "phone.fill"), for: .normal)
        callButton.addTarget(self, action: #selector(callTapped), for: .touchUpInside)

        chevronView.image = UIImage(systemName: "chevron.right")
        chevronView.tintColor = AppColor.text666
        chevronView.contentMode = .scaleAspectFit

        cardView.addSubview(nameLabel)
        cardView.addSubview(phoneLabel)
        cardView.addSubview(callButton)
        cardView.addSubview(chevronView)

        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(
                UIEdgeInsets(top: 0, left: AppSpacing.md, bottom: 10, right: AppSpacing.md)
            )
        }
        nameLabel.snp.makeConstraints { make in
            make.leading.top.equalToSuperview().inset(14)
            make.trailing.lessThanOrEqualTo(callButton.snp.leading).offset(-8)
        }
        phoneLabel.snp.makeConstraints { make in
            make.leading.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(6)
            make.bottom.equalToSuperview().inset(14)
        }
        chevronView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(14)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        }
        callButton.snp.makeConstraints { make in
            make.trailing.equalTo(chevronView.snp.leading).offset(-8)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(32)
        }
    }

    @objc private func callTapped() {
        onCallPhone?(personPhone)
    }
}
