//
//  AllFunctionMenuCell.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 全部功能页菜单卡片，支持右上角添加或移除操作。
final class AllFunctionMenuCell: UICollectionViewCell {

    static let reuseIdentifier = "AllFunctionMenuCell"

    enum ActionStyle {
        case none
        case add
        case remove
    }

    var onActionTap: (() -> Void)?

    private let cardView = UIView()
    private let iconView = UIImageView()
    private let titleLabel = UILabel()
    private let actionButton = UIButton(type: .system)

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

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

    override func prepareForReuse() {
        super.prepareForReuse()
        onActionTap = nil
    }

    func apply(menu: HomeMenuItem, actionStyle: ActionStyle) {
        iconView.image = UIImage(systemName: menu.iconName)
        titleLabel.text = menu.title

        switch actionStyle {
        case .none:
            actionButton.isHidden = true
        case .add:
            actionButton.isHidden = false
            actionButton.setImage(UIImage(systemName: "plus.circle.fill"), for: .normal)
            actionButton.tintColor = AppColor.primary
        case .remove:
            actionButton.isHidden = false
            actionButton.setImage(UIImage(systemName: "minus.circle.fill"), for: .normal)
            actionButton.tintColor = AppColor.danger
        }
    }

    private func setupUI() {
        contentView.backgroundColor = .clear

        cardView.backgroundColor = .white
        cardView.layer.cornerRadius = AppRadius.md
        cardView.clipsToBounds = true

        iconView.tintColor = AppColor.primary
        iconView.contentMode = .scaleAspectFit

        titleLabel.font = .app(.caption)
        titleLabel.textColor = AppColor.textPrimary
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 2

        actionButton.addTarget(self, action: #selector(actionTapped), for: .touchUpInside)

        contentView.addSubview(cardView)
        cardView.addSubview(iconView)
        cardView.addSubview(titleLabel)
        cardView.addSubview(actionButton)

        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        iconView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(AppSpacing.sm)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(32)
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(iconView.snp.bottom).offset(AppSpacing.xs)
            make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
            make.bottom.lessThanOrEqualToSuperview().inset(AppSpacing.xs)
        }
        actionButton.snp.makeConstraints { make in
            make.top.trailing.equalToSuperview().inset(AppSpacing.xxs)
            make.width.height.equalTo(20)
        }
    }

    @objc private func actionTapped() {
        onActionTap?()
    }
}
