//
//  OrderEditOptionView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 「需要剪辑修图」是/否单选，对齐 Android `OrderEditOptionSection`。
final class OrderEditOptionView: UIView {

    var onSelectionChange: ((Int) -> Void)?

    private let titleLabel = UILabel()
    private let readOnlyLabel = UILabel()
    private let yesButton = UIControl()
    private let noButton = UIControl()
    private let yesCircle = UIView()
    private let noCircle = UIView()
    private let yesLabel = UILabel()
    private let noLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLabel.text = "需要剪辑修图"
        titleLabel.font = .app(.body)
        titleLabel.textColor = OrderTokens.label563

        readOnlyLabel.font = .systemFont(ofSize: 14, weight: .medium)
        readOnlyLabel.textColor = OrderTokens.valueBlack
        readOnlyLabel.isHidden = true

        [yesLabel, noLabel].forEach {
            $0.text = $0 === yesLabel ? "是" : "否"
            $0.font = .app(.body)
            $0.textColor = AppColor.textPrimary
        }

        [yesCircle, noCircle].forEach {
            $0.layer.cornerRadius = 10
            $0.layer.borderWidth = 1.5
            $0.layer.borderColor = AppColor.primary.cgColor
            $0.snp.makeConstraints { make in
                make.width.height.equalTo(20)
            }
        }

        configureChoiceButton(yesButton, circle: yesCircle, label: yesLabel, tag: 1)
        configureChoiceButton(noButton, circle: noCircle, label: noLabel, tag: 2)

        let choicesStack = UIStackView(arrangedSubviews: [yesButton, noButton])
        choicesStack.axis = .horizontal
        choicesStack.spacing = AppSpacing.md

        addSubview(titleLabel)
        addSubview(readOnlyLabel)
        addSubview(choicesStack)

        titleLabel.snp.makeConstraints { make in
            make.leading.centerY.equalToSuperview()
        }
        readOnlyLabel.snp.makeConstraints { make in
            make.trailing.centerY.equalToSuperview()
        }
        choicesStack.snp.makeConstraints { make in
            make.trailing.centerY.equalToSuperview()
        }
        snp.makeConstraints { make in
            make.height.greaterThanOrEqualTo(28)
        }
    }

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

    func apply(isRefined: Int, readOnly: Bool) {
        readOnlyLabel.isHidden = !readOnly
        yesButton.isHidden = readOnly
        noButton.isHidden = readOnly
        if readOnly {
            readOnlyLabel.text = switch isRefined {
            case 1: "是"
            case 2: "否"
            default: "--"
            }
        } else {
            updateCircle(yesCircle, selected: isRefined == 1)
            updateCircle(noCircle, selected: isRefined == 2)
        }
    }

    private func configureChoiceButton(_ control: UIControl, circle: UIView, label: UILabel, tag: Int) {
        control.tag = tag
        control.addTarget(self, action: #selector(choiceTapped(_:)), for: .touchUpInside)
        let row = UIStackView(arrangedSubviews: [circle, label])
        row.axis = .horizontal
        row.spacing = AppSpacing.xxs
        row.isUserInteractionEnabled = false
        control.addSubview(row)
        row.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

    private func updateCircle(_ circle: UIView, selected: Bool) {
        if selected {
            circle.backgroundColor = AppColor.primary
            circle.layer.borderWidth = 0
            let check = UIImageView(image: UIImage(systemName: "checkmark"))
            check.tintColor = .white
            check.tag = 999
            circle.subviews.forEach { $0.removeFromSuperview() }
            circle.addSubview(check)
            check.snp.makeConstraints { make in
                make.center.equalToSuperview()
                make.width.height.equalTo(12)
            }
        } else {
            circle.backgroundColor = .clear
            circle.layer.borderWidth = 1.5
            circle.layer.borderColor = AppColor.primary.cgColor
            circle.subviews.forEach { $0.removeFromSuperview() }
        }
    }

    @objc private func choiceTapped(_ sender: UIControl) {
        onSelectionChange?(sender.tag)
    }
}
