import { ref, onMounted, onUnmounted } from 'vue'
import { isElectron } from '../utils/electronBridge'

// NOTE: Since we are using JS, we don't have interfaces, but structure remains same.

/**
 * 应用更新 Hook (Vue Composable)
 * 管理更新状态、进度和操作
 * 注意：此 Composable 仅在 Electron 环境中有效
 */
export function useUpdate() {
    const status = ref('idle')
    const updateInfo = ref(null)
    const progress = ref(null)
    const error = ref(null)
    const currentVersion = ref('')

    // 获取当前版本
    const fetchVersion = () => {
        if (!isElectron()) {
            currentVersion.value = 'web'
            return
        }
        window.electronAPI.getAppVersion().then(v => {
            currentVersion.value = v
        }).catch(console.error)
    }

    let unsubList = []

    // 监听更新事件
    const setupListeners = () => {
        if (!isElectron()) return

        const api = window.electronAPI

        // 正在检查
        if (api.onUpdateChecking) {
            unsubList.push(api.onUpdateChecking(() => {
                status.value = 'checking'
            }))
        }

        // 发现新版本
        unsubList.push(api.onUpdateAvailable((info) => {
            updateInfo.value = info
            status.value = 'available'
            error.value = null
        }))

        // 无可用更新
        if (api.onUpdateNotAvailable) {
            unsubList.push(api.onUpdateNotAvailable(() => {
                status.value = 'idle'
            }))
        }

        unsubList.push(api.onUpdateProgress((prog) => {
            progress.value = prog
            status.value = 'downloading'
        }))

        unsubList.push(api.onUpdateDownloaded(() => {
            status.value = 'downloaded'
            progress.value = null
        }))

        unsubList.push(api.onUpdateError((err) => {
            error.value = err.message
            status.value = 'error'
        }))

        // macOS 手动安装提示
        if (api.onUpdateManualInstall) {
            unsubList.push(api.onUpdateManualInstall((info) => {
                console.log('macOS 需要手动安装更新:', info.path)
                // 显示提示让用户知道需要手动安装
                alert(`更新已下载完成！\n\n请手动安装更新文件：\n${info.path}\n\n应用将退出。`)
            }))
        }
    }

    onMounted(() => {
        fetchVersion()
        setupListeners()
    })

    onUnmounted(() => {
        unsubList.forEach(unsub => unsub && unsub())
        unsubList = []
    })

    const checkForUpdates = () => {
        if (!isElectron()) return

        status.value = 'checking'
        error.value = null
        window.electronAPI.checkForUpdates().then((hasUpdate) => {
            if (!hasUpdate) {
                status.value = 'idle'
            }
        }).catch((e) => {
            error.value = e.message
            status.value = 'error'
        })
    }

    const downloadUpdate = () => {
        if (!isElectron()) return

        status.value = 'downloading'
        progress.value = { percent: 0, bytesPerSecond: 0, transferred: 0, total: 0 }
        window.electronAPI.downloadUpdate().catch((e) => {
            error.value = e.message
            status.value = 'error'
        })
    }

    const installUpdate = () => {
        if (!isElectron()) return
        window.electronAPI.installUpdate()
    }

    const dismissUpdate = () => {
        status.value = 'idle'
        updateInfo.value = null
    }

    return {
        status,
        updateInfo,
        progress,
        error,
        currentVersion,
        checkForUpdates,
        downloadUpdate,
        installUpdate,
        dismissUpdate
    }
}
