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

import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentHistoricovehiculoBinding
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.feature.historicovehiculo.adapter.HistoricoVehiculoAdapter
import es.verdnatura.presentation.view.feature.historicovehiculo.model.ItemHistoricoVehiculoList
import java.text.SimpleDateFormat
import java.util.*


class HistoricoVehiculoFragment(
    var numberPlate: String = ""
) : BaseFragment<FragmentHistoricovehiculoBinding, HistoricoVehiculoViewModel>(
    HistoricoVehiculoViewModel::class
) {

    private var adapter: HistoricoVehiculoAdapter? = null

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

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


    override fun init() {

        ma.hideBottomNavigation(View.GONE)

        viewModel.machineWorker_getHistorical(getData(USER), getData(PASSWORD), numberPlate)
        binding.mainToolbar.toolbarTitle.text = getString(R.string.vehiclediary)
        setEvents()
        super.init()
    }


    private fun setEvents() {
        binding.mainToolbar.backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }
    }

    override fun observeViewModel() {
        with(viewModel) {
            loadHistoricoVehiculoList.observe(viewLifecycleOwner, Observer { event ->
                event.getContentIfNotHandled().notNull {
                    binding.splashProgress.visibility = View.GONE
                    adapter = HistoricoVehiculoAdapter(it.list)
                    binding.historicovehiculoRecyclerview.adapter = adapter
                    binding.historicovehiculoRecyclerview.layoutManager =
                        LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
                    navigateToToday(it)

                    if (it.list.isEmpty()) {
                        ma.messageWithSound(
                            getString(R.string.lookWithpermission), true, true, getString(
                                R.string.info
                            )
                        )
                    } else {
                        if (it.list[0].isError) {
                            ma.messageWithSound(
                                it.list[0].errorMessage, true, true, getString(
                                    R.string.info
                                )
                            )

                        }
                    }

                }
            })
        }
    }

    private fun navigateToToday(it: ItemHistoricoVehiculoList) {
        var positionToday = 0
        val sdf = SimpleDateFormat(getString(R.string.dateFormat))
        val currentDate = sdf.format(Date())

        for (item in it.list) {
            if (currentDate == item.inTimed) {
                break
            }
            positionToday += 1
        }

        binding.historicovehiculoRecyclerview.scrollToPosition(positionToday)
    }


}