//
//  HomeCollectionCells.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 首页在线状态卡片 cell。
final class HomeWorkStatusCell: UICollectionViewCell {

    static let reuseIdentifier = "HomeWorkStatusCell"

    let cardView = HomeWorkStatusCardView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(cardView)
        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

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

    func apply(isOnline: Bool, countdownText: String, reminderMinutes: Int) {
        cardView.apply(isOnline: isOnline, countdownText: countdownText, reminderMinutes: reminderMinutes)
    }
}

/// 首页位置上报卡片 cell。
final class HomeLocationReportCell: UICollectionViewCell {

    static let reuseIdentifier = "HomeLocationReportCell"

    let cardView = HomeLocationReportCardView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(cardView)
        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

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

    func apply(address: String, isReporting: Bool) {
        cardView.apply(address: address, isReporting: isReporting)
    }
}

/// 首页快捷操作 cell。
final class HomeQuickActionsCell: UICollectionViewCell {

    static let reuseIdentifier = "HomeQuickActionsCell"

    let cardView = HomeQuickActionsView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(cardView)
        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

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

    func apply(isOnline: Bool) {
        cardView.apply(isOnline: isOnline)
    }
}

/// 首页门店信息 cell。
final class HomeStoreCell: UICollectionViewCell {

    static let reuseIdentifier = "HomeStoreCell"

    let cardView = HomeStoreCardView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(cardView)
        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }

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

    func apply(store: StoreItem) {
        cardView.apply(store: store)
    }
}

/// 首页常用应用菜单 cell。
final class HomeMenuCell: UICollectionViewCell {

    static let reuseIdentifier = "HomeMenuCell"

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

    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()
        iconView.image = nil
        iconView.accessibilityIdentifier = nil
        titleLabel.text = nil
    }

    func apply(menu: HomeMenuItem) {
        iconView.image = HomeMenuIconFactory.image(named: menu.iconName)
        iconView.accessibilityIdentifier = menu.iconName
        titleLabel.text = menu.title
    }

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

        cardView.backgroundColor = AppColor.cardBackground
        cardView.layer.cornerRadius = AppRadius.lg
        cardView.layer.borderWidth = 1
        cardView.layer.borderColor = AppColor.cardOutline.cgColor
        cardView.clipsToBounds = true

        iconContainerView.backgroundColor = AppColor.iconBackground
        iconContainerView.layer.cornerRadius = 20

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

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

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

        contentView.addSubview(cardView)
        cardView.addSubview(contentStackView)
        iconContainerView.addSubview(iconView)
        contentStackView.addArrangedSubview(iconContainerView)
        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.xs)
        }
        iconContainerView.snp.makeConstraints { make in
            make.width.height.equalTo(40)
        }
        iconContainerView.setContentHuggingPriority(.required, for: .vertical)
        iconContainerView.setContentCompressionResistancePriority(.required, for: .vertical)
        iconView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.height.equalTo(24)
        }
    }
}

/// 首页分区标题 supplementary view。
final class HomeSectionHeaderView: UICollectionReusableView {

    static let reuseIdentifier = "HomeSectionHeaderView"

    private let titleLabel = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        titleLabel.font = .app(.title)
        titleLabel.textColor = AppColor.textPrimary
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
        }
    }

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

    func apply(title: String) {
        titleLabel.text = title
    }
}
