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

import android.view.View
import android.view.inputmethod.EditorInfo
import es.verdnatura.R
import es.verdnatura.databinding.FragmentGeneralBlackBinding
import es.verdnatura.domain.toast
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.itemScanValue
import es.verdnatura.presentation.view.component.CustomDialogInput
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO

class PalletScanFragment(
    var title: String = "", var action: String
) : BaseFragment<FragmentGeneralBlackBinding, PalletScanViewModel>(
    PalletScanViewModel::class
) {

    override fun getLayoutId(): Int = R.layout.fragment_general_black
    private lateinit var customDialogList: CustomDialogInput
    private var itemScaned: Int = 0
    private var isShowed = false

    companion object {
        fun newInstance(title: String, action: String) = PalletScanFragment(title, action)
    }

    override fun init() {

        customDialogList = CustomDialogInput(requireContext())
        ma.hideBottomNavigation(View.GONE)
        setEvents()
        setToolBar()
        showPalletScan()
        super.init()
    }

    private fun setToolBar() {
        binding.mainToolbar.toolbarTitle.text = title
    }

    private fun setEvents() {

        binding.mainToolbar.backButton.setOnClickListener {
            ma.onMyBackPressed()

        }

        customDialogList.setOnDismissListener {
            customDialogList.dismiss()
        }
    }

    override fun observeViewModel() {

        with(viewModel) {

            responsePalletGet.observe(viewLifecycleOwner) {
                if (it.truckFk == 0) {
                    ma.messageWithSound(
                        message = getString(R.string.palletNotFound),
                        isError = true,
                        isPlayed = true,
                        isToasted = false
                    )
                }else{

                    ma.onComprobarPalletFromPalletScan(
                        ItemExpeditionTruckVO(it.truckFk), ItemPalletVO(Pallet = itemScaned)
                    )
                    customDialogList.dismiss()
                }
            }
        }

    }

    private fun showPalletScan() {

        customDialogList.setTitle(
            if (action == "receive") {
                getString(R.string.palletScan)
            } else {
                getString(R.string.palletScanAdd)
            }
        )

        customDialogList.setOkButton(getString(R.string.end)) {

            customDialogList.dismiss()
            ma.onMyBackPressed()

        }.setValue("").show()

        customDialogList.setFocusText()
        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()) {
                    try {
                        itemScaned = itemScanValue(
                            customDialogList.getValue(), arrayOf("expeditionPallet"), "id"
                        ).toString().toInt()

                    } catch (ex: Exception) {
                        ex.message!!.toast(requireContext())
                    }

                    ma.hideKeyboard(customDialogList.getEditText())

                    when (action) {
                        "receive" -> {
                            viewModel.expeditionStateAddByPallet(
                                itemScaned, "STORED"
                            )
                            showPalletScan()
                        }

                        "get" -> viewModel.expeditionPalletGet(
                            itemScaned
                        )
                    }
                    customDialogList.setValue("")

                }

                return@setOnEditorActionListener true
            }
            false
        }
    }

}