package es.verdnatura.presentation.view.feature.showticket

import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import es.verdnatura.R
import es.verdnatura.databinding.FragmentControladorBinding
import es.verdnatura.domain.ConstAndValues
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnCollectionSelectedListener
import es.verdnatura.presentation.view.feature.precontrol.ShowTicketViewModel
import es.verdnatura.presentation.view.feature.sacador.model.CollectionVO

class ShowTicketFragment(var menuOrigin: String) :
    BaseFragment<FragmentControladorBinding, ShowTicketViewModel>(
        ShowTicketViewModel::class
    ) {

    private var goBack: Boolean = false
    private var onCollectionSelectedListener: OnCollectionSelectedListener? = null
    override fun getLayoutId(): Int = R.layout.fragment_controlador
    private var type = ""


    companion object {
        fun newInstance(menuOrigin: String) = ShowTicketFragment(menuOrigin = menuOrigin)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnCollectionSelectedListener) onCollectionSelectedListener = context
    }

    override fun init() {

       // binding.splashProgress.visibility = View.GONE
        binding.mainToolbar.toolbarTitle.text = getString(R.string.verticket)
        binding.splashProgress.visibility = View.GONE

        if (menuOrigin == getString(R.string.main)) {
            hideBackButton(binding.mainToolbar)
        }

        setEvents()

        super.init()
    }

    override fun onPause() {
        goBack = true
        super.onPause()
    }

    override fun onCreate(savedInstanceState: Bundle?) {

        type = ConstAndValues.VERTICKET
        super.onCreate(savedInstanceState)
    }

    private fun setEvents() {

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

        binding.scanInput.requestFocus()
        binding.scanInput.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5) {
                goBack = false
                if (!binding.scanInput.text.isNullOrEmpty()) {
                    binding.splashProgress.visibility = View.VISIBLE

                    viewModel.collectionTicketGet(
                        usuario = getData(USER),
                        password = getData(PASSWORD),
                        sectorFk = getData(SECTORFK),
                        collectionFk = binding.scanInput.text.toString(),
                        type = type

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

    override fun observeViewModel() {
        with(viewModel) {
            collectionTicketList.observe(viewLifecycleOwner) {
                binding.splashProgress.visibility = View.GONE
                if (it.isError) {
                    ma.messageWithSound(it.errorMessage, true, false)
                } else {
                    if (!goBack) navigateToCollectionList(it)
                    goBack = false
                }

            }


        }


    }

    private fun navigateToCollectionList(collection: CollectionVO) {
        onCollectionSelectedListener?.onCollectionSelected(collection, ConstAndValues.VERTICKET)
    }

}

