//
//  OrderFilterPillControl.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 订单筛选灰 pill，对齐 Android 筛选/时间控件。
final class OrderFilterPillControl: UIButton {

    private let pillTitleLabel = UILabel()
    private let trailingIconView = UIImageView()

    var pillTitle: String = "" {
        didSet { pillTitleLabel.text = pillTitle }
    }

    var showsTrailingIcon: Bool = false {
        didSet { trailingIconView.isHidden = !showsTrailingIcon }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = OrderTokens.filterBackground
        layer.cornerRadius = OrderTokens.pillRadius
        clipsToBounds = true

        pillTitleLabel.font = .app(.body)
        pillTitleLabel.textColor = OrderTokens.label563
        pillTitleLabel.lineBreakMode = .byTruncatingTail
        pillTitleLabel.isUserInteractionEnabled = false

        trailingIconView.contentMode = .scaleAspectFit
        trailingIconView.tintColor = OrderTokens.label563
        trailingIconView.isHidden = true
        trailingIconView.isUserInteractionEnabled = false

        addSubview(pillTitleLabel)
        addSubview(trailingIconView)

        pillTitleLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(AppSpacing.sm)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualTo(trailingIconView.snp.leading).offset(-AppSpacing.xxs)
        }
        trailingIconView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().inset(AppSpacing.sm)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(15)
        }
        snp.makeConstraints { make in
            make.height.equalTo(OrderTokens.filterPillHeight)
        }
    }

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

    func setTrailingSystemImage(_ name: String) {
        trailingIconView.image = UIImage(systemName: name)
        showsTrailingIcon = true
    }
}
