//
//  LocationReportDialogPresenter.swift
//  suixinkan
//

import UIKit

/// 位置上报相关弹窗，对齐 Android 文案。
enum LocationReportDialogPresenter {

    /// 展示切换在线状态确认弹窗。
    @MainActor
    static func presentOnlineStatusSwitch(
        from viewController: UIViewController,
        isOnline: Bool,
        onConfirm: @escaping () -> Void,
        onCancel: (() -> Void)? = nil
    ) {
        let message = isOnline
            ? "是否确认切换为离线状态?\n离线后将暂停位置上报,直到下次手动切换为在线状态"
            : "是否确认切换为在线状态?\n在线后将开始位置上报和计时"
        let alert = UIAlertController(title: "切换在线状态", message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "取消", style: .cancel) { _ in onCancel?() })
        alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in onConfirm() })
        viewController.present(alert, animated: true)
    }

    /// 展示上报成功弹窗。
    @MainActor
    static func presentReportSuccess(
        from viewController: UIViewController,
        reportTime: String,
        countdown: String,
        onDismiss: (() -> Void)? = nil
    ) {
        let message = "上报已成功,上报时间为\(reportTime)\n距离下次上报时间\(countdown)"
        let alert = UIAlertController(title: "上报成功", message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in onDismiss?() })
        viewController.present(alert, animated: true)
    }

    /// 展示位置超时提醒弹窗。
    @MainActor
    static func presentLocationTimeout(
        from viewController: UIViewController,
        onReport: @escaping () -> Void,
        onLater: (() -> Void)? = nil
    ) {
        let alert = UIAlertController(
            title: "位置上报提醒",
            message: "倒计时即将结束，请立即上报位置",
            preferredStyle: .alert
        )
        alert.addAction(UIAlertAction(title: "稍后", style: .cancel) { _ in onLater?() })
        alert.addAction(UIAlertAction(title: "立即上报", style: .default) { _ in onReport() })
        viewController.present(alert, animated: true)
    }

    /// 展示提醒设置选择器。
    @MainActor
    static func presentReminderPicker(
        from viewController: UIViewController,
        onSelect: @escaping (Int) -> Void
    ) {
        let alert = UIAlertController(title: "提醒设置", message: "提前提醒时间", preferredStyle: .actionSheet)
        [0, 5, 10, 15, 30].forEach { minutes in
            let title = minutes == 0 ? "不提醒" : "提前\(minutes)分钟"
            alert.addAction(UIAlertAction(title: title, style: .default) { _ in onSelect(minutes) })
        }
        alert.addAction(UIAlertAction(title: "取消", style: .cancel))
        viewController.present(alert, animated: true)
    }
}
