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

import android.text.InputType
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

//Tarea #4940
class PackingHollandFragment(var entrypoint: String) :
    BaseFragment<FragmentGeneralBlackBinding, PackingHollandViewModel>(
        PackingHollandViewModel::class
    ) {

    override fun getLayoutId(): Int = R.layout.fragment_general_black
    private var shelvingScaned: Int = 0
    private lateinit var customDialogInput: CustomDialogInput

    companion object {
        fun newInstance(entrypoint: String) = PackingHollandFragment(entrypoint = entrypoint)
    }

    override fun init() {

        customDialogInput = CustomDialogInput(requireContext())
        binding.mainToolbar.toolbarTitle.text = entrypoint
        binding.splashProgress.visibility = GONE
        binding.scanInput.visibility = VISIBLE
        setEvents()

        super.init()
    }

    private fun setEvents() {

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

        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.scanInput.text.isNullOrEmpty()) {

                    try {
                        shelvingScaned = itemScanValue(
                            binding.scanInput.text.toString(),
                            "buy",
                            "more"
                        ).toString().toInt()
                        showQuantityPacking()
                    } catch (ex: Exception) {
                        ex.message!!.toast(requireActivity())
                    }

                }

                binding.scanInput.setText("")
                ma.hideKeyboard(binding.scanInput)
                return@setOnEditorActionListener true
            }
            false
        }
    }

    override fun observeViewModel() {
        with(viewModel) {
            response.observe(viewLifecycleOwner) {
                binding.splashProgress.visibility = GONE
                if (it.isError) {
                    ma.messageWithSound(it.errorMessage, true, true)
                } else {
                    ma.messageWithSound(it.errorMessage, false, true, isToasted = false)
                }

            }

        }
    }


    private fun showQuantityPacking() {

        customDialogInput.setInputText(InputType.TYPE_CLASS_NUMBER)
        customDialogInput.setTitle(getString(R.string.titlePackingHolland))
        customDialogInput.setDescription(getString(R.string.packingQuantity) + shelvingScaned)
            .setValue("")
            .setOkButton(getString(R.string.modify)) {
                callPacking(customDialogInput.getValue())
            }.setKoButton(getString(R.string.cancel)) {
                customDialogInput.dismiss()
            }.show()
        customDialogInput.setFocusText()
        ma.hideKeyboard(customDialogInput.getEditText())

        customDialogInput.getEditText().setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5 || actionId == 6) {

                callPacking(customDialogInput.getValue())

                return@setOnEditorActionListener true
            }
            false
        }

        ma.hideKeyboard(customDialogInput.getEditText())
    }


    private fun callPacking(value: String) {
        if (customDialogInput.getValue().isNotEmpty()) {

            try {

                ma.hideKeyboard(customDialogInput.getEditText())
                customDialogInput.dismiss()
                binding.splashProgress.visibility = VISIBLE
                viewModel.travel_updatePacking(
                    itemFk = shelvingScaned,
                    packingFk = value.toInt()
                )


            } catch (ex: Exception) {
                binding.splashProgress.visibility = INVISIBLE
                getString(R.string.errorInput).toast(requireActivity())
                customDialogInput.setValue("")
            }

        }
    }
}


