package com.narutohuo.xindazhou.common.version.ui

import android.app.AlertDialog
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import com.narutohuo.xindazhou.common.version.model.VersionInfo

/**
 * 版本更新对话框
 */
class UpdateDialog : DialogFragment() {
    
    private var versionInfo: VersionInfo? = null
    private var onDismiss: (() -> Unit)? = null
    
    companion object {
        fun create(
            versionInfo: VersionInfo,
            onDismiss: (() -> Unit)? = null
        ): UpdateDialog {
            return UpdateDialog().apply {
                this.versionInfo = versionInfo
                this.onDismiss = onDismiss
            }
        }
    }
    
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val info = versionInfo ?: return super.onCreateDialog(savedInstanceState)
        val builder = AlertDialog.Builder(requireContext())
        builder.setTitle("发现新版本")
        builder.setMessage("版本号: ${info.versionName}\n更新内容: ${info.versionDesc ?: "优化体验"}")
        
        if (info.forceUpdate == true) {
            // 强制更新，只能点击更新
            builder.setCancelable(false)
            builder.setPositiveButton("立即更新") { _, _ ->
                // TODO: 打开下载链接或应用商店
                onDismiss?.invoke()
            }
        } else {
            // 可选更新
            builder.setPositiveButton("立即更新") { _, _ ->
                // TODO: 打开下载链接或应用商店
                onDismiss?.invoke()
            }
            builder.setNegativeButton("稍后更新") { _, _ ->
                onDismiss?.invoke()
            }
        }
        
        return builder.create()
    }
    
    override fun onDismiss(dialog: android.content.DialogInterface) {
        super.onDismiss(dialog)
        onDismiss?.invoke()
    }
}

