package com.hx.utils.application.ui.home

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.hx.utils.application.R
import com.hx.utils.application.base.BaseAdapter
import com.hx.utils.application.base.BaseFragment
import com.hx.utils.application.databinding.FragmentHomeBinding
import com.hx.utils.application.databinding.ItemMenuBinding
import com.hx.utils.application.view.ModalBottomSheet

class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
    private var fyOrderAdapter: BaseAdapter<String, ItemMenuBinding>? = null

    override fun setupViewModel() {
        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]

    }

    override fun setupView() {
        viewModel.items.observe(viewLifecycleOwner) {
            initList(it)
        }
    }

    override fun setupBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(inflater, container, false)
    }


    private fun initList(newList: MutableList<String>) {
        fyOrderAdapter?.let {
            it.setList(newList)
            binding.menuList.layoutManager = GridLayoutManager(activity, 2)
            binding.menuList.adapter = fyOrderAdapter
        } ?: run {
            fyOrderAdapter = object : BaseAdapter<String, ItemMenuBinding>() {
                override fun onCreateView(parent: ViewGroup?): ItemMenuBinding {
                    return ItemMenuBinding.inflate(layoutInflater, parent, false)
                }

                override fun onBindView(bind: ItemMenuBinding, data: String, position: Int) {
                    bind.name.text = data
                    viewModel.itemsImg.value?.get(position)
                        ?.let { bind.name.setCompoundDrawablesWithIntrinsicBounds(0, it, 0, 0) }
                }
            }.also {
                it.setList(newList)
                it.setOnItemClickListener { _, _, position ->
                    val item = it.getItemByPosition(position) as String
                    val bundle = bundleOf("data" to item)
                    when (position) {
                        0 -> {
                            findNavController(
                                requireActivity(),
                                R.id.nav_host_fragment_activity_main
                            ).navigate(R.id.navigation_web, bundle)
                        }

                        1 -> {
                            findNavController(
                                requireActivity(),
                                R.id.nav_host_fragment_activity_main
                            ).navigate(R.id.navigation_sms, bundle)
                        }

                        2 -> {
                            val bottomSheetFragment = ModalBottomSheet()
                            bottomSheetFragment.show(
                                requireActivity().supportFragmentManager,
                                bottomSheetFragment.tag
                            )
                        }

                        3 -> {
                            ToastUtils.showLong("还没有弄")
                        }

                        4 -> {
                            findNavController(
                                requireActivity(),
                                R.id.nav_host_fragment_activity_main
                            ).navigate(R.id.navigation_face, bundle)
                        }


                        else -> {
                            ToastUtils.showLong(item)
                        }
                    }
                }
                binding.menuList.layoutManager = GridLayoutManager(activity, 2)
                binding.menuList.adapter = it
            }
        }

    }


}