//
//  OrderPhoneRowView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 订单手机号行，支持摄影师/店铺两种 Android 样式。
final class OrderPhoneRowView: UIView {

    enum Style {
        case photographer
        case deposit
    }

    var onTap: (() -> Void)?

    private let style: Style
    private let titleLabel = UILabel()
    private let phoneLabel = UILabel()
    private let phoneButton = UIButton(type: .system)
    private let iconButton = UIButton(type: .system)

    init(style: Style) {
        self.style = style
        super.init(frame: .zero)
        setupUI()
    }

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

    func apply(phone: String, hidden: Bool = false) {
        let masked = OrderPhoneMask.mask(phone)
        phoneLabel.text = masked
        isHidden = hidden || phone.isEmpty
    }

    private func setupUI() {
        titleLabel.font = .app(.body)
        titleLabel.textColor = OrderTokens.label563
        titleLabel.text = "手机号："

        phoneLabel.font = .systemFont(ofSize: 14, weight: .medium)
        phoneLabel.textAlignment = .right

        phoneButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
        iconButton.addTarget(self, action: #selector(handleTap), for: .touchUpInside)

        addSubview(titleLabel)
        addSubview(phoneLabel)
        addSubview(phoneButton)
        addSubview(iconButton)

        titleLabel.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
        }

        switch style {
        case .photographer:
            phoneLabel.textColor = OrderTokens.valueBlack
            iconButton.backgroundColor = OrderTokens.phoneIconBackground
            iconButton.layer.cornerRadius = 12
            iconButton.tintColor = AppColor.primary
            iconButton.setImage(UIImage(systemName: "phone.fill"), for: .normal)
            iconButton.snp.makeConstraints { make in
                make.trailing.centerY.equalToSuperview()
                make.width.height.equalTo(24)
            }
            phoneLabel.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalTo(iconButton.snp.leading).offset(-7)
            }
        case .deposit:
            phoneLabel.textColor = AppColor.primary
            iconButton.tintColor = AppColor.primary
            iconButton.setImage(UIImage(systemName: "phone.fill"), for: .normal)
            iconButton.snp.makeConstraints { make in
                make.trailing.centerY.equalToSuperview()
                make.width.height.equalTo(16)
            }
            phoneLabel.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.trailing.equalTo(iconButton.snp.leading).offset(-AppSpacing.xxs)
            }
        }

        phoneButton.snp.makeConstraints { make in
            make.leading.equalTo(phoneLabel)
            make.trailing.equalTo(iconButton)
            make.top.bottom.equalToSuperview()
        }
    }

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