package com.adpro.pixelshow

import android.app.Application
import android.util.Log
import com.ad.maxlibrary.MaxInitManager
import com.adpro.pixelshow.data.AppDataBase
import com.adpro.pixelshow.data.MovieDao
import com.adpro.pixelshow.util.TypeUtil
import com.applovin.sdk.AppLovinSdk
import com.up.uploadlibrary.UpLoadManager

import java.io.IOException


class IAppLifecycle : Application() {
    private val animaLs = "Animals.json"
    private val experiment = "Experimental.json"
    private val film = "Film.json"
    private val nature = "Nature.json"
    private val patterns = "Patterns.json"
    private val street = "Street.json"
    private val travel = "Travel.json"
    private val wallpaper = "Featured.json"

    companion object {
        var classificationList: MutableList<String> = mutableListOf()
        var isDataInitialized = false
            private set
        var TAG = "IAppLifecycle"
    }

    override fun onCreate() {
        super.onCreate()
        UpLoadManager.init(this,TAG){ _, _ ->
            // 处理逻辑
            null
            Log.d(TAG, "upload success")
        }

        MaxInitManager.init(
            this,
            "Dd37BrtbLDlaeiDhxVzaDbsI67Mc1h5lAGIinzo4v2IbkpufdtVmT5Tag9O3aGexzkS4txEPigaEexktewANIk",
            "3cbf4bd957da0eb3",
            "768b5586c4ab6ad1",
            "a4dd3cb20fd8925d",
            "=============="
        )
        val database = AppDataBase.getDatabase(this)
        val paperDao = database.movieDao()
        Thread {
            initializeData(paperDao)
            isDataInitialized = true
        }.start()

    }

    private fun initializeData(paperDao: MovieDao) {
        val names = arrayOf(animaLs, travel, experiment, film, nature, patterns, street, wallpaper)
        val startTime = System.currentTimeMillis()

        for (name in names) {
            try {
                // 在后台线程读取文件
                val inputStream = assets.open(name)
                val jsonData = TypeUtil.getConvert(inputStream)
                Log.d("DataInit", "Loaded JSON for $name")

                if (jsonData.isNotEmpty()) {
                    val classificationName = name.substring(0, name.indexOf("."))

                    // 同步添加分类名称（线程安全）
                    synchronized(classificationList) {
                        classificationList.add(classificationName)
                    }

                    // 解析数据
                    val movies = TypeUtil.getData(jsonData, classificationName)

                    // 批量插入数据
                    if (movies.isNotEmpty()) {
                        // 使用批量插入方法
                        paperDao.insertAll(movies)
                        Log.d("DataInit", "[$classificationName] insert ${movies.size} data")
                    }
                }
            } catch (e: IOException) {
                Log.e("DataInit", " $name error", e)
            }
        }

        val duration = System.currentTimeMillis() - startTime
        Log.d("DataInit", "use time: ${duration}ms")
    }
}