@file:Suppress("UNUSED_ANONYMOUS_PARAMETER")

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


import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.inputmethod.EditorInfo
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentGeneralBlackBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.GeneralAdapter
import es.verdnatura.presentation.common.GeneralItem
import es.verdnatura.presentation.common.OnGeneralItemRowClickListener
import es.verdnatura.presentation.common.hideKeyboard
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.component.CustomDialogList

class ClaimFragment(
    var entryPoint: String = ""
) : BaseFragment<FragmentGeneralBlackBinding, ClaimViewModel>(
    ClaimViewModel::class
) {

    private var goBack: Boolean = false

    override fun getLayoutId(): Int = R.layout.fragment_general_black
    private lateinit var customDialog: CustomDialog
    private lateinit var customDialogList: CustomDialogList
    private var listClaims: ArrayList<GeneralItem> = ArrayList()
    private var claimAdapter: GeneralAdapter? = null


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


    override fun init() {

        customDialog = CustomDialog(requireContext())
        customDialogList = CustomDialogList(requireContext())
        goBack = false
        ma.hideBottomNavigation(GONE)
        binding.splashProgress.visibility = GONE

        setEvents()
        setToolBar()
        setAdapter()
        showInputClaim()
        super.init()
    }

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

    private fun setToolBar() {

        binding.mainToolbar.toolbarTitle.text = entryPoint

    }

    private fun setAdapter() {
        claimAdapter = GeneralAdapter(listClaims, object : OnGeneralItemRowClickListener {

            override fun OnGeneralItemRowClickListener(item: GeneralItem) {

            }
        })
        customDialogList.getRecyclerView().adapter = claimAdapter
        customDialogList.getRecyclerView().layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
    }

    private fun itemShelving_addByClaim(shelving: String) {

        binding.splashProgress.visibility = VISIBLE
        viewModel.itemShelving_addByClaim(
            getData(USER),
            getData(PASSWORD),
            listClaims.first().toString(),
            shelving
        )
        customDialogList.dismiss()
    }


    private fun setEvents() {

        binding.mainToolbar.backButton.setOnClickListener {

            customDialogList.dismiss()
            requireActivity().onBackPressed()

        }

    }


    override fun observeViewModel() {


        with(viewModel) {
            binding.splashProgress.visibility = GONE


            responseadd.observe(viewLifecycleOwner, Observer {

                if (it.isError) {
                    customDialogList.dismiss()
                    listClaims.clear()
                    claimAdapter!!.notifyDataSetChanged()
                    showInputClaim()
                    ma.messageWithSound(it.errorMessage, true, false)
                } else {
                    customDialogList.dismiss()
                    ma.messageWithSound(
                        getString(R.string.claimLabel) +listClaims.first().code + getString(R.string.keepPlate) +
                            listClaims.get(
                                1
                            ).code
                        , false, true
                    )
                    listClaims.clear()
                    claimAdapter!!.notifyDataSetChanged()
                    showInputClaim()


                }
                binding.splashProgress.visibility = GONE
            })

        }

    }

    private fun getTextToPosition(TagsScaned: Int): String {
        var message = getString(R.string.dataCompllete)

        when (TagsScaned) {
            1 -> message = getString(R.string.scanShelving)

        }

        return message

    }

    private fun getTextScaned(TagScaned: Int): String {
        var message = ""

        when (TagScaned) {
            1 -> message = getString(R.string.claimLabel)
            2 -> message = getString(R.string.shelving)

        }

        return message

    }

    private fun add_item(itemScaned: String) {

        val nameClaim: String = getTextScaned(listClaims.size + 1) + itemScaned
        customDialogList.setTitle(getTextToPosition(listClaims.size + 1))

        listClaims.add(GeneralItem(itemScaned, nameClaim))
        claimAdapter!!.notifyDataSetChanged()

    }


    private fun showInputClaim() {

        customDialogList.setTitle(getString(R.string.insertClaimCode))
            .setOkButton(getString(R.string.end)) {

                ma.hideKeyboard(customDialogList.getEditText())

                customDialogList.dismiss()
                requireActivity().onBackPressed()


            }.setValue("").show()

        customDialogList.getEditText().requestFocus()
        ma.hideKeyboard(customDialogList.getEditText())

        customDialogList.getEditText().setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0 || actionId == 5 || actionId == 6) {
                if (!customDialogList.getValue().isEmpty()) {

                    //listClaims.add(GeneralItem(customDialogList.getValue(),customDialogList.getValue()))
                    add_item(customDialogList.getValue())

                    if (listClaims.size > 1) {

                        itemShelving_addByClaim(customDialogList.getValue().uppercase())
                    }

                }
                customDialogList.setValue("")
                ma.hideKeyboard(customDialogList.getEditText())
                return@setOnEditorActionListener true
            }
            false
        }

        hideKeyboard()

    }


}



