package es.verdnatura.presentation.view.feature.qr


import android.view.KeyEvent.*
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import es.verdnatura.R
import es.verdnatura.databinding.FragmentQrBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.main.activity.MainActivity


@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class QrFragment(
    var entryPoint: String = ""
) : BaseFragment<FragmentQrBinding, QrFragmentViewModel>(QrFragmentViewModel::class) {

    private var goBack: Boolean = false
    override fun getLayoutId(): Int = R.layout.fragment_qr
    private lateinit var customDialog: CustomDialog
    private var itemscaned = ""

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


    override fun init() {

        binding.splashProgress.visibility = View.GONE
        customDialog = CustomDialog(requireContext())

        setToolbar()
        setEvents()
        super.init()
    }

    private fun setToolbar() {

        binding.mainToolbar.toolbarTitle.text = entryPoint
    }

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


    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) {


                goBack = false
                if (!binding.scanInput.text.isNullOrEmpty()) {
                    binding.splashProgress.visibility = View.VISIBLE
                    viewModel.qr_getCall(getData(USER), getData(PASSWORD), binding.scanInput.text.toString())
                    itemscaned = binding.scanInput.text.toString()
                }

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

    }

    override fun observeViewModel() {

        with(viewModel) {
            qrresponse.observe(viewLifecycleOwner, Observer {
                binding.splashProgress.visibility = View.GONE
                if (it.isError) {

                       ma.messageWithSound(it.errorMessage+  " "+itemscaned,true,false)

                } else {

                    customDialog.setTitle(getString(R.string.info))
                        .setDescription(getString(R.string.operation) + itemscaned + "." + getString(
                                                    R.string.answer) +it.response)
                        .setOkButton(getString(R.string.accept)) {
                            customDialog.dismiss()
                            binding.mainToolbar.backButton.performClick()
                        }.show()

                }

            })
        }

    }


}

