//
//  HomeWorkStatusCardView.swift
//  suixinkan
//

import SnapKit
import UIKit

/// 首页在线状态与倒计时卡片。
final class HomeWorkStatusCardView: UIView {

    var onOnlineTap: (() -> Void)?
    var onReminderTap: (() -> Void)?

    private let contentStackView = UIStackView()
    private let onlineButton = UIButton(type: .system)
    private let countdownStackView = UIStackView()
    private let countdownIconView = UIImageView(image: UIImage(systemName: "clock.fill"))
    private let countdownLabel = UILabel()
    private let reminderButton = UIButton(type: .system)

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = AppColor.cardBackground
        layer.cornerRadius = AppRadius.lg
        layer.borderWidth = 1
        layer.borderColor = AppColor.cardOutline.cgColor
        clipsToBounds = true

        contentStackView.axis = .horizontal
        contentStackView.alignment = .center
        contentStackView.distribution = .equalSpacing
        contentStackView.spacing = AppSpacing.sm

        onlineButton.titleLabel?.font = .app(.captionMedium)
        onlineButton.layer.cornerRadius = AppRadius.sm
        onlineButton.setConfigurationContentInsets(
            NSDirectionalEdgeInsets(top: 7, leading: 14, bottom: 7, trailing: 14)
        )
        onlineButton.addTarget(self, action: #selector(onlineTapped), for: .touchUpInside)

        countdownStackView.axis = .horizontal
        countdownStackView.alignment = .center
        countdownStackView.spacing = 6
        countdownIconView.tintColor = AppColor.primary
        countdownIconView.contentMode = .scaleAspectFit
        countdownIconView.setContentCompressionResistancePriority(.required, for: .horizontal)

        countdownLabel.font = .systemFont(ofSize: 18, weight: .semibold)
        countdownLabel.textColor = AppColor.primary
        countdownLabel.adjustsFontSizeToFitWidth = true
        countdownLabel.minimumScaleFactor = 0.85
        countdownLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

        reminderButton.titleLabel?.font = .systemFont(ofSize: 16, weight: .semibold)
        reminderButton.setTitleColor(AppColor.primary, for: .normal)
        reminderButton.tintColor = AppColor.primary
        let reminderIconConfig = UIImage.SymbolConfiguration(pointSize: 14, weight: .semibold)
        reminderButton.setImage(UIImage(systemName: "bell.fill", withConfiguration: reminderIconConfig), for: .normal)
        reminderButton.semanticContentAttribute = .forceLeftToRight
        reminderButton.setConfigurationImagePlacement(.leading, padding: 6)
        reminderButton.titleLabel?.adjustsFontSizeToFitWidth = true
        reminderButton.titleLabel?.minimumScaleFactor = 0.8
        reminderButton.addTarget(self, action: #selector(reminderTapped), for: .touchUpInside)

        countdownStackView.addArrangedSubview(countdownIconView)
        countdownStackView.addArrangedSubview(countdownLabel)
        contentStackView.addArrangedSubview(onlineButton)
        contentStackView.addArrangedSubview(countdownStackView)
        contentStackView.addArrangedSubview(reminderButton)
        addSubview(contentStackView)

        contentStackView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(AppSpacing.md)
        }
        countdownIconView.snp.makeConstraints { make in
            make.width.height.equalTo(18)
        }
        snp.makeConstraints { make in
            make.height.equalTo(84)
        }
    }

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

    func apply(isOnline: Bool, countdownText: String, reminderMinutes: Int) {
        onlineButton.setTitle(isOnline ? "在线" : "离线", for: .normal)
        if isOnline {
            onlineButton.backgroundColor = AppColor.online
            onlineButton.setTitleColor(AppColor.successBackground, for: .normal)
        } else {
            onlineButton.backgroundColor = AppColor.inputBackground
            onlineButton.setTitleColor(AppColor.textTabInactive, for: .normal)
        }
        countdownLabel.text = countdownText
        let reminderTitle = reminderMinutes == 0 ? "不提醒" : "提前\(reminderMinutes)分钟"
        reminderButton.setTitle(reminderTitle, for: .normal)
    }

    @objc private func onlineTapped() { onOnlineTap?() }
    @objc private func reminderTapped() { onReminderTap?() }
}
