package com.ytpm.hydtw.dao

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.io.IOException
import java.io.InputStream

data class IdiomStory(
    val title: String,
    val content: String,
    val remark: String
)

// JSON 读取工具类
object JsonReader {
    // 从 assets 读取 JSON 文件内容
    fun readJsonFromAssets(context: Context, fileName: String): String? {
        return try {
            val inputStream: InputStream = context.assets.open(fileName)
            val size: Int = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer)
            inputStream.close()
            String(buffer, Charsets.UTF_8)
        } catch (e: IOException) {
            e.printStackTrace()
            null
        }
    }

    // 解析 JSON 数据为成语故事列表
    fun parseIdiomStories(jsonString: String): List<IdiomStory> {
        val gson = Gson()
        val type = object : TypeToken<List<IdiomStory>>() {}.type
        return gson.fromJson(jsonString, type)
    }
}