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

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import es.verdnatura.R
import es.verdnatura.databinding.FragmentBufferBinding
import es.verdnatura.presentation.base.BaseFragment

class BufferFragment(
    var entryPoint: String = ""
) : BaseFragment<FragmentBufferBinding, BufferFragmentViewModel>(BufferFragmentViewModel::class) {

    private var password = ""
    //private var goBack: Boolean = false
    override fun getLayoutId(): Int = R.layout.fragment_buffer

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


    override fun init() {
        
        binding.splashProgress.visibility = View.GONE
        setToolbar()
        setEvents()
        super.init()
    }

    private fun setToolbar() {

        binding.mainToolbar.toolbarTitle.text = entryPoint
    }

    override fun onCreate(savedInstanceState: Bundle?) {

        password = getData(PASSWORD)
        super.onCreate(savedInstanceState)
    }

    private fun setEvents() {

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

        binding.scanInput.requestFocus()
         binding.scanInput.setOnEditorActionListener { _, actionId, _ ->
            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


                    when (entryPoint){

                        getString(R.string.titleBufferEmpty)->entryPoint="UNLOADING"
                        getString(R.string.titleBufferFill)-> entryPoint="LOADING"
                        getString(R.string.titleBufferAccumulate)->entryPoint="ACCUMULATION"
                        getString(R.string.titleBufferOff)->entryPoint="DISABLED"
                        getString(R.string.titleBufferStrapping)->entryPoint="STRAPPING"
                    }

                        viewModel.buffer_setTypeByName(
                            getData(USER),
                            getData(PASSWORD),
                            binding.scanInput.text.toString(),
                            entryPoint

                        )


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

    override fun observeViewModel() {

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

                    ma.messageWithSound(
                        getString(R.string.errorOperation)+ it.errorMessage,it.isError,isPlayed = true)

                } else {

                    ma.messageWithSound(entryPoint.lowercase() + getString (R.string.operationSuccess),false,isPlayed = true)
                    binding.mainToolbar.backButton.performClick()

                }

            })
        }

    }


}

