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

import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import es.verdnatura.R
import es.verdnatura.databinding.FragmentExpeditionscanSorterBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.itemScanned
import es.verdnatura.presentation.view.component.CustomDialogInputTwoValues
import org.json.JSONObject

class CmrExpeditionPalletFragment(
    var title: String = ""
) : BaseFragment<FragmentExpeditionscanSorterBinding, CmrExpeditionPalletViewModel>(
    CmrExpeditionPalletViewModel::class
) {

    override fun getLayoutId(): Int = R.layout.fragment_expeditionscan_sorter
    private lateinit var customDialogList: CustomDialogInputTwoValues
    private var itemScaned: String = ""
    private lateinit var dataScanned: itemScanned

    companion object {
        fun newInstance(title: String) = CmrExpeditionPalletFragment(title)
    }

    override fun init() {

        customDialogList = CustomDialogInputTwoValues(requireContext())
        ma.hideBottomNavigation(View.GONE)
        binding.splashProgress.visibility = View.GONE
        setEvents()
        setToolBar()
        showExpeditionPalletScan()
        super.init()
    }

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

    private fun cmrExpeditionPallet_add(vExpeditionPallet: String, vCmrId: String) {

        customDialogList.dismiss()
        binding.splashProgress.visibility = View.VISIBLE
        viewModel.cmrExpeditionPallet_add(
            getData(USER),
            getData(PASSWORD),
            vExpeditionPallet,
            vCmrId
        )

    }

    private fun setEvents() {

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

        }

    }

    override fun observeViewModel() {

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

                if (it.isError) {

                    ma.messageWithSound(
                        getString(R.string.cmrError) + it.errorMessage,
                        true,
                        true,
                        isToasted = true
                    )
                    showExpeditionPalletScan()

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

                }
            }

        }

    }

    private fun showExpeditionPalletScan() {

        customDialogList.setInfoTex(getString(R.string.pallet))
        customDialogList.setInfoTextTwo(getString(R.string.cmr))
        customDialogList.setInputTextVisibility(View.GONE)

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

                ma.hideKeyboard(customDialogList.getEditText())
                customDialogList.dismiss()
                binding.mainToolbar.backButton.performClick()


            }.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()) {
                    customDialogList.setInputTextVisibility(View.VISIBLE)
                    customDialogList.setTitle(getString(R.string.cmrScan))
                    customDialogList.setFocusTextTwo()

                }

                return@setOnEditorActionListener false
            }
            false
        }
        customDialogList.getEditTextTwo().setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5 || actionId == 6) {

                if (customDialogList.getValue().isNotEmpty() && customDialogList.getValueTwo()
                        .isNotEmpty()
                ) {
                    ma.hideKeyboard(customDialogList.getEditText())

                    if (itemScanedIsCMR(customDialogList.getValueTwo())) {
                        itemScaned = dataScanned.value
                        customDialogList.setValueTwo("")
                        cmrExpeditionPallet_add(
                            customDialogList.getValue(),
                            itemScaned
                        )
                    } else {
                        customDialogList.setValueTwo("")
                        ma.messageWithSound(getString(R.string.errorCmrNotScanned), true, true, isToasted = true)
                    }


                }

                return@setOnEditorActionListener true
            }
            false
        }
    }

    private fun itemScanedIsCMR(valueTwo: String): Boolean {

        return try {
            dataScanned = itemScanned(JSONObject(valueTwo))
            dataScanned.table == "cmr"

        } catch (Ex: Exception) {
            false
        }


    }

}

