package es.verdnatura.presentation.view.feature.main.activity


import android.content.SharedPreferences
import android.media.MediaPlayer
import android.util.Log
import android.view.Menu
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.databinding.ktx.BuildConfig
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.google.android.material.bottomnavigation.LabelVisibilityMode.LABEL_VISIBILITY_UNLABELED
import es.verdnatura.R
import es.verdnatura.databinding.ActivityMainBinding
import es.verdnatura.domain.ConstAndValues.MAINACTIVITY
import es.verdnatura.domain.ConstAndValues.PREITEMPICKER
import es.verdnatura.domain.toast
import es.verdnatura.presentation.base.BaseActivity
import es.verdnatura.presentation.common.*
import es.verdnatura.presentation.testing.testPrint
import es.verdnatura.presentation.view.component.CustomDialogMainActivity
import es.verdnatura.presentation.view.feature.ajustes.fragment.AjustesFragment
import es.verdnatura.presentation.view.feature.articulo.fragment.ItemCardFragment
import es.verdnatura.presentation.view.feature.articulo.fragment.ItemProposalFragment
import es.verdnatura.presentation.view.feature.buffer.fragment.BufferFragment
import es.verdnatura.presentation.view.feature.buffer.fragment.BufferLoadFragment
import es.verdnatura.presentation.view.feature.buscaritem.fragment.BuscarItemFragment
import es.verdnatura.presentation.view.feature.calidad.fragment.BuyersFragment
import es.verdnatura.presentation.view.feature.calidad.fragment.QaualityFragment
import es.verdnatura.presentation.view.feature.claim.fragment.ClaimFragment
import es.verdnatura.presentation.view.feature.collection.fragment.CollectionFragment
import es.verdnatura.presentation.view.feature.collection.fragment.CollectionShowTicketFragment
import es.verdnatura.presentation.view.feature.controlador.fragment.ControladorFragment
import es.verdnatura.presentation.view.feature.controlador.fragment.WebFragment
import es.verdnatura.presentation.view.feature.controlvehiculo.fragment.ControlVehiculoFragment
import es.verdnatura.presentation.view.feature.controlvehiculo.fragment.ControlVehiculoUsuarioFragment
import es.verdnatura.presentation.view.feature.diadeventa.fragment.DayOfSaleFragment
import es.verdnatura.presentation.view.feature.faltas.fragment.FaltasFragment
import es.verdnatura.presentation.view.feature.historicoarticulo.fragment.HistoricoArticuloFragment
import es.verdnatura.presentation.view.feature.historicoshelving.fragment.itemShelvingLogFragment
import es.verdnatura.presentation.view.feature.historicoshelvinglog.fragment.shelvingLogFragment
import es.verdnatura.presentation.view.feature.historicovehiculo.fragment.HistoricoVehiculoFragment
import es.verdnatura.presentation.view.feature.inventario.fragment.InventaryFragment
import es.verdnatura.presentation.view.feature.main.model.ItemMenuVO
import es.verdnatura.presentation.view.feature.paletizador.fragment.*
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckVO
import es.verdnatura.presentation.view.feature.paletizador.model.ItemPalletVO
import es.verdnatura.presentation.view.feature.parking.fragment.ParkingFragment
import es.verdnatura.presentation.view.feature.pasillero.fragment.PasilleroFragment
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO
import es.verdnatura.presentation.view.feature.precontrol.PreControladorFragment
import es.verdnatura.presentation.view.feature.presacador.fragment.EndSacadorFragment
import es.verdnatura.presentation.view.feature.presacador.fragment.InitPreSacadorFragment
import es.verdnatura.presentation.view.feature.qr.QrFragment
import es.verdnatura.presentation.view.feature.reposicion.fragment.ReposicionFragment
import es.verdnatura.presentation.view.feature.reubication.fragment.ReubicationCollectionFragment
import es.verdnatura.presentation.view.feature.reubication.fragment.ReubicationFragment
import es.verdnatura.presentation.view.feature.reubication.model.Reubication
import es.verdnatura.presentation.view.feature.sacador.fragment.SacadorFragment
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO
import es.verdnatura.presentation.view.feature.shelvingparking.fragment.ShelvingParkingFragment
import es.verdnatura.presentation.view.feature.showticket.ShowTicketFragment
import es.verdnatura.presentation.view.feature.smarttag.register.RegisterSmartTagsFragment
import es.verdnatura.presentation.view.feature.smarttag.sacador.fragment.AssociateSmartTagsFragment
import es.verdnatura.presentation.view.feature.ubicador.fragment.AutomaticAddItemFragment
import es.verdnatura.presentation.view.feature.ubicador.fragment.UbicadorFragment
import es.verdnatura.presentation.view.feature.workermistake.adapter.WorkermistakeFragment
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import timber.log.Timber


