package com.hikoncont.util

import android.content.Context
import android.util.Log
import org.json.JSONObject
import java.io.File

object WebViewStateStore {
    private const val TAG = "WebViewStateStore"
    private const val FILE_NAME = "webview_state_test.json"

    private fun getStateFile(context: Context): File = File(context.filesDir, FILE_NAME)

    fun hasOpened(context: Context): Boolean {
        return try {
            val file = getStateFile(context)
            if (!file.exists()) return false
            val text = file.readText()
            val json = JSONObject(text)
            json.optBoolean("opened", false)
        } catch (e: Exception) {
            Log.e(TAG, "读取WebView状态文件失败", e)
            false
        }
    }

    fun markOpened(context: Context) {
        try {
            val file = getStateFile(context)
            val json = JSONObject().apply {
                put("opened", true)
                put("timestamp", System.currentTimeMillis())
            }
            file.writeText(json.toString())
            Log.i(TAG, "已写入WebView状态文件: ${file.absolutePath}")
        } catch (e: Exception) {
            Log.e(TAG, "写入WebView状态文件失败", e)
        }
    }
}


