package com.quiescent.wallpaper.collection.test

import android.app.Application
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.quiescent.wallpaper.collection.test.bean.Info
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.StringWriter

class WallpaperCollection : Application() {

      var list:MutableList<Info> = mutableListOf()

    companion object {
        lateinit var instance:WallpaperCollection
    }
    private  fun getJsonString(fileInputStream: InputStream): String {
        return try {
            val charArray = CharArray(fileInputStream.available())
            var readCount = 0
            val streamReader =
                InputStreamReader(fileInputStream)
            val bufferedReader = BufferedReader(streamReader)
            val stringWriter = StringWriter()
            while (bufferedReader.read(charArray).also { readCount = it } != -1) {
                stringWriter.write(charArray, 0, readCount)
            }
            stringWriter.toString()
        } catch (e: IOException) {
            ""
        }
    }
    override fun onCreate() {
        super.onCreate()
        instance = this
        val open = assets.open("wallpaper.json")
        val jsonStr =  getJsonString(open)
        list = Gson().fromJson(jsonStr, object : TypeToken<List<Info>>() {}.type)

    }





}