package com.develop.common.utils

import android.content.Context
import com.develop.base.ext.GlobalApp
import com.develop.common.bean.CfConfig
import com.develop.common.bean.WorkMode
import com.google.gson.Gson
import org.json.JSONObject
import java.io.IOException
import java.nio.charset.Charset

object ConfigUtils {


    fun loadConfig(): CfConfig? {
        return try {
            val inputStream = GlobalApp().assets.open("config.json")
            val size = inputStream.available()
            val buffer = ByteArray(size)
            inputStream.read(buffer)
            inputStream.close()

            val jsonString = String(buffer, Charset.defaultCharset())
            val gson = Gson()
            val cfConfig = gson.fromJson(jsonString, CfConfig::class.java)
            cfConfig;
        } catch (ex: IOException) {
            ex.printStackTrace()
            null
        }
    }

    fun getMode(type: String): WorkMode? {
        return loadConfig()?.workModes?.find { it.type == type }?.copy()
    }

}