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.common.MODE_ENTRANCE
import com.develop.food.base.common.Mode
import com.develop.food.base.common.Recipes
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.router.Screens
import com.develop.food.base.router.navigateTo
import com.develop.food.base.ui.BaseActivity
import com.develop.food.base.utils.MmkvUtils
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

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

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

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MmkvUtils.save("isFirstIn", true)
        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))
        }
        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!!!!!")
        }
    }

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

    override fun onDestroy() {
        super.onDestroy()
        loadingDialog.removeSelf()
        wifiHelp.destroy()
    }
    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()
    }
}