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

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.inputmethod.EditorInfo
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentExpeditionTruckListBinding
import es.verdnatura.domain.isoToString
import es.verdnatura.domain.notNull
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnOptionsSelectedListener
import es.verdnatura.presentation.common.OnTruckClickListener
import es.verdnatura.presentation.view.component.CustomDialogHour
import es.verdnatura.presentation.view.feature.inventario.adapter.ToolBarAdapter
import es.verdnatura.presentation.view.feature.paletizador.adapter.ExpeditionListAdapter
import es.verdnatura.presentation.view.feature.paletizador.model.ItemExpeditionTruckList
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@Suppress("UNUSED_ANONYMOUS_PARAMETER")
class ExpeditionTruckListFragment :
    BaseFragment<FragmentExpeditionTruckListBinding, ExpeditionTruckListViewModel>(
        ExpeditionTruckListViewModel::class
    ) {

    private var adapter: ExpeditionListAdapter? = null
    private lateinit var customDialogHor: CustomDialogHour
    private var onTruckClickListener: OnTruckClickListener? = null

    companion object {
        fun newInstance() = ExpeditionTruckListFragment()
    }

    override fun onAttach(context: Context) {
        if (context is OnTruckClickListener) onTruckClickListener = context
        super.onAttach(context)
    }

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

    @SuppressLint("SetTextI18n")
    override fun init() {
        customDialogHor = CustomDialogHour(requireContext())
        binding.mainToolbar.toolbarTitle.text = getString(R.string.psScan) + LocalDate.now()
            .format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))
        binding.mainToolbar.backButton.setOnClickListener {
            ma.onMyBackPressed()
        }

        setToolBar()
        viewModel.expeditionTruckList()
        super.init()
    }

    private fun setToolBar() {
        val listIcons: ArrayList<Drawable> = ArrayList()
        val iconReload: Drawable =
            ResourcesCompat.getDrawable(resources, R.drawable.ic_autorenew_black_24dp, null)!!
        val iconPlus: Drawable =
            ResourcesCompat.getDrawable(resources, R.drawable.ic_add_black_24dp, null)!!
        val iconState: Drawable =
            ResourcesCompat.getDrawable(resources, R.drawable.ic_visibility_white_24dp, null)!!

        listIcons.add(iconState)
        listIcons.add(iconReload)
        listIcons.add(iconPlus)


        binding.mainToolbar.toolbarIcons.adapter =
            ToolBarAdapter(listIcons, object : OnOptionsSelectedListener {
                override fun onOptionsItemSelected(item: Drawable) {
                    if (item == iconReload) {
                        viewModel.expeditionTruckList()
                    } else if (item == iconPlus) {
                        addTruck()
                    }
                    if (item == iconState) {
                        ma.openFragmentExpeditionState()
                    }
                }
            })
        binding.mainToolbar.toolbarIcons.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
    }

    override fun observeViewModel() {
        with(viewModel) {
            loadExpeditionTruckList.observe(viewLifecycleOwner) { event ->
                event.getContentIfNotHandled().notNull { printExpeditionList(it) }
            }

        }
    }

    private fun printExpeditionList(it: ItemExpeditionTruckList) {
        val lm = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        it.list.map { it.eta = it.eta.isoToString() }
        adapter = ExpeditionListAdapter(it.list, onTruckClickListener!!)
        binding.expeditionTruckRecyclerview.adapter = adapter
        binding.expeditionTruckRecyclerview.layoutManager = lm
    }

    private fun addTruck() {
        customDialogHor.setTitle(getString(R.string.newTruck))
            .setOkButton(getString(R.string.save)) {
                ma.hideKeyboard(customDialogHor.getDestinoEditText())
                if (customDialogHor.getDestinoValue().isNotEmpty() && customDialogHor.getHoraValue()
                        .isNotEmpty()
                ) {
                    customDialogActionTruck()
                }
                ma.hideKeyboard(customDialogHor.getDestinoEditText())
                customDialogHor.dismiss()
            }.setKoButton(getString(R.string.cancel)) {
                customDialogHor.dismiss()
            }.setHoraValue("").setDestinoValue("").show()

        customDialogHor.getDestinoEditText().requestFocus()
        customDialogHor.getDestinoEditText().setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || actionId == 0) {
                if (customDialogHor.getDestinoValue().isNotEmpty() && customDialogHor.getHoraValue()
                        .isNotEmpty()
                ) {
                    customDialogActionTruck()
                }

                return@setOnEditorActionListener true
            }
            ma.hideKeyboard(customDialogHor.getDestinoEditText())
            false
        }
    }

    private fun customDialogActionTruck() {
        viewModel.roadMapStopAdd(
            customDialogHor.getHoraValue(), customDialogHor.getDestinoValue()
        )
        customDialogHor.dismiss()
    }

}