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

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentItemdayofsaleCardBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnOptionsSelectedListener
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.ToolBarAdapterTooltip
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.component.CustomDialogList
import es.verdnatura.presentation.view.feature.diadeventa.adapter.DayOfSaleAdapter

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class DayOfSaleFragment(
    var entryPoint: String = ""
) : BaseFragment<FragmentItemdayofsaleCardBinding, DayOfSaleViewModel>(DayOfSaleViewModel::class) {

    private lateinit var customDialogList: CustomDialogList
    private lateinit var customDialog: CustomDialog
    private var adapter: DayOfSaleAdapter? = null
    private var itemScan = ""
    private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null

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

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

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

    override fun init() {

        customDialog = CustomDialog(requireContext())
        customDialogList = CustomDialogList(requireContext())
        setEvents()
        setToolBar(entryPoint)
        super.init()

    }

    private fun setToolBar(title: String) {
        binding.mainToolbar.toolbarTitle.text = title
        val listIcons: ArrayList<ImageView> = ArrayList()
        val iconParking = ImageView(context)
        iconParking.setImageResource(R.drawable.ic_local_parking_black_24dp)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            iconParking.tooltipText = getTooltip(R.drawable.ic_local_parking_black_24dp)
        }

        listIcons.add(iconParking)
        binding.mainToolbar.toolbarIcons.adapter =
            ToolBarAdapterTooltip(listIcons, object : OnOptionsSelectedListener {
                override fun onOptionsItemSelected(item: Drawable) {

                    if (item == iconParking.drawable) {
                        showParking()

                    }

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

    }

    private fun setEvents() {
        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.scanInput.text.toString().isNullOrEmpty()) {
                    viewModel.itemShelvingGetSaleDate(binding.scanInput.text.toString())
                    binding.mainToolbar.toolbarTitle.text =
                        entryPoint + ":" + binding.scanInput.text.toString().uppercase()
                }
                binding.scanInput.setText("")
                ma.hideKeyboard(binding.scanInput)
                return@setOnEditorActionListener true
            }
            false
        }

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

    }

    override fun observeViewModel() {
        with(viewModel) {

            itemShelvingSaleDateList.observe(viewLifecycleOwner) {
                if (it.list.isEmpty()) {
                    customDialog.setTitle(getString(R.string.noResults))
                        .setDescription(getString(R.string.wagoonNoSaleToday))
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()

                }
                adapter = DayOfSaleAdapter(
                    it.list,
                    onPasillerosItemClickListener = pasillerosItemClickListener!!
                )
                binding.itemexpeditionstateRecyclerview.adapter = adapter
                binding.itemexpeditionstateRecyclerview.layoutManager =
                    LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)

            }

            response.observe(viewLifecycleOwner) {
                if (it.isError) {
                    ma.messageWithSound(it.errorMessage, true, false)

                } else {
                    customDialog.setTitle(getString(R.string.info))
                        .setDescription(getString(R.string.parkingOk))
                        .setOkButton(getString(R.string.close)) {
                            customDialog.dismiss()
                        }.show()

                }

            }

        }
    }

    private fun showParking() {

        customDialogList.setTitle(getString(R.string.showParking))
            .setOkButton(getString(R.string.close)) {

                ma.hideKeyboard(customDialogList.getEditText())

                customDialogList.dismiss()
            }.setValue("").show()

        customDialogList.getEditText().requestFocus()
        ma.hideKeyboard(customDialogList.getEditText())
        customDialogList.getEditText().setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (customDialogList.getValue().isNotEmpty()) {
                    viewModel.setParking(
                        parking = customDialogList.getValue(),
                        scanItem = itemScan
                    )
                    customDialogList.dismiss()
                }
                customDialogList.setValue("")
                ma.hideKeyboard(customDialogList.getEditText())
                return@setOnEditorActionListener true
            }
            false
        }

    }

}


