package es.verdnatura.presentation.view.feature.category

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.packingHolland.fragment.PackingHollandViewModel


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

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

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

        override fun init() {

            customDialogInput = CustomDialogInput(requireContext())
            binding.mainToolbar.toolbarTitle.text = entrypoint
            binding.scanInput.hint = "Escanear item"
            binding.splashProgress.visibility = View.GONE
            binding.scanInput.visibility = View.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 {

                            binding.scanInput.setText(
                                itemScanValue(
                                    binding.scanInput.text.toString(),
                                    "buy",
                                    "more"
                                ).toString()
                            )
                            itemScaned = binding.scanInput.text.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 = View.GONE
                    if (it.isError) {
                        ma.messageWithSound(it.errorMessage, true, false)
                    } else {
                        showQuantityPacking()
                    }

                }

            }
        }


        private fun showQuantityPacking() {


            customDialogInput.setTitle(getString(R.string.titleChangeCategory))
            customDialogInput.setDescription(getString(R.string.ubication) + itemScaned)
                .setValue("")
                .setOkButton(getString(R.string.modify)) {

                    if (customDialogInput.getValue().isNotEmpty()
                    ) {
                        callPacking(customDialogInput.getValue())
                    }


                }.setKoButton(getString(R.string.cancel)) {
                    customDialogInput.dismiss()
                }.show()

            //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) {

                    if (customDialogInput.getValue().isNotEmpty()) {
                         //callPacking(customDialogInput.getValue())
                        getNewPrice(customDialogInput.getValue(), itemScaned, 5.51)
                    }

                    return@setOnEditorActionListener true
                }
                false
            }

            customDialogInput.setFocusText()
            ma.hideKeyboard(customDialogInput.getEditText())
        }

        private fun getNewPrice(shelving:String,item:Int,price:Double) {
            customDialogInput.setTitle(getString(R.string.titleChangeCategory))
            customDialogInput.setDescription(getString(R.string.itemChangePrice) + item + "-" + "Matrícula:"+shelving)
                .setValue(price.toString())
                .setOkButton(getString(R.string.modify)) {

                    if (customDialogInput.getValue().isNotEmpty()
                    ) {
                        callPacking(customDialogInput.getValue())
                    }


                }.setKoButton(getString(R.string.cancel)) {
                    customDialogInput.dismiss()
                }.show()

            //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) {

                    if (customDialogInput.getValue().isNotEmpty()) {
                        //callPacking(customDialogInput.getValue())
                        //getNewPrice(customDialogInput.getValue().toInt(), 5.51)
                    }

                    return@setOnEditorActionListener true
                }
                false
            }

            customDialogInput.setFocusText()
            ma.hideKeyboard(customDialogInput.getEditText())

        }

        private fun callPacking(value: String) {
            try {
               //  binding.splashProgress.visibility = View.VISIBLE
              /*  viewModel.travel_updatePacking(
                    itemFk = shelvingScaned,
                    packingFk = value.toInt()
                )*/

            } catch (ex: Exception) {
                getString(R.string.errorInput).toast(requireActivity())
            }
            customDialogInput.setValue("")
        }
    }


