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

import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.Log.d
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentPackagingCountBinding
import es.verdnatura.domain.ConstAndValues.ENTRYTYPE
import es.verdnatura.domain.ConstAndValues.SUPPLIERNAME
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.*
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.component.CustomDialogInput
import es.verdnatura.presentation.view.feature.packaging.adapter.ItemSupplierAdapter
import es.verdnatura.presentation.view.feature.packaging.fragment.PackagingViewModel.PackagingViewModel
import es.verdnatura.presentation.view.feature.packaging.model.ItemSupplier
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class PackagingCountFragment(
    var entryPoint: String = ""

) : BaseFragment<FragmentPackagingCountBinding, PackagingViewModel>(
    PackagingViewModel::class
) {

    private var adapteritemsupplier: ItemSupplierAdapter? = null
    private lateinit var customDialogInput: CustomDialogInput
    private var listItemSupplier: MutableList<ItemSupplier> = ArrayList()
    private lateinit var customDialog: CustomDialog
    private var layoutManager: LinearLayoutManager? = null

    companion object {
        fun newInstance(entryPoint: String) = PackagingCountFragment(entryPoint)
    }

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

    override fun onCreate(savedInstanceState: Bundle?) {

        /* viewModel.supplier_getItemsSalix(
             2, 1
         )*/

        super.onCreate(savedInstanceState)
    }

    override fun init() {
        binding.quantityPackaging.setOnClickListener {
            ma.onPasillerosItemClickListener(
                PasillerosItemVO(title = getString(R.string.titlePackagingObs)),
                getString(R.string.titlePackagingObs)
            )
        }
        customDialogInput = CustomDialogInput(requireContext())
        customDialog = CustomDialog(requireContext())
        ma.hideBottomNavigation(View.GONE)
        setToolBar()
        setEvents()
        super.init()
    }

    private fun setToolBar() {

        binding.mainToolbar.toolbarTitle.text =
            mobileApplication.dataStoreApp.readDataStoreKey<String>(ENTRYTYPE) + ":" + mobileApplication.dataStoreApp.readDataStoreKey(
                SUPPLIERNAME
            )

        val listIcons: ArrayList<ImageView> = ArrayList()
        val iconNext = ImageView(context)
        iconNext.setImageResource(R.drawable.ic_chevron_right_black_24dp)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            iconNext.tooltipText = getString(R.string.entryNextObsers)
        }
        listIcons.add(iconNext)
        binding.mainToolbar.toolbarIcons.adapter =
            ToolBarAdapterTooltip(listIcons, object : OnOptionsSelectedListener {
                override fun onOptionsItemSelected(item: Drawable) {
                    when (item) {
                        iconNext.drawable -> {
                            ma.onPasillerosItemClickListener(
                                PasillerosItemVO(title = getString(R.string.titlePackagingObs)),
                                getString(R.string.titlePackagingObs)
                            )
                        }

                    }

                }

            })
        binding.mainToolbar.toolbarIcons.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)

    }

    private fun setEvents() {

        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

    }

    override fun observeViewModel() {
        with(viewModel) {

            itemSupplierList.observe(viewLifecycleOwner) {
                adapteritemsupplier =
                    ItemSupplierAdapter(it.list, object : OnImageUpdateClickListener {

                        override fun OnImageUpdateClickListener(item: Any) {
                            d("VERDNATURA::", "Seleccionado update" + (item as ItemSupplier).name)
                        }

                    }, object : OnImageTrashClickListener {
                        override fun OnImageUpdateClickListener(item: Any) {
                            d("VERDNATURA::", "Seleccionado trash" + (item as ItemSupplier).name)
                        }

                    }, object : OnEditSubQuantityListener {
                        override fun OnEditSubQuantityListener(item: Any, text: String) {
                            d(
                                "VERDNATURA::",
                                "la subquantity es " + (item as ItemSupplier).name + "el valor es :" + text
                            )
                            hideKeyboard()
                        }
                    })

                binding.itemsupplierRecyclerview.adapter = adapteritemsupplier
                layoutManager =
                    LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
                binding.itemsupplierRecyclerview.layoutManager = layoutManager

            }

        }
    }

}

