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

import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.core.view.isVisible
import es.verdnatura.R
import es.verdnatura.databinding.FragmentGeneralBlackBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.component.CustomDialogInput
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: String = ""
    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)
        binding.splashProgress.visibility = View.GONE
        setEvents()
        setToolBar()
        showPalletScan()
        super.init()
    }

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

    private fun expeditionState_addByPallet(vPalletId: String) {

        customDialogList.dismiss()
        binding.splashProgress.visibility = View.VISIBLE

        viewModel.expeditionState_addByPallet(
            getData(USER),
            getData(PASSWORD),
            vPalletId,
            "STORED"
        )

    }

    private fun expeditionPallet_get(vPalletId: String) {

        binding.splashProgress.visibility = View.VISIBLE
        customDialogList.dismiss()

        viewModel.expeditionPallet_get(
            getData(USER),
            getData(PASSWORD),
            vPalletId,
        )

    }


    private fun setEvents() {

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

        }
        customDialogList.setOnDismissListener {
            if (!binding.splashProgress.isVisible)
                requireActivity().onBackPressed()
        }
    }

    override fun observeViewModel() {


        with(viewModel) {
            binding.splashProgress.visibility = View.GONE
            responseScan.observe(viewLifecycleOwner) {

                if (it.isError) {

                    ma.messageWithSound(
                        it.errorMessage,
                        true,
                        true,
                        isToasted = true
                    )
                    showPalletScan()

                } else {
                    binding.splashProgress.visibility = View.GONE
                    ma.messageWithSound(getString(R.string.palletScanned) + itemScaned, false, true)
                    showPalletScan()

                }
            }
            responsePalletGet.observe(viewLifecycleOwner) {
                binding.splashProgress.visibility = View.GONE
                if (it.isError) {

                    ma.messageWithSound(
                        it.errorMessage,
                        true,
                        true,
                        isToasted = true
                    )
                    showPalletScan()

                } else {
                    binding.splashProgress.visibility = View.GONE
                    ma.messageWithSound(getString(R.string.palletScanned) + itemScaned, false, true)

                    ma.onComprobarPalletFromPalletScan(it, ItemPalletVO(Pallet = itemScaned))

                }
            }
        }

    }

    private fun showPalletScan() {

        //customDialogList.setCancelable(false)

        when (action) {
            "receive" -> {
                customDialogList.setTitle(getString(R.string.palletScan))

            }
            "get" -> {
                customDialogList.setTitle(getString(R.string.palletScanAdd))
            }
            else -> {}
        }


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

            ma.hideKeyboard(customDialogList.getEditText())
            customDialogList.dismiss()


        }.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 || actionId == 6) {

                if (customDialogList.getValue().isNotEmpty()
                ) {
                    itemScaned = customDialogList.getValue()
                    ma.hideKeyboard(customDialogList.getEditText())

                    when (action) {
                        "receive" -> expeditionState_addByPallet(
                            customDialogList.getValue()
                        )
                        "get" -> expeditionPallet_get(customDialogList.getValue())
                    }
                    customDialogList.setValue("")
                }

                return@setOnEditorActionListener true
            }
            false
        }
    }

}

