package com.narutohuo.xindazhou.core.storage

import android.content.Context
import android.content.SharedPreferences
import java.io.File

/**
 * 存储服务实现类（基于 SharedPreferences）
 * 
 * 统一提供键值存储和文件存储功能
 * 单例模式，需要在 Application 中初始化
 * 
 * 使用方式：
 * ```kotlin
 * // 在 Application 中初始化
 * StorageImpl.init(context)
 * 
 * // 使用存储
 * StorageImpl.putString("key", "value")
 * val value = StorageImpl.getString("key")
 * ```
 */
object StorageImpl : IStorage {
    
    private const val PREFS_NAME = "xdz_storage_prefs"
    private var prefs: SharedPreferences? = null
    private var context: Context? = null
    private var initialized = false
    
    /**
     * 初始化存储服务
     * 
     * 需要在 Application.onCreate() 中调用
     * 
     * @param ctx 应用上下文
     */
    fun init(ctx: Context) {
        context = ctx.applicationContext
        prefs = context?.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
        initialized = true
    }
    
    /**
     * 检查是否已初始化
     */
    fun isInitialized(): Boolean = initialized
    
    // ========== 键值存储 ==========
    
    override fun putString(key: String, value: String) {
        prefs?.edit()?.putString(key, value)?.apply()
    }
    
    override fun getString(key: String, defaultValue: String): String {
        return prefs?.getString(key, defaultValue) ?: defaultValue
    }
    
    override fun putInt(key: String, value: Int) {
        prefs?.edit()?.putInt(key, value)?.apply()
    }
    
    override fun getInt(key: String, defaultValue: Int): Int {
        return prefs?.getInt(key, defaultValue) ?: defaultValue
    }
    
    override fun putLong(key: String, value: Long) {
        prefs?.edit()?.putLong(key, value)?.apply()
    }
    
    override fun getLong(key: String, defaultValue: Long): Long {
        return prefs?.getLong(key, defaultValue) ?: defaultValue
    }
    
    override fun putBoolean(key: String, value: Boolean) {
        prefs?.edit()?.putBoolean(key, value)?.apply()
    }
    
    override fun getBoolean(key: String, defaultValue: Boolean): Boolean {
        return prefs?.getBoolean(key, defaultValue) ?: defaultValue
    }
    
    override fun remove(key: String) {
        prefs?.edit()?.remove(key)?.apply()
    }
    
    override fun clear() {
        prefs?.edit()?.clear()?.apply()
    }
    
    // ========== 文件存储 ==========
    
    override fun saveFile(path: String, data: ByteArray): Boolean {
        return try {
            val ctx = context ?: return false
            val file = File(ctx.filesDir, path)
            file.parentFile?.mkdirs()
            file.writeBytes(data)
            true
        } catch (e: Exception) {
            false
        }
    }
    
    override fun readFile(path: String): ByteArray? {
        return try {
            val ctx = context ?: return null
            val file = File(ctx.filesDir, path)
            if (file.exists()) file.readBytes() else null
        } catch (e: Exception) {
            null
        }
    }
    
    override fun deleteFile(path: String): Boolean {
        return try {
            val ctx = context ?: return false
            val file = File(ctx.filesDir, path)
            if (file.exists()) file.delete() else false
        } catch (e: Exception) {
            false
        }
    }
}

