package com.sunny.app.soft.timberkeyboardnew.data

import android.util.Log
import com.google.gson.Gson
import com.sunny.app.soft.timberkeyboardnew.App
import com.sunny.app.soft.timberkeyboardnew.data.entity.BackgroundEntity
import com.sunny.app.soft.timberkeyboardnew.data.entity.CategoryEntity
import com.sunny.app.soft.timberkeyboardnew.room.MyDatabase
import org.json.JSONObject
import java.io.InputStream
import java.io.InputStreamReader

object DataManager {
    private fun parseJsonFile(jsonInputStream: InputStream) {
        val reader = InputStreamReader(jsonInputStream)
        val jsonString = reader.readText()

        val jsonObject = JSONObject(jsonString)
        val categoryName = jsonObject.getString("name")
        val covert = jsonObject.getString("coverPath")

        val cateId = MyDatabase.myDatabase.CategoryEntityDao()
            .insertData(CategoryEntity(name = categoryName, coverPath = covert))

        Log.d(App.TAG, " categoryName=${categoryName} cateId=${cateId}")
        val jsonArray = jsonObject.getJSONArray("backgrounds")
        for (i in 0 until jsonArray.length()) {
            val json1 = jsonArray.getJSONObject(i)
            val keyboardCovert = json1.getString("coverPath")
            val zipPath = json1.getString("contentPath")
            val skinNumber = json1.getInt("skinNumber")

            MyDatabase.myDatabase.BackgroundEntityDao()
                .insertData(
                    BackgroundEntity(
                        categoryId = cateId,
                        coverPath = keyboardCovert,
                        contentPath = zipPath,
                        skinNumber = skinNumber,
                        like = false
                    )
                )
        }
//        val gson = Gson()
//        val json = gson.fromJson(jsonString, JsonObject::class.java)
//        val entity: CategoryEntity = gson.fromJson(json.toString(), CategoryEntity::class.java)

    }

    fun getCategoryEntity(uil: String) {
        return parseJsonFile(App.appContext.assets.open(uil))
    }

    fun getModelList(): List<CategoryEntity> {
        val modelList = mutableListOf<CategoryEntity>()



        return modelList
    }

}