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

import SnapKit
import UIKit

/// 全部功能页菜单卡片，整张卡片响应选择，右上角图标仅展示添加或移除状态。
final class AllFunctionMenuCell: UICollectionViewCell {

    static let reuseIdentifier = "AllFunctionMenuCell"

    /// 卡片右上角展示的操作状态。
    enum ActionStyle {
        case none
        case add
        case remove
    }

    private let cardView = UIView()
    private let contentStackView = UIStackView()
    private let iconView = UIImageView()
    private let titleLabel = UILabel()
    private let actionImageView = UIImageView()

    override var isHighlighted: Bool {
        didSet {
            cardView.alpha = isHighlighted ? 0.72 : 1
        }
    }

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

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

    /// 更新菜单内容、操作状态及整张卡片的无障碍语义。
    func apply(menu: HomeMenuItem, actionStyle: ActionStyle) {
        iconView.image = HomeMenuIconFactory.image(named: menu.iconName)
        titleLabel.text = menu.title
        accessibilityLabel = menu.title


        switch actionStyle {
        case .none:
            actionImageView.isHidden = true
            accessibilityTraits = []
            accessibilityHint = nil
        case .add:
            actionImageView.isHidden = false
            actionImageView.image = UIImage(systemName: "plus.circle.fill")
            actionImageView.tintColor = AppColor.primary
            accessibilityTraits = .button
            accessibilityHint = "添加到常用应用"
        case .remove:
            actionImageView.isHidden = false
            actionImageView.image = UIImage(systemName: "minus.circle.fill")
            actionImageView.tintColor = AppColor.danger
            accessibilityTraits = .button
            accessibilityHint = "移出常用应用"
        }
    }

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

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

        iconView.tintColor = AppColor.primary
        iconView.contentMode = .center
        iconView.adjustsImageSizeForAccessibilityContentSizeCategory = false

        contentStackView.axis = .vertical
        contentStackView.alignment = .center
        contentStackView.spacing = AppSpacing.sm
        contentStackView.isUserInteractionEnabled = false

        titleLabel.font = .app(.body)
        titleLabel.textColor = AppColor.textSecondary
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 2

        isAccessibilityElement = true
        actionImageView.contentMode = .scaleAspectFit
        actionImageView.isUserInteractionEnabled = false

        contentView.addSubview(cardView)
        cardView.addSubview(contentStackView)
        cardView.addSubview(actionImageView)
        contentStackView.addArrangedSubview(iconView)
        contentStackView.addArrangedSubview(titleLabel)

        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        contentStackView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(AppSpacing.xxs)
        }
        iconView.snp.makeConstraints { make in
            make.width.height.equalTo(26)
        }
        actionImageView.snp.makeConstraints { make in
            make.top.trailing.equalToSuperview().inset(AppSpacing.xxs)
            make.width.height.equalTo(20)
        }
    }
}
