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

import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import es.verdnatura.R
import es.verdnatura.databinding.FragmentShelvinglogBinding
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.historicoshelvinglog.adapter.ShelvingLogAdapter
import es.verdnatura.presentation.view.feature.historicoshelvinglog.model.LogType
import es.verdnatura.presentation.view.feature.pasillero.model.PasillerosItemVO

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class ShelvingLogFragment(
    var entryPoint: String = "",
    private var logType: LogType

) : BaseFragment<FragmentShelvinglogBinding, ShelvingLogViewModel>(
    ShelvingLogViewModel::class
) {
    private var adapter: ShelvingLogAdapter? = null
    private var itemScan = ""

    companion object {
        fun newInstance(entryPoint: String, logType: LogType) =
            ShelvingLogFragment(entryPoint, logType)
    }

    private lateinit var customDialog: CustomDialog

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

    override fun init() {

        customDialog = CustomDialog(requireContext())
        binding.linearHeader.visibility = View.GONE
        setEvents()
        setToolBar(entryPoint)
        super.init()

    }

    private fun setToolBar(title:String) {

        if (logType == LogType.SHELVING) {
            binding.mainToolbar.toolbarTitle.text = title

        } else {
            binding.mainToolbar.toolbarTitle.text = title
            binding.scanInput.setHint(getString(R.string.scanParkingTxt))

        }

    }

    private fun setEvents() {
        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                if (binding.scanInput.text.toString().isNotEmpty()) {

                    when (logType) {
                        LogType.SHELVING -> {
                            viewModel.shelvings((binding.scanInput.text.toString()))

                        }

                        LogType.PREVIOUS -> viewModel.getParkingId((binding.scanInput.text.toString()))
                    }
                    itemScan = binding.scanInput.text.toString()
                }
                binding.scanInput.setText("")
                ma.hideKeyboard(binding.scanInput)
                return@setOnEditorActionListener true
            }
            false
        }

        binding.mainToolbar.backButton.setOnClickListener {
            ma.onMyBackPressed()
        }

    }

    override fun observeViewModel() {
        with(viewModel) {

            loadShelvingLogSalixList.observe(viewLifecycleOwner) { event ->
                event.getContentIfNotHandled().notNull {
                    binding.itemshelvinglogRecyclerview.adapter = adapter
                    binding.itemshelvinglogRecyclerview.layoutManager =
                        LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
                    setToolBar(getString(R.string.shelvingLog) + ":" + itemScan)
                    if (it.list.isEmpty()) {
                        customDialog.setTitle(getString(R.string.noResults))
                            .setDescription(getString(R.string.noDataLabelScanned))
                            .setOkButton(getString(R.string.close)) {
                                customDialog.dismiss()
                            }.show()
                    } else {
                        openWebViewer(it.list[0].id!!, "shelving")
                    }
                }

            }
            loadResponseParking.observe(viewLifecycleOwner) { event ->
                event.getContentIfNotHandled().notNull {
                    setToolBar(getString(R.string.parking) + ":" + itemScan)
                    openWebViewer(it, "parking")

                }
            }
        }
    }

    private fun openWebViewer(param: Int, web: String) {
        ma.onPasillerosItemClickListener(
            PasillerosItemVO(
                title = R.string.titleWebViewer,
            ),

            entryPoint = Gson().toJson(
                mutableMapOf(
                    "entryPoint" to param,
                    "web" to "${mobileApplication.dataStoreApp.getServerLilium()}/#/$web/$param/log"
                )
            )

        )

    }

}
