package com.develop.common.utils


import com.develop.common.bean.CfConfig
import com.develop.common.bean.TuyaConfig
import com.develop.common.bean.WorkMode
import com.develop.food.base.utils.GlobalApp
import com.develop.food.base.utils.getSN
import com.google.gson.Gson
import java.io.IOException
import java.io.InputStream
import java.nio.charset.Charset

object ConfigUtils {


    data class Lang(val name: String?, val value: String, val icon: String?,val show:Boolean?,val showName:String)

    data class LangConfig(var langs: ArrayList<Lang>)


    fun loadConfig(): CfConfig? {
        return try {
            var sn = getSN()
            var brandNum = sn.substring(0,3)

            val fileList = GlobalApp().assets.list("") ?: arrayOf()
            var inputStream: InputStream? = null
            if(fileList.contains("config_${brandNum}.json")){
                inputStream = GlobalApp().assets.open("config_${brandNum}.json")
            }else{
                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 loadLangConfig(): LangConfig? {
        return try {

            var sn = getSN()
            var brandNum = sn.substring(0,3)

            val fileList = GlobalApp().assets.list("") ?: arrayOf()
            var inputStream: InputStream? = null
            if(fileList.contains("lang_config_${brandNum}.json")){
                inputStream = GlobalApp().assets.open("lang_config_${brandNum}.json")
            }else{
                inputStream = GlobalApp().assets.open("lang_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, LangConfig::class.java)
            return cfConfig
        } catch (ex: IOException) {
            ex.printStackTrace()
            null
        }
    }

    fun loadTuyaConfig(): Map<String,String>? {
        return try {
            var sn = getSN()

            var inputStream: InputStream? = null
            inputStream = GlobalApp().assets.open("tuya_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, TuyaConfig::class.java)
            cfConfig.licences.get(sn)
        } catch (ex: IOException) {
            ex.printStackTrace()
            null
        }
    }


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



}