package com.wall.photography.wallpaper

import android.app.Application
import android.util.Log
import com.wall.photography.wallpaper.data.Bean
import com.wall.photography.wallpaper.manager.DealData
import com.wall.photography.wallpaper.room.MyDataBase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MyApp : Application() {
    companion object {
        lateinit var application: Application
        const val TAG = "-------------tt"
        const val ROOM_NAME = "Photography_Wallpaper"
        const val ROOM_Version = 1
        const val TABLE_NAME = "wall_paper"
        var data_Wallpaper: List<Bean>? = null
        var data_animals: List<Bean>? = null
        var data_experimental: List<Bean>? = null
        var data_film: List<Bean>? = null
        var data_nature: List<Bean>? = null
        var data_streetphotography: List<Bean>? = null
        var data_texturespatterns: List<Bean>? = null
        var data_travel: List<Bean>? = null
        var dataAlready: Boolean = false
        const val pageSize = 10

        lateinit var strings: Array<String>

    }

    override fun onCreate() {
        super.onCreate()
        application = this
        Log.d(TAG, "---------------onCreate")

        CoroutineScope(Dispatchers.IO).launch {
            Log.d(TAG, "---------------协程开始" + Thread.currentThread().name)
            strings = resources.getStringArray(R.array.category_name)
            val allWallpaper = MyDataBase.getInstance().getBeanDao().getCategoryCovert(strings[7])
            if (allWallpaper != null) {
                Log.d(TAG, "---------------协程结束  --" + Thread.currentThread().name)
                return@launch
            }
            for (i in 0 until 8) {
                val initData = initData(strings[i] + ".json")
                val subList = initData.subList(0, 500)
                for (data in subList) {
                    MyDataBase.getInstance().getBeanDao().insertBean(data)
                }

            }
            dataAlready = true
            Log.d(TAG, "---------------协程结束  --" + Thread.currentThread().name)
        }
        Log.d(TAG, "---------------onCreate end")
    }

    private fun initData(fileName: String): List<Bean> {
        Log.d(TAG, "-----------协程.......----fileName=$fileName")
        val fdInput = assets.open(fileName)
        val jsonStr = DealData.getJsonString(fdInput)
        val cateName = fileName.substringBefore(".json")
        val bean = DealData.getBean(this, jsonStr, cateName)
        return bean
    }


}