class MainActivity : BaseActivity<ActivityMainBinding>(), OnPasillerosItemClickListener,
    OnTruckClickListener, OnPalletClickListener, OnComprobarPalletViewClickListener,
    OnCollectionSelectedListener, OnBuyerSelectedListener {

    private var lastBottomMenuItemSelected: ItemMenuVO? = null
    private lateinit var customDialog: CustomDialogMainActivity
    var firstItem: ItemMenuVO? = null
    var fm = supportFragmentManager

    //var TAG = "MainActivity"
    var mperror: MediaPlayer? = null
    var mpok: MediaPlayer? = null
    override fun getLayoutId(): Int = R.layout.activity_main
    override fun init() {

        mperror = MediaPlayer.create((this), R.raw.error)
        mpok = MediaPlayer.create((this), R.raw.ok)
        customDialog = CustomDialogMainActivity(this)


        setBottomMenuFragment()

        if (haveSector() && havePrinter()) addFragment(
            PasilleroFragment.newInstance(getString(R.string.main), true),
            R.id.main_frame_layout,
            PasilleroFragment.TAG,
            false
        )
        else addFragment(
            AjustesFragment.newInstance(),
            R.id.main_frame_layout,
            AjustesFragment.TAG,
            false
        )

        // sergio: de esta manera se eliminan los logs de lanzamiento
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
        }


    }

    fun haveSector(): Boolean {
        val prefs: SharedPreferences = getSharedPreferences("es.verdnatura.user.prefs", 0)
        val sectorFk = prefs.getInt("sectorFk", -1)
        return sectorFk != -1

    }


    fun havePrinter(): Boolean {
        val prefs: SharedPreferences = getSharedPreferences("es.verdnatura.user.prefs", 0)
        val printerFk = prefs.getInt("printerFk", -1)
        return printerFk != -1

    }

    @DelicateCoroutinesApi
    fun setBottomMenuFragment() {
        val bottomMenu = binding.mainBottomNavigation
        //iconos abajo sin texto
        bottomMenu.labelVisibilityMode = LABEL_VISIBILITY_UNLABELED

        bottomMenu.itemIconTintList = null
        GlobalScope.launch(Dispatchers.Main) {
            val bottomMenuItems: ArrayList<ItemMenuVO> = ArrayList()

            bottomMenuItems.add(
                ItemMenuVO(
                    0,
                    title = getString(R.string.titleCorridors),
                    defaultImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_init_ui
                    ),
                    selectedImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_init_ui_selected
                    )
                )
            )

            bottomMenuItems.add(
                ItemMenuVO(
                    1,
                    title = getString(R.string.verticket),
                    defaultImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_eye_ui
                    ),
                    selectedImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_eye_ui_selected
                    )
                )
            )

            bottomMenuItems.add(
                ItemMenuVO(
                    2,
                    title = getString(R.string.Parking),
                    defaultImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_parking_ui
                    ),
                    selectedImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_parking_ui_selected
                    )
                )
            )

            bottomMenuItems.add(
                ItemMenuVO(
                    3,
                    title = getString(R.string.Ajustes),
                    defaultImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_setting_ui
                    ),
                    selectedImage = ContextCompat.getDrawable(
                        applicationContext, R.drawable.ic_setting_ui_selected
                    )
                )
            )


            // Add al items to menu view
            bottomMenuItems.forEach {
                bottomMenu.menu.add(Menu.NONE, it.id, Menu.NONE, it.title).icon = it.defaultImage
            }


            if (haveSector()) {
                // Select first item by default
                firstItem = bottomMenuItems.first()
                bottomMenu.selectedItemId = firstItem!!.id
                bottomMenu.menu.findItem(firstItem!!.id).icon = firstItem!!.selectedImage
                lastBottomMenuItemSelected = firstItem
            } else {
                // Select last item by default
                val lastItem = bottomMenuItems.last()
                bottomMenu.selectedItemId = lastItem.id
                bottomMenu.menu.findItem(lastItem.id).icon = lastItem.selectedImage
                lastBottomMenuItemSelected = lastItem
            }


            bottomMenu.setOnNavigationItemSelectedListener { menuItem ->
                // Get selected item from itemList and apply ui changes to menu view
                val selectedItemMenu = bottomMenuItems.find { menuItem.itemId == it.id }
                menuItem.icon = selectedItemMenu?.selectedImage

                // Get last selected item and disable it
                bottomMenu.menu.findItem(lastBottomMenuItemSelected?.id!!).icon =
                    lastBottomMenuItemSelected?.defaultImage
                lastBottomMenuItemSelected = selectedItemMenu


                when (selectedItemMenu?.title) {


                    getString(R.string.titleCorridors) -> {
                        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
                        delete_Fragments()
                        addFragment(
                            PasilleroFragment.newInstance(getString(R.string.main), true),
                            R.id.main_frame_layout,
                            PasilleroFragment.TAG,
                            false
                        )

                    }

                    getString(R.string.verticket) -> {
                        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
                        delete_Fragments()
                        addFragment(
                            ShowTicketFragment.newInstance(getString(R.string.main)),
                            R.id.main_frame_layout,
                            MAINACTIVITY.TAG,
                            false
                        )

                    }
                    getString(R.string.Parking) -> {
                        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
                        delete_Fragments()
                        addFragment(
                            ParkingFragment.newInstance(getString(R.string.main)),
                            R.id.main_frame_layout,
                            MAINACTIVITY.TAG,
                            false
                        )


                    }
                    getString(R.string.Ajustes) -> {
                        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
                        delete_Fragments()
                        addFragment(
                            AjustesFragment.newInstance(),
                            R.id.main_frame_layout,
                            AjustesFragment.TAG,
                            false
                        )
                    }


                }

                true
            }
            /* sergio: de esta manera no repinta el icono del menu seleccionado */
            bottomMenu.setOnNavigationItemReselectedListener {

                when (it.title) {

                    getString(R.string.titleCorridors) -> {
                        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
                        delete_Fragments()
                        addFragment(
                            PasilleroFragment.newInstance(getString(R.string.main), true),
                            R.id.main_frame_layout,
                            PasilleroFragment.TAG,
                            false
                        )

                    }
                }

            }

        }
    }

    private fun delete_Fragments() {
        fm.getFragments().forEach {
            val fragment: Fragment? = supportFragmentManager.findFragmentByTag(it.tag.toString())
            if (fragment != null) supportFragmentManager
                .beginTransaction().remove(fragment)
                .commit()
        }
    }

    override fun onPasillerosItemClickListener(item: PasillerosItemVO, entryPoint: String) {

        /*  d("VERDNATURA::","El title es "+item.title)
          d("VERDNATURA::","El titleTOsHOW es "+item.titleToShow)
          d("VERDNATURA::","El entryPoint es "+entryPoint)*/
        when (item.title) {

            getString(R.string.titleSmarttagPickerAssociate) -> {
                addFragmentOnTop(AssociateSmartTagsFragment.newInstance(getString(R.string.SmarttagAssociate)))
            }

            getString(R.string.titleSmarttagsRegister) -> {
                addFragmentOnTop(RegisterSmartTagsFragment.newInstance(item.title))
            }

            //BUFFERS
            getString(R.string.titlePalletizar) -> {
                addFragmentOnTop(ExpeditionTruckListFragment.newInstance())
            }

            getString(R.string.titlePalletizers) -> {

                addFragmentOnTop(
                    PasilleroFragment.newInstance(
                        getString(R.string.titlePalletizers),
                        isInitMenu = false
                    )
                )


            }
            getString(R.string.titleControllers) -> {
                addFragmentOnTop(ControladorFragment.newInstance())


            }

            getString(R.string.titleCorridors) -> {
                addFragmentOnTop(
                    PasilleroFragment.newInstance(item.title, isInitMenu = false),
                )

            }

            getString(R.string.titleExpeditionState) -> {
                // addFragment(ExpeditionStateFragment.newInstance(entryPoint),R.id.main_frame_layout, ExpeditionTruckListFragment.TAG,false)
                addFragmentOnTop(ExpeditionStateFragment.newInstance(item.title))
            }
            getString(R.string.titleScanExpedition) -> {
                addFragmentOnTop(ExpeditionScanSorterFragment.newInstance(item.title))
            }

            getString(R.string.titleBufferManegement) -> {
                addFragmentOnTop(
                    PasilleroFragment.newInstance(
                        item.title,
                        isInitMenu = false
                    )
                )
            }
            getString(R.string.titleClaims) -> {
                addFragmentOnTop(
                    PasilleroFragment.newInstance(
                        item.title,
                        isInitMenu = false
                    )
                )

            }

            getString(R.string.titleUserControlVehicle) -> {
                addFragmentOnTop(ControlVehiculoUsuarioFragment.newInstance(entryPoint))
            }

            /* getString(R.string.titlePrePicker) -> {
                 addFragmentOnTop(PreSacadorFragment.newInstance())
             }*/
            getString(R.string.titleDayOfSale) -> {
                addFragmentOnTop(DayOfSaleFragment(item.title))
            }

            getString(R.string.titleShowTicket) -> {
                addFragmentOnTop(ShowTicketFragment.newInstance(getString(R.string.titleCorridors)))
            }
            getString(R.string.titleHistoricalVehicle) -> {
                addFragmentOnTop(ControlVehiculoFragment.newInstance(HistoricoVehiculoFragment.TAG))
                //   fm.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE)
                //  addFragment(HistoricoVehiculoFragment.newInstance(entryPoint),R.id.main_frame_layout, ControladorFragment.TAG,false)
            }
            getString(R.string.titleReplacement) -> {
                addFragmentOnTop(ReposicionFragment.newInstance())
            }
            getString(R.string.titleItemConsult) -> {
                addFragmentOnTop(ItemCardFragment.newInstance(entryPoint))
            }
            //sergio:no visible desde el menú inicio
            getString(R.string.titleSearchSimilarItems) -> {
                addFragmentOnTop(ItemProposalFragment.newInstance(entryPoint))
            }

            //segio: esta opción no se muestra, solo es accesible para añadir fragmentos de otras partes.
            getString(R.string.titleHistoricalHide) -> {
                addFragmentOnTop(HistoricoVehiculoFragment.newInstance(entryPoint))
            }

            getString(R.string.Parking) -> {
                addFragmentOnTop(ParkingFragment.newInstance(""))
            }

            getString(R.string.titleItemSearch) -> {
                addFragmentOnTop(BuscarItemFragment.newInstance(entryPoint))
            }

            getString(R.string.titleHistorical) -> {
                addFragmentOnTop(HistoricoArticuloFragment.newInstance(entryPoint))
            }

            getString(R.string.titleInventory) -> {
                addFragmentOnTop(InventaryFragment.newInstance())
            }
            getString(R.string.titleFaults) -> {
                addFragmentOnTop(FaltasFragment.newInstance())
            }
            getString(R.string.titleShelvingParking) -> {
                addFragmentOnTop(ShelvingParkingFragment.newInstance())
            }
            getString(R.string.titleUbicator) -> {
                addFragmentOnTop(UbicadorFragment.newInstance(entryPoint))
            }
            getString(R.string.titleAuto) -> {
                addFragmentOnTop(AutomaticAddItemFragment.newInstance(entryPoint))
            }

            getString(R.string.titleQuality) -> {
                addFragmentOnTop(BuyersFragment.newInstance())
            }
            getString(R.string.titlePickers) -> {
                //addFragmentOnTop(SacadorFragment.newInstance())
                addFragmentOnTop(ControlVehiculoFragment.newInstance(""))
            }

            getString(R.string.titleWebViewer) -> {
                // el entryPoint de WebFragment se le pasa un JSON del tipo {"entryPoint":"itemScanned","web":"www.google.com}
                addFragmentOnTop(WebFragment.newInstance(entryPoint))
            }
            //Menu qr
            getString(R.string.titleQrReader) -> {
                addFragmentOnTop(QrFragment.newInstance(item.title))
            }

            //Menu buffer
            getString(R.string.titleBufferEmpty) -> {
                addFragmentOnTop(BufferFragment.newInstance(item.title))
            }
            getString(R.string.titleBufferFill) -> {
                addFragmentOnTop(BufferFragment.newInstance(item.title))
            }
            getString(R.string.titleBufferAccumulate) -> {
                addFragmentOnTop(BufferFragment.newInstance(item.title))
            }
            getString(R.string.titleBufferOff) -> {
                addFragmentOnTop(BufferFragment.newInstance(item.title))
            }
            getString(R.string.titleBufferload) -> {
                addFragmentOnTop(BufferLoadFragment.newInstance(item.title))
            }
            getString(R.string.titleBufferStrapping) -> {
                addFragmentOnTop(BufferFragment.newInstance(item.title))
            }

            getString(R.string.titlePreControl) -> {
                addFragmentOnTop(PreControladorFragment.newInstance())
            }
            getString(R.string.titleShelvingHistorical) -> {
                addFragmentOnTop(itemShelvingLogFragment.newInstance(item.title))

            }
            getString(R.string.titleLogShelving) -> {
                addFragmentOnTop(shelvingLogFragment.newInstance(item.title))
            }
            getString(R.string.titleWorkerMistake) -> {
                addFragmentOnTop(WorkermistakeFragment.newInstance(item.title))
            }

            getString(R.string.titlePrePicker) -> {
                addFragmentOnTop(InitPreSacadorFragment.newInstance(getString(R.string.getPreviousCollection)))
            }

            getString(R.string.titleClaimUbication) -> {
                addFragmentOnTop(ClaimFragment.newInstance(item.title))
            }

            getString(R.string.titleReUbicator) -> {
                addFragmentOnTop(ReubicationFragment.newInstance(item.title))
            }
            getString(R.string.titleCMRState) -> {
                addFragmentOnTop(CmrExpeditionPalletFragment.newInstance(item.title))
            }

            getString(R.string.scanPlatform) -> {
                addFragmentOnTop(PalletScanFragment.newInstance(item.title, "receive"))
            }


            getString(R.string.titleScanPalletizar) -> {
                addFragmentOnTop(PalletScanFragment.newInstance(item.title, "get"))
            }

            getString(R.string.testing) -> {
                addFragmentOnTop(testPrint.newInstance(item.title))
            }
        }

    }

    fun addFragmentOnTop(fragment: Fragment?) {

        supportFragmentManager
            .beginTransaction()
            .replace(R.id.main_frame_layout, fragment!!)
            .addToBackStack(null)
            .commitAllowingStateLoss()
    }

    override fun onBackPressed() {

        var index = supportFragmentManager.backStackEntryCount - 1
        var backEntry =""
        if (index >= 0) {
            backEntry = supportFragmentManager.getBackStackEntryAt(index).name.toString()
            //Log.d("VERDNATURA::", "El fragment es " + backEntry)
        }



            try {
                fm.executePendingTransactions()
            } catch (e: Exception) {
            }

            if (fm.backStackEntryCount > 0) {

                fm.popBackStackImmediate()

                if (!backEntry.isNullOrBlank())
                    if (backEntry!!.contains("fragment.ExpeditionPalletDetailFragment.Companion")) {
                        addFragmentOnTop(ExpeditionTruckListFragment.newInstance())
                    }
            } else {
                customDialog.setTitle(getString(R.string.closeSession))
                    .setDescription(getString(R.string.sureCloseSession))
                    .setOkButton(getString(R.string.exit)) {
                        customDialog.dismiss()
                        finish()

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

            }
        }

        override fun onTruckClickListener(item: ItemExpeditionTruckVO, entryPoint: String) {
            //Log.d("VERDNATURA","Pulsdo item"+item.truckFk)
            addFragmentOnTop(ExpeditionPalletFragment.newInstance(item))

        }

        override fun onPalletClickListener(
            itemTruck: ItemExpeditionTruckVO,
            itemPallet: ItemPalletVO
        ) {
            //Tarea #4125
            //Log.d("VERDNATURA::","Entrando en pallet2")
            addFragmentOnTop(ExpeditionScanFragment.newInstance(itemTruck, itemPallet))

        }


        override fun onComprobarPalletViewClickListener(
            itemTruck: ItemExpeditionTruckVO,
            itemPallet: ItemPalletVO
        ) {

            addFragmentOnTop(ExpeditionPalletDetailFragment.newInstance(itemTruck, itemPallet))

        }

        fun onComprobarPalletFromPalletScan(
            itemTruck: ItemExpeditionTruckVO,
            itemPallet: ItemPalletVO
        ) {
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

            addFragment(
                ExpeditionPalletDetailFragment.newInstance(itemTruck, itemPallet),
                R.id.main_frame_layout,
                ExpeditionPalletDetailFragment.TAG,
                true
            )

        }

        /*  fun onParkingSaleSelected(sales: SaleVO) {
              addFragmentOnTop(ParkingSaleFragment.newInstance(SaleVO = sales))
          }*/


        override fun onCollectionSelected(collection: CollectionVO, type: String) {

            when (type) {
                PREITEMPICKER -> {

                    addFragmentOnTop(EndSacadorFragment.newInstance(collection, type))
                }
                "SHOWTICKET" -> {
                    addFragmentOnTop(CollectionShowTicketFragment.newInstance(collection, type))
                }

                else -> {
                    addFragmentOnTop(CollectionFragment.newInstance(collection, type))
                }
            }


        }

        override fun onBuyerSelected(userFk: String) {
            addFragmentOnTop(QaualityFragment.newInstance(userFk))
        }


        //sergio:se llama al pulsar sobre el coche IN desde Sacador
        fun onVehicleSelected(tag: String) {
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

            // se pone a true en la pila para que cuando se vuelva atrás permanezca ya que se borran todos los fragmentos desde otras opciones
            addFragment(
                ControlVehiculoFragment.newInstance(tag),
                R.id.main_frame_layout,
                ControlVehiculoFragment.TAG,
                true
            )
            //sergio: de esta manera la llamada del fragment no se vuelve a repetir.
            // addFragmentOnTop(ControlVehiculoFragment.newInstance(tag))
        }

        fun openFragmentPickers(delete: Boolean) {
            //delete_Fragments()
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
            if (delete) {
                delete_Fragments()
            }
            addFragment(
                SacadorFragment.newInstance(),
                R.id.main_frame_layout,
                SacadorFragment.TAG,
                true


            )
            //addFragmentOnTop(SacadorFragment.newInstance())

        }

        fun openFragmentExpeditionState() {
            //delete_Fragments()
            // fm.popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE)
            // addFragment(ExpeditionStateFragment.newInstance("main"),R.id.main_frame_layout, ExpeditionStateFragment.TAG,false)

            addFragmentOnTop(ExpeditionStateFragment.newInstance(getString(R.string.titleExpeditionState)))

        }

        fun openFragmentReubications(list: MutableList<Reubication>, shelving: String) {

            addFragmentOnTop(ReubicationCollectionFragment.newInstance(list, shelving))

        }

        fun hideBottomNavigation(visible: Int) {
            binding.mainBottomNavigation.visibility = visible
        }


        fun messageWithSound(
            message: String,
            isError: Boolean,
            isPlayed: Boolean?,
            titleWithError: String = getString(R.string.error),
            isToasted: Boolean? = true
        ) {

            if (!isError) {

                if (isPlayed == true) (mpok?.start())
                if (isToasted!!) message.toast(this, Toast.LENGTH_SHORT)
            } else {

                if (isPlayed == true) (mperror?.start())

                if (isToasted == true) {
                    message.toast(this, Toast.LENGTH_SHORT)
                } else {
                    customDialog.setTitle(titleWithError).setDescription(message)
                        .setOkButton(getString(R.string.accept)) {
                            customDialog.dismiss()
                        }.show()
                }


            }


        }
    }
