package com.develop.main.init.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.food.base.api.response.RecipeDataConfig
import com.develop.food.base.common.CURRENT_LANGUAGE
import com.develop.food.base.common.LANGUAGE_SETTING
import com.develop.food.base.common.MODE_ENTRANCE
import com.develop.food.base.common.Mode
import com.develop.food.base.common.Recipes
import com.develop.food.base.data.DataFactory
import com.develop.food.base.manager.wifi.WifiData
import com.develop.food.base.manager.wifi.WifiHelp
import com.develop.food.base.repo.FoodDataProvider
import com.develop.food.base.repo.entity.DevRecipeCookingStep
import com.develop.food.base.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.AppVerUtils
import com.develop.food.base.utils.MmkvUtils
import com.develop.food.base.utils.SerialportLibUtils
import com.develop.food.base.utils.SettingKeyBoardUtils
import com.develop.food.base.utils.isNightTheme
import com.develop.food.base.utils.resId2Dimension
import com.develop.main.R
import com.develop.main.databinding.ActivityModeEntranceBinding
import com.google.gson.Gson
import java.util.Arrays

@Route(path = Screens.Main.ENTRANCE_CHOSEN)
class ModeEntranceActivity : BaseActivity<ActivityModeEntranceBinding>() {

    private var loadingDialog: LoadingDialog = LoadingDialog()
    private var isShowLoading = false
    override fun createViewBinding(inflater: LayoutInflater): ActivityModeEntranceBinding {
        return ActivityModeEntranceBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MmkvUtils.save("isFirstIn", true)

        //设置系统语言
        addListLocale()

//        val wifiData = MmkvUtils.getString("connectedWifi")
//        wifiHelp = WifiHelp.Build(this).build()
//        if (!wifiData.isNullOrEmpty() && wifiHelp.getConnectionWifi() == null) {
//            wifiHelp.setWifiEnabled(true)
//            wifiHelp.connect(Gson().fromJson(wifiData, WifiData::class.java), false)
//        }


        binding.modeLayout.setOnClickListener {
            if (FoodDataProvider.checkDataExit()) {
                val bundle = Bundle()
                bundle.putInt(MODE_ENTRANCE, Mode)
                navigateTo(Screens.Main.HOME) {
                    with(bundle)
                }
                finish()
            } else {
                showLoading()
            }

        }
        binding.recipesLayout.setOnClickListener {
            if (FoodDataProvider.checkDataExit()) {
                val bundle = Bundle()
                bundle.putInt(MODE_ENTRANCE, Recipes)
                navigateTo(Screens.Main.HOME) {
                    with(bundle)
                }
                finish()
            } else {
                showLoading()
            }
        }
        if (FoodDataProvider.getPrepareLiveData().value != true) {
            showLoadingDialog()
        }
        if (isNightTheme()) {
            val recipesParam = binding.ivRecipes.layoutParams as RelativeLayout.LayoutParams
            recipesParam.width = dp88
            recipesParam.height = dp75
            binding.ivRecipes.layoutParams = recipesParam

            val modeParam = binding.ivMode.layoutParams as RelativeLayout.LayoutParams
            modeParam.width = dp87
            modeParam.height = dp84
            binding.ivMode.layoutParams = modeParam
        }
    }

    private fun showLoadingDialog() {
        loadingDialog.showDialog(supportFragmentManager, "Loading")
        FoodDataProvider.getPrepareLiveData().observe(this) {
            loadingDialog.removeSelf()
            Log.d("QQQQ", "dismiss!!!!!")
        }
    }

    override fun onResume() {
        super.onResume()
        AppVerUtils.checkRecipeUpdate()
    }

    private fun showLoading() {
        isShowLoading = true
        loadingDialog.showDialog(supportFragmentManager, "Loading")
    }

    override fun onDestroy() {
        super.onDestroy()
        loadingDialog.removeSelf()
    }

    fun addListLocale() {
        //判断是否设置语言，如果重新设置需要重新写入系统语言，将选中放第一位
        if (MmkvUtils.getBool(LANGUAGE_SETTING, false)) {
            //设置语言
            var list = DataFactory.createLanguageDataV2(resources)
            var listStr = mutableListOf<String>()
            listStr.add("en")
            for (i in 1..list.size) {
                if (list[i - 1].value.lowercase() != "en") {
                    listStr.add(list[i - 1].value.lowercase())
                }
            }

            //调试键盘问题，但没用
            val condition: (String) -> Boolean = {
                it == (MmkvUtils.getString(CURRENT_LANGUAGE)?.lowercase() ?: "en")
            }
            firstLocale(listStr, condition)
            //获取配置语言，然后设置系统语言，首语言默认英语
            SerialportLibUtils.getAddLocale(listStr)

            MmkvUtils.save(LANGUAGE_SETTING, false)

        }
    }

    fun firstLocale(list: MutableList<String>, condition: (String) -> Boolean) {
        val index = list.indexOfFirst(condition)
        if (index >= 0) {
            val element = list.removeAt(index)
            list.add(0, element)
        }
    }


    companion object {
        var dp88 = (R.dimen.convert_88px).resId2Dimension().toInt()
        var dp87 = (R.dimen.convert_87px).resId2Dimension().toInt()
        var dp84 = (R.dimen.convert_84px).resId2Dimension().toInt()
        var dp75 = (R.dimen.convert_75px).resId2Dimension().toInt()
    }
}