//
//  OrderSourceEntryView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 订单卡片内「订单来源」入口，对齐 Android `OrderSourcePicker` 折叠态。
final class OrderSourceEntryView: UIControl {

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

    private let titleLabel = UILabel()
    private let chevronView = UIImageView()
    private let hintLabel = UILabel()
    private let personRow = UIStackView()
    private let personNameLabel = UILabel()
    private let personPhoneLabel = UILabel()
    private let personSpacerView = UIView()
    private let divider = UIView()
    private let leadContentView = OrderSourceLeadContentView()
    private var currentImages: [String] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

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

    /// 应用订单来源展示状态。
    func apply(selection: OrderSourceSelection?) {
        if let selection {
            hintLabel.isHidden = true
            personRow.isHidden = false
            divider.isHidden = false
            leadContentView.isHidden = false

            personNameLabel.text = selection.person.name
            let phone = selection.person.phone.trimmingCharacters(in: .whitespacesAndNewlines)
            personPhoneLabel.text = phone
            personPhoneLabel.isHidden = phone.isEmpty

            currentImages = selection.lead.images
            leadContentView.apply(lead: selection.lead, compact: true, enablePhoneCall: false)
            leadContentView.onImageTap = { [weak self] _, index in
                guard let self else { return }
                self.onImageTap?(self.currentImages, index)
            }
        } else {
            hintLabel.isHidden = false
            personRow.isHidden = true
            divider.isHidden = true
            leadContentView.isHidden = true
            currentImages = []
        }
    }

    private func setupUI() {
        backgroundColor = OrderTokens.sourceBackground
        layer.cornerRadius = AppRadius.sm
        layer.borderWidth = 1
        layer.borderColor = OrderTokens.sourceBorder.cgColor

        titleLabel.text = "订单来源"
        titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
        titleLabel.textColor = OrderTokens.label563

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

        hintLabel.text = "选择获客员及其创建的带客单"
        hintLabel.font = .systemFont(ofSize: 13)
        hintLabel.textColor = AppColor.text666
        hintLabel.numberOfLines = 0

        personRow.axis = .horizontal
        personRow.alignment = .center
        personRow.spacing = 10
        personNameLabel.font = .systemFont(ofSize: 13, weight: .medium)
        personNameLabel.textColor = AppColor.text333
        personNameLabel.lineBreakMode = .byTruncatingTail
        personNameLabel.setContentHuggingPriority(.required, for: .horizontal)
        personNameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        personPhoneLabel.font = .systemFont(ofSize: 13)
        personPhoneLabel.textColor = AppColor.text666
        personPhoneLabel.lineBreakMode = .byTruncatingTail
        personPhoneLabel.setContentHuggingPriority(.required, for: .horizontal)
        personPhoneLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        personSpacerView.setContentHuggingPriority(.defaultLow, for: .horizontal)
        personSpacerView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        personRow.addArrangedSubview(personNameLabel)
        personRow.addArrangedSubview(personPhoneLabel)
        personRow.addArrangedSubview(personSpacerView)

        divider.backgroundColor = OrderTokens.sourceDivider

        let headerRow = UIStackView(arrangedSubviews: [titleLabel, UIView(), chevronView])
        headerRow.axis = .horizontal
        headerRow.alignment = .center

        let stack = UIStackView(arrangedSubviews: [headerRow, hintLabel, personRow, divider, leadContentView])
        stack.axis = .vertical
        stack.spacing = 10
        stack.isUserInteractionEnabled = false
        addSubview(stack)
        stack.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(AppSpacing.sm)
        }
        chevronView.snp.makeConstraints { make in
            make.width.height.equalTo(20)
        }
        divider.snp.makeConstraints { make in
            make.height.equalTo(OrderTokens.dividerHeight)
        }
    }
}
