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

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.core.content.res.ResourcesCompat
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(
    private var tagName: String = "",
    var isInitMenu: Boolean
) : BaseFragment<FragmentPasilleroBinding, PasilleroViewModel>(PasilleroViewModel::class) {

    //Tarea 5313
    constructor() : this("", false)

    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)


        if (getString(R.string.main) == tagName) {
            if (getData(WORKFORMSELECTED) != "Producción") {
                binding.mainToolbarDesign.toolbarTitle.setBackgroundColor(Color.RED)
                binding.mainToolbarDesign.toolbarTitle.text =
                    String.format(getString(R.string.mode, getData(WORKFORMSELECTED)))
                binding.mainToolbarDesign.backButton.visibility = View.GONE
            } else {
                binding.mainToolbarDesign.imageViewMenu.visibility = View.VISIBLE
                binding.mainToolbarDesign.backButton.visibility = View.GONE
            }
        } else {
            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()
            getString(R.string.titleDelivery) -> viewModel.inititializeDefaultDelivery()
            getString(R.string.titlePrevious) -> viewModel.inititializeDefaultPrevia()
            getString(R.string.titleControllers) -> viewModel.inititializeDefaultChecker()
            getString(R.string.titleOther) -> viewModel.inititializeDefaultOther()
            getString(R.string.historicals) -> viewModel.inititializeDefaultHistorical()

            else -> {
                //Tarea 5313
                viewModel.inititializeDefaultDataInit()
            }
        }


        super.onCreate(savedInstanceState)
    }

    override fun observeViewModel() {
        with(viewModel) {

            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(

                    ResourcesCompat.getDrawable(
                        requireContext().resources,
                        R.drawable.list_divider_items,
                        requireContext().theme
                    )!!
                )
                binding.pasillerosItems.addItemDecoration(dividerItemDecoration)
            }
        }


        super.observeViewModel()

    }

}
