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

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.media.MediaPlayer
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentUbicadorBinding
import es.verdnatura.domain.notNull
import es.verdnatura.domain.toast
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.*
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.component.CustomDialogInput
import es.verdnatura.presentation.view.component.CustomDialogTwoButtons
import es.verdnatura.presentation.view.component.CustomDialogUbicador
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.ubicador.adapter.UbicadorAdapter
import es.verdnatura.presentation.view.feature.ubicador.model.ItemUbicadorVO


@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class UbicadorFragment(
    var shelvingFk: String = ""
) : BaseFragment<FragmentUbicadorBinding, UbicadorViewModel>(
    UbicadorViewModel::class
) {

    private var adapter: UbicadorAdapter? = null
    private lateinit var customDialogInput: CustomDialogInput
    private lateinit var customDialogUbicador: CustomDialogUbicador
    private lateinit var customDialogTwoButtons: CustomDialogTwoButtons
    private var listItems: ArrayList<ItemUbicadorVO> = ArrayList()
    private lateinit var customDialog: CustomDialog
    private var deep: Int = 1
    private var parking: String = ""
    private var etiquetas: String = "0"
    private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null
    var mperror: MediaPlayer? = null
    var mpok: MediaPlayer? = null


    companion object {
        fun newInstance(entryPoint: String) = UbicadorFragment(entryPoint)
    }

    override fun getLayoutId(): Int = R.layout.fragment_ubicador

    override fun onAttach(context: Context) {
        if (context is OnPasillerosItemClickListener) pasillerosItemClickListener = context
        super.onAttach(context)
    }

    override fun init() {

        customDialogInput = CustomDialogInput(requireContext())
        customDialog = CustomDialog(requireContext())
        customDialogUbicador = CustomDialogUbicador(requireContext())
        customDialogTwoButtons = CustomDialogTwoButtons(requireContext())

        ma.hideBottomNavigation(View.GONE)
        binding.mainToolbar.toolbarTitle.text = getString(R.string.locator)
        setEvents()
        setToolBar()
        if (!shelvingFk.isNullOrEmpty()) {
            binding.splashProgress.visibility = View.VISIBLE
            binding.mainToolbar.toolbarTitle.text = shelvingFk
            viewModel.itemShelvingList(getData(USER), getData(PASSWORD), shelvingFk)
        }
        super.init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        mperror = MediaPlayer.create((activity as MainActivity), R.raw.error)
        mpok = MediaPlayer.create((activity as MainActivity), R.raw.ok)
        super.onCreate(savedInstanceState)
    }

    private fun setToolBar() {

        val listIcons: ArrayList<ImageView> = ArrayList()
        val iconAdd = ImageView(context)
        iconAdd.setImageResource(R.drawable.ic_add_black_24dp)
        val iconReload = ImageView(context)
        iconReload.setImageResource(R.drawable.ic_autorenew_black_24dp)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            iconReload.tooltipText = getTooltip(R.drawable.ic_autorenew_black_24dp)
            iconAdd.tooltipText = getString(R.string.addItemShelving)
        }
        listIcons.add(iconAdd)
        listIcons.add(iconReload)
        binding.mainToolbar.toolbarIcons.adapter =
            ToolBarAdapterTooltip(listIcons, object : OnOptionsSelectedListener {
                override fun onOptionsItemSelected(item: Drawable) {

                    when (item) {
                        iconAdd.drawable -> showAddItem("", "", "", "", false, ItemUbicadorVO(), "")
                        iconReload.drawable -> {
                            binding.splashProgress.visibility = View.VISIBLE
                            viewModel.itemShelvingList(
                                getData(USER),
                                getData(PASSWORD),
                                shelvingFk
                            )
                        }
                    }
                    /*if (item == iconAdd){
                        showAddItem("","","","",false,ItemUbicadorVO(),"")
                    }else if(item == iconReload){
                        binding.splashProgress.visibility = View.VISIBLE
                        viewModel.itemShelvingList(user,password,shelvingFk,"itemShelvingList")
                    }*/
                }

            })
        binding.mainToolbar.toolbarIcons.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
    }

    @SuppressLint("SetTextI18n")
    private fun setEvents() {
        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        setTooltips()
        binding.editMatricula.requestFocus()
        binding.editMatricula.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.editMatricula.text.toString().isNullOrEmpty()) {
                    binding.mainToolbar.toolbarTitle.text = binding.editMatricula.text.toString()
                    shelvingFk = binding.editMatricula.text.toString()
                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.itemShelvingList(
                        getData(USER),
                        getData(PASSWORD),
                        binding.editMatricula.text.toString()
                    )
                }

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

            return@setOnEditorActionListener false
        }

        binding.editPrioridad.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (!binding.editPrioridad.text.toString().isNullOrEmpty()) {
                    viewModel.shelvingPriorityUpdate(
                        getData(USER),
                        getData(PASSWORD),
                        binding.editPrioridad.text.toString(),
                        shelvingFk
                    )
                }

                ma.hideKeyboard(binding.editPrioridad)
                return@setOnEditorActionListener true
            }

            return@setOnEditorActionListener false
        }


        binding.automaticImg.setOnClickListener {
            if (!shelvingFk.isNullOrEmpty())
                ma.onPasillerosItemClickListener(
                    PasillerosItemVO(title = getString(R.string.titleAuto)),
                    entryPoint = shelvingFk
                )
            else
                getString(R.string.scanShelving).toast(requireActivity(), Toast.LENGTH_SHORT)
        }

        binding.parkingImg.setOnClickListener {
            customDialogInput.setTitle(getString(R.string.parkingShelving)).setValue("").setOkButton(getString(
                            R.string.parking)) {
                viewModel.shelvingPark(
                    getData(USER),
                    getData(PASSWORD),
                    shelvingFk,
                    customDialogInput.getValue()
                )
                parking = customDialogInput.getValue()
                binding.mainToolbar.toolbarTitle.text =
                    shelvingFk.uppercase() + getString(R.string.pUppercase) +parking  + getString(R.string.label) + etiquetas
                if (mpok != null) mpok!!.start()
                customDialogInput.dismiss()
            }.setKoButton(getString(R.string.cancel)) {
                customDialogInput.dismiss()
            }.show()

            customDialogInput.getEditText().requestFocus()
            ma.hideKeyboard(customDialogInput.getEditText())

            customDialogInput.getEditText().setOnEditorActionListener { v, actionId, event ->
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                    viewModel.shelvingPark(
                        getData(USER),
                        getData(PASSWORD),
                        shelvingFk,
                        customDialogInput.getValue()
                    )
                    customDialogInput.dismiss()
                    parking = customDialogInput.getValue()
                    binding.mainToolbar.toolbarTitle.text =
                        shelvingFk.uppercase() + getString(R.string.pUppercase) + parking + getString(R.string.label) + etiquetas
                    if (mpok != null) {
                        mpok!!.start()
                    }

                    return@setOnEditorActionListener true
                }
                false
            }

        }

        binding.editImg.setOnClickListener {
            customDialogInput.setTitle(getString(R.string.changeShelving)).setValue("").setOkButton(getString(R.string.save)) {
                viewModel.shelvingChange(
                    getData(USER),
                    getData(PASSWORD),
                    shelvingFk,
                    customDialogInput.getValue()
                )
                shelvingFk = customDialogInput.getValue()
                binding.mainToolbar.toolbarTitle.text =
                    shelvingFk.uppercase() +getString(R.string.pUppercase) + parking +getString(R.string.label) + etiquetas
                if (mpok != null) mpok!!.start()
                customDialogInput.dismiss()
            }.setKoButton(getString(R.string.cancel)) {
                customDialogInput.dismiss()
            }.show()

            customDialogInput.getEditText().requestFocus()
            ma.hideKeyboard(customDialogInput.getEditText())

            customDialogInput.getEditText().setOnEditorActionListener { v, actionId, event ->
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                    viewModel.shelvingChange(
                        getData(USER),
                        getData(PASSWORD),
                        shelvingFk,
                        customDialogInput.getValue()
                    )
                    shelvingFk = customDialogInput.getValue()
                    binding.mainToolbar.toolbarTitle.text =
                        shelvingFk.uppercase() +getString(R.string.pUppercase) + parking +getString(R.string.label) + etiquetas
                    if (mpok != null) mpok!!.start()
                    customDialogInput.dismiss()
                    return@setOnEditorActionListener true
                }
                false
            }

        }

        binding.deleteImg.setOnClickListener {
            customDialog.setTitle(getString(R.string.empty))
                .setDescription(getString(R.string.operationNoReturn)).setOkButton(getString(R.string.empty)) {
                    viewModel.clearShelvingList(
                        getData(USER),
                        getData(PASSWORD), shelvingFk
                    )
                    listItems.forEach {
                        it.stickers = "0"
                        it.visible = "0"
                    }
                    if (adapter != null) {
                        adapter!!.notifyDataSetChanged()
                    }
                    customDialog.dismiss()
                }.setKoButton(getString(R.string.cancel)) {
                    customDialog.dismiss()
                }.show()


        }

    }

    private fun setTooltips() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            binding.editImg.tooltipText = getTooltip(R.drawable.ic_mode_edit_black_24dp)
            binding.deleteImg.tooltipText = getTooltip(R.drawable.ic_delete_forever_black_24dp)
            binding.editPrioridad.tooltipText =
                getString(R.string.changePriority)
            binding.parkingImg.tooltipText = getTooltip(R.drawable.ic_local_parking_black_24dp)
            binding.automaticImg.tooltipText =getTooltip(R.drawable.ic_flash_auto_black_24dp)


        }
    }

    @SuppressLint("SetTextI18n")
    override fun observeViewModel() {
        with(viewModel) {
            loadShelvingList.observe(viewLifecycleOwner, Observer { event ->
                event.getContentIfNotHandled().notNull {
                    deep = it.list.size
                    listItems = ArrayList()
                    var contEtiquetas= 0
                    it.list.forEach { item ->
                        try {
                            contEtiquetas += item.stickers.toInt()
                        } catch (e: Exception) {
                        }

                        parking = item.code.uppercase()
                        listItems.add(item)
                    }
                    etiquetas = contEtiquetas.toString()
                    binding.mainToolbar.toolbarTitle.text =
                        shelvingFk.uppercase() +getString(R.string.pUppercase) + parking +getString(R.string.label) + etiquetas
                    if (it.list.size > 0)
                        binding.editPrioridad.setText(it.list[0].priority)
                    binding.splashProgress.visibility = View.GONE

                    //sergio: para mostrar CustomDialogUbicator, ubicador con grouping
                    adapter = UbicadorAdapter(listItems,
                        onPasillerosItemClickListener = pasillerosItemClickListener!!,
                        onVisibleClickListener = object : OnVisibleClickListener {
                            override fun onVisibleClickListener(item: ItemUbicadorVO) {

                                // if (item.grouping.isNullOrEmpty()){item.grouping=""}
                                showAddItem(
                                    item.item,
                                    item.packing,
                                    item.stickers,
                                    item.visible,
                                    true,
                                    item,
                                    grouping = if (item.grouping.isNullOrEmpty()) {
                                        ""
                                    } else item.grouping
                                )

                            }

                        },
                        onMoreClickListener = object : OnMoreClickListener {
                            override fun onMoreClickListener(item: ItemUbicadorVO) {
                                showMoreOptions(item)
                            }

                        })

                    binding.locationRecyclerview.adapter = adapter
                    binding.locationRecyclerview.layoutManager =
                        LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, true)

                }
            })

            response.observe(viewLifecycleOwner, Observer {

                if (it.isError) {
                    ma.messageWithSound(it.errorMessage, it.isError, false)


                }else{
                    ma.messageWithSound("", it.isError, true)

                }
            })

            responsepriority.observe(viewLifecycleOwner, Observer {

                ma.messageWithSound(
                    if (it.isError) it.errorMessage else {
                        getString(R.string.priorityChanged)
                    }, it.isError, true
                )

            })

            responseMake.observe(viewLifecycleOwner, Observer {
                if (!shelvingFk.isNullOrEmpty()) {
                    binding.splashProgress.visibility = View.VISIBLE
                    binding.mainToolbar.toolbarTitle.text = shelvingFk
                    viewModel.itemShelvingList(
                        getData(USER),
                        getData(PASSWORD), shelvingFk
                    )
                }
            })
        }
    }

    private fun showAddItem(
        itemFk: String,
        packing: String,
        etiquetas: String,
        visible: String,
        delete: Boolean,
        itemUbicador: ItemUbicadorVO,
        grouping: String
    ) {
        var title = getString(R.string.newItem)
        if (delete) title = getString(R.string.editItem) + itemFk
        customDialogUbicador.setTitle(title).setItemValue(itemFk).setPackingValue(packing)
            .setGroupingValue(grouping).setEtiquetaValue(etiquetas).setVisibleValue(visible)
            .setOkButton(getString(R.string.save)) {
                var packingUpdate = false
                if (packing != customDialogUbicador.getPackingValue() || etiquetas != customDialogUbicador.getEtiquetaValue()) {
                    packingUpdate = true
                }
                saveNewItem(
                    customDialogUbicador.getItemValue(),
                    customDialogUbicador.getEtiquetaValue(),
                    customDialogUbicador.getPackingValue(),
                    customDialogUbicador.getVisibleValue(),
                    if (delete) packingUpdate else null,
                    itemUbicador.id,
                    customDialogUbicador.getGroupingValue()
                )
            }.setKoButton(getString(R.string.close)) {
                customDialogUbicador.dismiss()
            }.show()

        customDialogUbicador.getEditItem().requestFocus()
        ma.hideKeyboard(customDialogUbicador.getEditItem())

        customDialogUbicador.getVisibleItem().setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                var packingUpdate = false
                if (packing != customDialogUbicador.getPackingValue() || etiquetas != customDialogUbicador.getEtiquetaValue()) {
                    packingUpdate = true
                }
                saveNewItem(
                    customDialogUbicador.getItemValue(),
                    customDialogUbicador.getEtiquetaValue(),
                    customDialogUbicador.getPackingValue(),
                    customDialogUbicador.getVisibleValue(),
                    if (delete) packingUpdate else null,
                    itemUbicador.id,
                    customDialogUbicador.getGroupingValue()
                )
                return@setOnEditorActionListener true
            }
            false
        }

        customDialogUbicador.getEtiquetaItem().setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                var packingUpdate = false
                if (packing != customDialogUbicador.getPackingValue() || etiquetas != customDialogUbicador.getEtiquetaValue()) {
                    packingUpdate = true
                }
                saveNewItem(
                    customDialogUbicador.getItemValue(),
                    customDialogUbicador.getEtiquetaValue(),
                    customDialogUbicador.getPackingValue(),
                    customDialogUbicador.getVisibleValue(),
                    if (delete) packingUpdate else null,
                    itemUbicador.id,
                    customDialogUbicador.getGroupingValue()
                )
                return@setOnEditorActionListener true
            }
            false
        }

    }

    private fun saveNewItem(
        itemFk: String,
        etiquetas: String,
        packing: String,
        visible: String,
        updatePacking: Boolean? = null,
        itemShelvingFk: String,
        groupingPack: String
    ) {
        if (!packing.isNullOrEmpty()) {
            if (updatePacking == null) {
                if (!itemFk.isNullOrEmpty() && !visible.isNullOrEmpty()) {

                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.itemShelvingMake(
                        getData(USER),
                        getData(PASSWORD),
                        shelvingFk,
                        itemFk,
                        deep.toString(),
                        visible,
                        packing,
                        getData(WAREHOUSEFK),
                        "1",
                        groupingPack
                    )
                    /*changeOfflineValue(
                        ItemUbicadorVO(
                            item = itemFk,
                            visible = visible,
                            packing = packing
                        )
                    )*/
                    customDialogUbicador.setItemValue("").setPackingValue("").setGroupingValue("")
                        .setEtiquetaValue("").setVisibleValue("")
                    customDialogUbicador.getEditItem().requestFocus()
                    ma.hideKeyboard(customDialogUbicador.getEditItem())
                    customDialogUbicador.dismiss()

                } else if (!itemFk.isNullOrEmpty() && !etiquetas.isNullOrEmpty()) {
                    val visible_calc = (etiquetas.toInt() * packing.toInt()).toString()
                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.itemShelvingMake(
                        getData(USER),
                        getData(PASSWORD),
                        shelvingFk,
                        itemFk,
                        deep.toString(),
                        visible_calc,
                        packing,
                        getData(WAREHOUSEFK),
                        "1",
                        groupingPack
                    )
                    /*changeOfflineValue(
                        ItemUbicadorVO(
                            item = itemFk,
                            visible = visible_calc,
                            packing = packing,
                            stickers = etiquetas
                        )
                    )*/
                    customDialogUbicador.setItemValue("").setPackingValue("").setGroupingValue("")
                        .setEtiquetaValue("").setVisibleValue("")
                    customDialogUbicador.getEditItem().requestFocus()
                    ma.hideKeyboard(customDialogUbicador.getEditItem())
                    customDialogUbicador.dismiss()

                } else {
                    getString(R.string.fillFields).toast(
                        (activity as MainActivity),
                        Toast.LENGTH_LONG
                    )
                }
            } else {
                if (!itemFk.isNullOrEmpty() && !visible.isNullOrEmpty() && !updatePacking) {
                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.itemShelvingMakeEdit(
                        getData(USER),
                        getData(PASSWORD),
                        shelvingFk,
                        itemFk,
                        deep.toString(),
                        visible,
                        packing,
                        getData(WAREHOUSEFK),
                        "1",
                        itemShelvingFk,
                        groupingPack
                    )
                  /*  changeOfflineValue(
                        ItemUbicadorVO(
                            item = itemFk,
                            visible = visible,
                            packing = packing
                        )
                    )*/
                    customDialogUbicador.setItemValue("").setPackingValue("").setGroupingValue("")
                        .setEtiquetaValue("").setVisibleValue("")
                    customDialogUbicador.getEditItem().requestFocus()
                    ma.hideKeyboard(customDialogUbicador.getEditItem())
                    customDialogUbicador.dismiss()

                } else if (!itemFk.isNullOrEmpty() && !etiquetas.isNullOrEmpty() && updatePacking) {
                    var visible_calc = "0"
                    try {
                        visible_calc = (etiquetas.toInt() * packing.toInt()).toString()
                    } catch (e: Exception) {
                    }
                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.itemShelvingMakeEdit(
                        getData(USER),
                        getData(PASSWORD),
                        shelvingFk,
                        itemFk,
                        deep.toString(),
                        visible_calc,
                        packing,
                        getData(WAREHOUSEFK),
                        "1",
                        itemShelvingFk,
                        groupingPack
                    )
                   /* changeOfflineValue(
                        ItemUbicadorVO(
                            item = itemFk,
                            visible = visible_calc,
                            packing = packing,
                            stickers = etiquetas
                        )
                    )*/
                    customDialogUbicador.setItemValue("").setPackingValue("").setGroupingValue("")
                        .setEtiquetaValue("").setVisibleValue("")
                    customDialogUbicador.getEditItem().requestFocus()
                    ma.hideKeyboard(customDialogUbicador.getEditItem())
                    customDialogUbicador.dismiss()

                } else {
                    getString(R.string.fillFields).toast(
                        (activity as MainActivity),
                        Toast.LENGTH_LONG
                    )
                }
            }

        } else {
            getString(R.string.mandatoryPacking).toast((activity as MainActivity), Toast.LENGTH_LONG)
        }
    }

    /* private fun editItem(itemFk:String,etiquetas:String,packing:String,visible:String){
         if (!packing.isNullOrEmpty()){
             if (!itemFk.isNullOrEmpty() && !visible.isNullOrEmpty()){
                 viewModel.itemShelvingMake(user,password,shelvingFk,itemFk,deep.toString(),visible,packing,warehouseFk,"1","itemShelvingMake")
                 changeOfflineValue(ItemUbicadorVO(item = itemFk,visible = visible,packing = packing))
                 customDialogUbicador.setItemValue("").setPackingValue("").setEtiquetaValue("").setVisibleValue("")
                 customDialogUbicador.getEditItem().requestFocus()
                 ma.hideKeyboard(customDialogUbicador.getEditItem())
             }else if(!itemFk.isNullOrEmpty() && !etiquetas.isNullOrEmpty()){
                 val visible_calc = (etiquetas.toInt() * packing.toInt()).toString()
                 viewModel.itemShelvingMake(user,password,shelvingFk,itemFk,deep.toString(),visible_calc,packing,warehouseFk,"1","itemShelvingMake")
                 changeOfflineValue(ItemUbicadorVO(item = itemFk,visible = visible_calc,packing = packing,stickers = etiquetas))
                 customDialogUbicador.setItemValue("").setPackingValue("").setEtiquetaValue("").setVisibleValue("")
                 customDialogUbicador.getEditItem().requestFocus()
                 ma.hideKeyboard(customDialogUbicador.getEditItem())
             }else{
                 "Rellena campos obligatorios".toast((activity as MainActivity), Toast.LENGTH_LONG)
             }
         }else{
             "Packing obligatorio".toast((activity as MainActivity), Toast.LENGTH_LONG)
         }
     }*/

    /*private fun changeOfflineValue(item: ItemUbicadorVO) {
        binding.splashProgress.visibility = View.VISIBLE
        *//*listItems.add(item)
        adapter?.notifyDataSetChanged()*//*
    }*/

    private fun showMoreOptions(item: ItemUbicadorVO) {
        customDialogTwoButtons.setTitle(item.item).setDescription(getString(R.string.selectAction))
            .setOkButton(getString(R.string.transfer)) {
                customDialogInput.setTitle(getString(R.string.shelvingNew))
                    .setDescription(getString(R.string.selectDestiny) + item.item).setValue("")
                    .setOkButton(getString(R.string.transfer)) {
                        if (customDialogInput.getValue().isNotEmpty()) {
                            viewModel.itemShelvingTransfer(
                                getData(USER),
                                getData(PASSWORD),
                                item.id,
                                customDialogInput.getValue()
                            )
                            listItems.remove(item)
                            adapter!!.notifyDataSetChanged()
                            customDialogInput.dismiss()
                        } else {
                            getString(R.string.wagonIncorrect).toast(requireContext())


                        }


                    }.setKoButton(getString(R.string.cancel)) {
                        customDialogInput.dismiss()
                    }.show()
                customDialogInput.getEditText().requestFocus()
                ma.hideKeyboard(customDialogInput.getEditText())
                customDialogInput.getEditText().setOnEditorActionListener { v, actionId, event ->
                    if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                        viewModel.itemShelvingTransfer(
                            getData(USER),
                            getData(PASSWORD),
                            item.id,
                            customDialogInput.getValue()
                        )
                        listItems.remove(item)
                        adapter!!.notifyDataSetChanged()
                        customDialogInput.dismiss()
                        return@setOnEditorActionListener true
                    }
                    false
                }
                customDialogTwoButtons.dismiss()
            }.setOkButtonTwo(getString(R.string.delete)) {
                customDialog.setTitle(getString(R.string.confirm))
                    .setDescription(getString(R.string.itemDeleteConfirm) + item.item + "?")
                    .setOkButton(getString(R.string.delete)) {
                        listItems.remove(item)
                        adapter!!.notifyDataSetChanged()
                        viewModel.itemShelvingDelete(
                            getData(USER),
                            getData(PASSWORD), item.id
                        )
                        customDialogTwoButtons.dismiss()
                        customDialog.dismiss()
                    }.setKoButton(getString(R.string.cancel)) {
                        customDialog.dismiss()
                        customDialogTwoButtons.dismiss()
                    }.show()

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