//
//  HomeQuickActionsView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 首页快捷操作按钮区。
final class HomeQuickActionsView: UIView {

    var onCollectPayment: (() -> Void)?
    var onSubmitTask: (() -> Void)?
    var onToggleOnline: (() -> Void)?

    private let stackView = UIStackView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        stackView.axis = .horizontal
        stackView.distribution = .fillEqually
        stackView.spacing = AppSpacing.sm
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

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

    func apply(isOnline: Bool) {
        stackView.arrangedSubviews.forEach { $0.removeFromSuperview() }
        stackView.addArrangedSubview(makeActionButton(title: "立即收款", icon: "qrcode", action: #selector(collectPaymentTapped)))
        stackView.addArrangedSubview(makeActionButton(title: "提交任务", icon: "checklist.checked", action: #selector(submitTaskTapped)))
        stackView.addArrangedSubview(makeActionButton(
            title: isOnline ? "在线" : "离线",
            icon: isOnline ? "wifi" : "wifi.slash",
            action: #selector(toggleOnlineTapped),
            backgroundColor: isOnline ? AppColor.online : AppColor.cardBackground,
            iconBackgroundColor: isOnline ? UIColor.white.withAlphaComponent(0.18) : AppColor.inputBackground,
            iconColor: isOnline ? AppColor.successBackground : AppColor.textTabInactive,
            titleColor: isOnline ? AppColor.successBackground : AppColor.textTabInactive,
            showsBorder: !isOnline
        ))
    }

    private func makeActionButton(
        title: String,
        icon: String,
        action: Selector,
        backgroundColor: UIColor = AppColor.cardBackground,
        iconBackgroundColor: UIColor = AppColor.iconBackground,
        iconColor: UIColor = AppColor.primary,
        titleColor: UIColor = AppColor.textSecondary,
        showsBorder: Bool = true
    ) -> UIView {
        let container = UIView()
        container.backgroundColor = backgroundColor
        container.layer.cornerRadius = AppRadius.lg
        container.layer.borderWidth = showsBorder ? 1 : 0
        container.layer.borderColor = AppColor.cardOutline.cgColor
        container.accessibilityLabel = title

        let button = UIButton(type: .system)
        button.addTarget(self, action: action, for: .touchUpInside)
        container.addSubview(button)
        button.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        let iconContainer = UIView()
        iconContainer.backgroundColor = iconBackgroundColor
        iconContainer.layer.cornerRadius = 18

        let imageView = UIImageView(image: UIImage(systemName: icon))
        imageView.accessibilityIdentifier = icon
        imageView.tintColor = iconColor
        imageView.contentMode = .scaleAspectFit

        let label = UILabel()
        label.text = title
        label.font = .app(.body)
        label.textColor = titleColor
        label.textAlignment = .center
        label.numberOfLines = 2

        iconContainer.addSubview(imageView)

        let column = UIStackView(arrangedSubviews: [iconContainer, label])
        column.axis = .vertical
        column.alignment = .center
        column.spacing = AppSpacing.xs
        column.isUserInteractionEnabled = false
        container.addSubview(column)
        column.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
        }
        iconContainer.snp.makeConstraints { make in
            make.width.height.equalTo(36)
        }
        imageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(22)
        }
        return container
    }

    @objc private func collectPaymentTapped() { onCollectPayment?() }
    @objc private func submitTaskTapped() { onSubmitTask?() }
    @objc private func toggleOnlineTapped() { onToggleOnline?() }
}
