package es.verdnatura.presentation.view.feature.pasillero.fragment

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentPasilleroBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.pasillero.adapter.PasillerosAdapter


class PasilleroFragment(
    var tagName: String = "",
    var isInitMenu: Boolean
) : BaseFragment<FragmentPasilleroBinding, PasilleroViewModel>(PasilleroViewModel::class) {

    private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null

    companion object {
        fun newInstance(tagName: String, isInitMenu: Boolean) =
            PasilleroFragment(tagName, isInitMenu = isInitMenu)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnPasillerosItemClickListener) pasillerosItemClickListener = context
    }

    override fun getLayoutId(): Int = R.layout.fragment_pasillero

    override fun init() {
        ma.hideBottomNavigation(View.VISIBLE)

        when (tagName) {
            getString(R.string.main)-> {
                binding.mainToolbarDesign.imageViewMenu.visibility = View.VISIBLE
                binding.mainToolbarDesign.backButton.visibility = View.GONE

            }

            tagName -> {
                if (tagName == getString(R.string.titleCorridors)) {
                    binding.mainToolbarDesign.backButton.visibility = View.GONE
                }
                binding.mainToolbarDesign.backButton.visibility = View.VISIBLE
                binding.mainToolbarDesign.toolbarTitle.text = tagName
                binding.mainToolbarDesign.imageViewMenu.visibility = View.GONE
            }
        }


        binding.mainToolbarDesign.backButton.setOnClickListener {

            requireActivity().onBackPressed()
        }




        super.init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {


        when (tagName) {
            getString(R.string.titleCorridors) -> { viewModel.inititializeDefaultData() }
            getString(R.string.titlePalletizers) -> viewModel.inititializeDefaultPallet()
            getString(R.string.titleBufferManegement) -> viewModel.inititializeDefaultBuffer()
            getString(R.string.main) -> viewModel.inititializeDefaultDataInit()
            getString(R.string.titleClaims) -> viewModel.inititializeDefaultClaim()

        }


        super.onCreate(savedInstanceState)
    }

    override fun observeViewModel() {
        with(viewModel) {

            /*  val mLayoutManager: RecyclerView.LayoutManager = GridLayoutManager(
                  activity,
                  requireActivity()!!.resources.getInteger(R.integer.number_of_grid_items)
              )*/

            binding.pasillerosItems.adapter =
                PasillerosAdapter(pasillerositem, pasillerosItemClickListener!!, isInitMenu)
            binding.pasillerosItems.layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)


            if (!isInitMenu) {
                val dividerItemDecoration = DividerItemDecoration(
                    context,
                    LinearLayoutManager.VERTICAL
                )
                dividerItemDecoration.setDrawable(requireContext().resources.getDrawable(R.drawable.list_divider_items,requireContext().theme))
                binding.pasillerosItems.addItemDecoration(dividerItemDecoration)
            }
        }


        super.observeViewModel()

    }

}
