package com.develop.main.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup.LayoutParams
import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.base.ext.navigateTo
import com.develop.common.data_repo.FoodDataProvider
import com.develop.base.util.MMkvUtils
import com.develop.common.data_repo.db.DataFactory
import com.develop.common.data_repo.db.RecipesType
import com.develop.common.dialog.LoadingDialog
import com.develop.common.router.Screens
import com.develop.common.tag.*
import com.develop.common.ui.CommonBindingActivity
import com.develop.common.utils.AppVersionUtil
import com.develop.common.widget.EntranceData
import com.develop.common.widget.EntranceItemView
import com.develop.common.widget.EntranceType
import com.develop.main.R
import com.develop.main.databinding.ActivityModeEntranceBinding
import com.drake.brv.utils.grid
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.smarx.notchlib.utils.ScreenUtil

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

    private val loadingDialog by lazy {
        LoadingDialog()
    }

    var prepareDataLiveData = MutableLiveData<Boolean>()
    override fun createViewBinding(inflater: LayoutInflater): ActivityModeEntranceBinding {
        return ActivityModeEntranceBinding.inflate(inflater)
    }

    private var title = ""
    private var isShowLoading = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MMkvUtils.save(FIRST_IN, true)
        prepareDataLiveData = FoodDataProvider.getPrepareLiveData() as MutableLiveData<Boolean>
        prepareDataLiveData.observe(this) {
            loadingDialog.removeSelf()
            if (FoodDataProvider.checkDataExit() && isShowLoading) {
                isShowLoading = false
                val bundle = Bundle()
                bundle.putInt(MODE_ENTRANCE, Recipes)
                bundle.putInt(RECIPES_TYPE, LocalFood)
                bundle.putString(HOME_TITLE, title)
                navigateTo(Screens.Main.HOME) {
                    with(bundle)
                }
                finish()
            }
        }
        binding.entranceRecycle.apply {
            grid(2)
            setup {
                addType<EntranceData>(R.layout.item_entrance)
                onBind {
                    val itemView = findView<EntranceItemView>(R.id.entrance_item)
                    val params = itemView.layoutParams as LayoutParams
                    params.height = (ScreenUtil.getScreenSize(this@ModeEntranceActivity)[1] / 2)
                    itemView.layoutParams = params
                    itemView.setEntranceData(getModel())

                }
                R.id.entrance_item.onClick {
                    val clickModel = getModel<EntranceData>()
                    title = getString(clickModel.title2StrId)
                    when (clickModel.type) {
                        EntranceType.automatic_recipes -> {
                            if (FoodDataProvider.checkDataExit()) {
                                val bundle = Bundle()
                                bundle.putInt(MODE_ENTRANCE, Recipes)
                                bundle.putInt(RECIPES_TYPE, LocalFood)
                                bundle.putString(HOME_TITLE, title)
                                navigateTo(Screens.Main.HOME) {
                                    with(bundle)
                                }
                                finish()
                            } else {
                                showLoading()
                            }
                        }
                        EntranceType.automatic_programmes -> {
                            val bundle = Bundle()
                            bundle.putInt(MODE_ENTRANCE, Mode)
                            bundle.putString(HOME_TITLE, title)
                            navigateTo(Screens.Main.HOME) {
                                with(bundle)
                            }
                            finish()
                        }
                        EntranceType.manual_cooking -> {
                            navigateTo(Screens.Cook.COOK_MODES) {
                                val bundle = Bundle()
                                bundle.putString(MODE_TYPE, "ADAPTED_COOKING")
                                with(bundle)
                            }
                        }
                        EntranceType.download_new_recipes -> {
                            val bundle = Bundle()
                            bundle.putInt(MODE_ENTRANCE, Recipes)
                            bundle.putInt(RECIPES_TYPE, NetFood)
                            bundle.putString(HOME_TITLE, title)
                            navigateTo(Screens.Main.HOME) {
                                with(bundle)
                            }
                            finish()
                        }
                    }
                }
            }
        }.models = DataFactory.getEntranceData()
    }

    override fun onResume() {
        super.onResume()
        AppVersionUtil.checkRecipeUpdate(false)
    }


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

    private fun showLoading() {
        isShowLoading = true
        loadingDialog.loadingTitle = "数据加载中"
        loadingDialog.showDialog(supportFragmentManager, "Loading")
    }

}