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.domain.ConstAndValues.MENUBYDEFAULTSELECTED
import es.verdnatura.domain.ConstAndValues.WORKFORMSELECTED
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.view.feature.pasillero.adapter.PasillerosAdapter
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO

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

    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) {

            val myWorkSelected: String = mobileApplication.dataStoreApp.readDataStoreKey(
                WORKFORMSELECTED
            )
            binding.mainToolbarDesign.layoutTool.visibility = View.GONE

            if (myWorkSelected != "Producción" && myWorkSelected != "") {
                binding.pasillerosItems.setBackgroundColor(Color.RED)
                binding.mainToolbarDesign.toolbarTitle.setBackgroundColor(Color.RED)
                binding.mainToolbarDesign.toolbarTitle.text =
                    String.format(getString(R.string.mode, myWorkSelected))
                binding.mainToolbarDesign.backButton.visibility = View.GONE
            } else {
                binding.mainToolbarDesign.imageViewMenu.visibility = View.VISIBLE
                binding.mainToolbarDesign.backButton.visibility = View.GONE
            }
        } else {
            val toolbarHeight = resources.getDimensionPixelSize(R.dimen.toolbar_height)
            binding.pasillerosItems.setPaddingRelative(
                binding.pasillerosItems.paddingStart,
                toolbarHeight,
                binding.pasillerosItems.paddingEnd,
                binding.pasillerosItems.paddingBottom
            )
            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 {
            ma.onMyBackPressed()
        }


        super.init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {

        val showMenu = mobileApplication.dataStoreApp.readDataStoreKey<String>(
            MENUBYDEFAULTSELECTED
        ).ifBlank { tagName }

        when (showMenu) {
            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.titleDeliveryTruck) -> viewModel.inititializeDefaultDeliveryTruck()
            getString(R.string.titlePrevious) -> viewModel.inititializeDefaultPrevia()
            getString(R.string.titleControllers) -> viewModel.inititializeDefaultChecker()
            getString(R.string.titleOther) -> viewModel.inititializeDefaultOther()
            getString(R.string.historicals) -> viewModel.inititializeDefaultHistorical()
            getString(R.string.titlePhotos) -> {
                viewModel.inititializeDefaultDataInit(
                    PasillerosItemVO(
                        R.drawable.photo_camera,
                        R.string.titlePhotos,
                        R.string.titlePhotos
                    )
                )
            }

            getString(R.string.titlePickers) -> {
                viewModel.inititializeDefaultDataInit(
                    PasillerosItemVO(
                        R.drawable.ic_picker_ui,
                        R.string.titlePickers,
                        R.string.titlePickersDescrip
                    )

                )
            }

            else -> {
                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()

    }

}
