package com.develop.main.init.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
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.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.main.databinding.ActivityModeEntranceBinding

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

    private var loadingDialog: LoadingDialog = LoadingDialog()

    override fun createViewBinding(inflater: LayoutInflater): ActivityModeEntranceBinding {
        return ActivityModeEntranceBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MmkvUtils.save("isFirstIn", true)
        binding.modeLayout.setOnClickListener {
            val bundle = Bundle()
            bundle.putInt(MODE_ENTRANCE, Mode)
            navigateTo(Screens.Main.HOME) {
                with(bundle)
            }
            finish()
        }
        binding.recipesLayout.setOnClickListener {
            val bundle = Bundle()
            bundle.putInt(MODE_ENTRANCE, Recipes)
            navigateTo(Screens.Main.HOME) {
                with(bundle)
            }
            finish()
        }
        if (FoodDataProvider.getPrepareLiveData().value != true) {
            showLoadingDialog()
        }
    }

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

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