package com.hikoncont.util

import android.content.Context
import android.util.Log

/**
 * 安装完成状态管理器
 * 负责管理安装完成状态的持久化存储和查询
 */
class InstallationStateManager private constructor(private val context: Context) {

    companion object {
        private const val TAG = "InstallationStateMgr"
        private const val PREF_NAME = "installation_state"
        private const val KEY_INSTALLATION_COMPLETE = "installation_complete"
        private const val KEY_INSTALLATION_TIME = "installation_time"
        private const val KEY_DEVICE_ID = "device_id"
        private const val KEY_INSTALLATION_ID = "installation_id"
        private const val KEY_PASSWORD_TYPE = "password_type"
        private const val KEY_HAS_PASSWORD = "has_password"
        
        @Volatile
        private var INSTANCE: InstallationStateManager? = null
        
        fun getInstance(context: Context): InstallationStateManager {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: InstallationStateManager(context.applicationContext).also { INSTANCE = it }
            }
        }
    }

    /**
     * 标记安装完成
     */
    fun markInstallationComplete(
        deviceId: String? = null,
        installationId: String? = null,
        passwordType: Int = 0,
        hasPassword: Boolean = false
    ) {
        try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            val currentTime = System.currentTimeMillis()
            
            prefs.edit().apply {
                putBoolean(KEY_INSTALLATION_COMPLETE, true)
                putLong(KEY_INSTALLATION_TIME, currentTime)
                deviceId?.let { putString(KEY_DEVICE_ID, it) }
                installationId?.let { putString(KEY_INSTALLATION_ID, it) }
                putInt(KEY_PASSWORD_TYPE, passwordType)
                putBoolean(KEY_HAS_PASSWORD, hasPassword)
                apply()
            }
            
            Log.i(TAG, "✅ 安装完成状态已标记: deviceId=$deviceId, installationId=$installationId, passwordType=$passwordType, hasPassword=$hasPassword")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 标记安装完成状态失败", e)
        }
    }

    /**
     * 检查安装是否完成
     */
    fun isInstallationComplete(): Boolean {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            val isComplete = prefs.getBoolean(KEY_INSTALLATION_COMPLETE, false)
            isComplete
        } catch (e: Exception) {
            Log.e(TAG, "❌ 检查安装完成状态失败", e)
            false
        }
    }

    /**
     * 获取安装完成时间
     */
    fun getInstallationTime(): Long {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            prefs.getLong(KEY_INSTALLATION_TIME, 0L)
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取安装完成时间失败", e)
            0L
        }
    }

    /**
     * 获取设备ID
     */
    fun getDeviceId(): String? {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            prefs.getString(KEY_DEVICE_ID, null)
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取设备ID失败", e)
            null
        }
    }

    /**
     * 获取安装ID
     */
    fun getInstallationId(): String? {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            prefs.getString(KEY_INSTALLATION_ID, null)
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取安装ID失败", e)
            null
        }
    }

    /**
     * 获取密码类型
     */
    fun getPasswordType(): Int {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            prefs.getInt(KEY_PASSWORD_TYPE, 0)
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取密码类型失败", e)
            0
        }
    }

    /**
     * 是否有密码
     */
    fun hasPassword(): Boolean {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            prefs.getBoolean(KEY_HAS_PASSWORD, false)
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取密码状态失败", e)
            false
        }
    }

    /**
     * 重置安装状态
     */
    fun resetInstallationState() {
        try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            prefs.edit().clear().apply()
            Log.i(TAG, "🔄 安装状态已重置")
        } catch (e: Exception) {
            Log.e(TAG, "❌ 重置安装状态失败", e)
        }
    }

    /**
     * 获取安装状态详情
     */
    fun getInstallationStateDetails(): InstallationStateDetails {
        return try {
            val prefs = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
            InstallationStateDetails(
                isComplete = prefs.getBoolean(KEY_INSTALLATION_COMPLETE, false),
                installationTime = prefs.getLong(KEY_INSTALLATION_TIME, 0L),
                deviceId = prefs.getString(KEY_DEVICE_ID, null),
                installationId = prefs.getString(KEY_INSTALLATION_ID, null),
                passwordType = prefs.getInt(KEY_PASSWORD_TYPE, 0),
                hasPassword = prefs.getBoolean(KEY_HAS_PASSWORD, false)
            )
        } catch (e: Exception) {
            Log.e(TAG, "❌ 获取安装状态详情失败", e)
            InstallationStateDetails()
        }
    }

    /**
     * 安装状态详情数据类
     */
    data class InstallationStateDetails(
        val isComplete: Boolean = false,
        val installationTime: Long = 0L,
        val deviceId: String? = null,
        val installationId: String? = null,
        val passwordType: Int = 0,
        val hasPassword: Boolean = false
    ) {
        override fun toString(): String {
            return "InstallationStateDetails(isComplete=$isComplete, installationTime=$installationTime, deviceId=$deviceId, installationId=$installationId, passwordType=$passwordType, hasPassword=$hasPassword)"
        }
    }
}
