package com.develop.main.init.ui

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.databinding.ViewDataBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.develop.food.base.common.*
import com.develop.food.base.data.DataFactory
import com.develop.food.base.data.FunType
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.NetWorkManager
import com.develop.food.base.utils.ResDimension
import com.develop.food.base.widgets.CommonPopupWindow
import com.develop.food.base.widgets.HomeFunctionTopBar
import com.develop.food.base.widgets.NewsLayout
import com.develop.main.R
import com.develop.main.databinding.ActivityHomeBinding
import com.develop.main.init.ui.viewmodel.HomeViewModel

@Route(path = Screens.Main.HOME)
class HomeActivity : BaseActivity<ActivityHomeBinding>(), NetWorkManager.OnSysChangeListener {
    private val recipesFragment by lazy {
        RecipesFragment()
    }
    private val modesFragment by lazy {
        ModesFragment()
    }

    private val homeViewModel by lazy {
        getViewModel(HomeViewModel::class.java)
    }


    private val newsPopupWindow by lazy {
        CommonPopupWindow.ViewBuilder<NewsLayout>()
            .width(LinearLayout.LayoutParams.MATCH_PARENT)
            .height(
                ResDimension(R.dimen.convert_1080px).toInt()
                        + ResDimension(R.dimen.convert_160px).toInt()
            )
            .outsideTouchable(true)
            .focusable(true)
            .alpha(0.5f)
            .clippingEnabled(false)
            .view(NewsLayout(this))
            .onShowBefore { popupWindow, view ->
                view.apply {
                    updateNewsData(DataFactory.genTestNewsList())
                    onNewsBackClickListener = object : NewsLayout.OnNewsNewsBackClickListener {
                        override fun onNewsBackClick() {
                            popupWindow.dismiss()
                        }
                    }
                }
            }
            .build<ViewDataBinding>(this)
    }

    private var modeEntrance = Recipes
    override fun createViewBinding(inflater: LayoutInflater): ActivityHomeBinding {
        return ActivityHomeBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initFragment(intent)
        binding.slideContainer.addSlideView(binding.rightSlide)
        binding.slideContainer.isCanSlide(true)
        initCallbackAndListener()
        NetWorkManager.get().addTimeChangeListener(this)

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        initFragment(intent, true)
    }


    private fun initFragment(intent: Intent?, isFromNew: Boolean = false) {
        modeEntrance = intent?.extras?.getInt(MODE_ENTRANCE) ?: Recipes
        if (modeEntrance == Recipes) {
            binding.functionTopBar.setCurrentFunType(FunType.LocalRecipes)
            if (isFromNew) {
                replaceFragment(R.id.fragment_container, recipesFragment)
            } else {
                addFragment(R.id.fragment_container, recipesFragment)
            }
        } else {
            binding.functionTopBar.setCurrentFunType(FunType.Modes)
            if (isFromNew) {
                replaceFragment(R.id.fragment_container, modesFragment)
            } else {
                addFragment(R.id.fragment_container, modesFragment)
            }
        }
    }

    private fun initCallbackAndListener() {
        binding.functionTopBar.onHomeFunctionClickListener =
            object : HomeFunctionTopBar.OnHomeFunctionClickListener {
                override fun onModeClick(funType: FunType) {
                    if (funType == FunType.LocalRecipes) {
                        replaceFragment(R.id.fragment_container, recipesFragment)
                        homeViewModel.recipesType = HomeViewModel.RecipesType.LOCAL
                        homeViewModel.refreshRecipesLiveData.postValue(true)
                    } else if (funType == FunType.Modes) {
                        replaceFragment(R.id.fragment_container, modesFragment)
                    }
                }

                override fun onNotifyClick() {
                    newsPopupWindow.showAtLocation(binding.functionTopBar, Gravity.TOP, 0, 0)

                }

                override fun onTabClick() {
                    binding.slideContainer.sliderShowWithAnim()
                }

                override fun onWifiClick() {
                    navigateTo(Screens.Setting.WIFI) {
                        val bundle = Bundle()
                        bundle.putBoolean(WIFI_FROM_MAIN_SETTING, true)
                        with(bundle)
                    }
                }

                override fun onUserClick() {
                    navigateTo(Screens.Auth.MEMBER) {
                        withBoolean("enter_from_home", true)
                    }
                }

            }

        binding.rightContentLayout.setOnClickListener {

        }

        binding.settingLayout.setOnClickListener {
            binding.slideContainer.sliderGoneWithAnim()
            navigateTo(Screens.Setting.MAIN_SETTING)
        }

        binding.favouriteLayout.setOnClickListener {
            binding.slideContainer.sliderGoneWithAnim()
            val bundle = Bundle()
            bundle.putString(FOOL_LIST_TITLE, getString(R.string.favourite_recipes))
            bundle.putInt(FOOD_LIST_TYPE, Favourite)
            navigateTo(Screens.Main.FOOD_LIST) {
                with(bundle)
            }
        }

        binding.historyLayout.setOnClickListener {
            binding.slideContainer.sliderGoneWithAnim()
            val bundle = Bundle()
            bundle.putString(FOOL_LIST_TITLE, getString(R.string.cook_history))
            bundle.putInt(FOOD_LIST_TYPE, History)
            navigateTo(Screens.Main.FOOD_LIST) {
                with(bundle)
            }
        }

        binding.policyLayout.setOnClickListener {
            binding.slideContainer.sliderGoneWithAnim()
            val bundle = Bundle()
            bundle.putBoolean(POLICY_FROM_MAIN_SETTING, true)
            navigateTo(Screens.Setting.POLICY) {
                with(bundle)
            }
        }

        homeViewModel.apply {
            netFoodLiveData.observe(this@HomeActivity) {
                binding.functionTopBar.updateNetFunType()
            }
        }
    }

    override fun onChange(manager: NetWorkManager?, type: Int) {
        if (type == NetWorkManager.SYS_CHANGE_NET_STATE) {
            NetWorkManager.get().showNetWorkView(binding.functionTopBar.getNetView())
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        NetWorkManager.get().stopTask()
    }
}