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

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.FragmentItemproposalBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnItemProposalClickListener
import es.verdnatura.presentation.common.OnOptionsSelectedListener
import es.verdnatura.presentation.common.OnPasillerosItemClickListener
import es.verdnatura.presentation.common.ToolBarAdapterTooltip
import es.verdnatura.presentation.view.feature.articulo.adapter.ItemProposalAdapter
import es.verdnatura.presentation.view.feature.articulo.model.ItemProposal
import es.verdnatura.presentation.view.feature.main.activity.MainActivity
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

class ItemProposalFragment(
    var itemFk: String = ""
) : BaseFragment<FragmentItemproposalBinding, ItemCardViewModel>(
    ItemCardViewModel::class
) {

    private var adapter: ItemProposalAdapter? = null
    private var listProposal: ArrayList<ItemProposal> = ArrayList()
    private var pasillerosItemClickListener: OnPasillerosItemClickListener? = null
    private var hideLoad = true
    private var layoutManager: LinearLayoutManager? = null
    private var filter = "1"

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

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

    override fun onAttach(context: Context) {
        if (context is OnPasillerosItemClickListener) pasillerosItemClickListener = context
        super.onAttach(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        viewModel.item_getSimilar(
            itemFk,
            getData(WAREHOUSEFK),
            vShipped = currentDay(),
            vIsShowedByType = filter,
            user = getData(USER),
            password = getData(PASSWORD)
        )
        super.onCreate(savedInstanceState)
    }

    private fun currentDay(): String {

        val calendar = Calendar.getInstance()
        val format = SimpleDateFormat(getString(R.string.dateFormat),Locale.getDefault())

        return format.format(calendar.time)
    }

    override fun init() {

         ma.hideBottomNavigation(View.GONE)
        binding.splashProgress.visibility = View.VISIBLE
        "${getString(R.string.ArticleSimilar)}:$itemFk".also { it -> it.also { binding.mainToolbar.toolbarTitle.text = it } }
        setToolBar()
        setEvents()

        super.init()
    }

    private fun setToolBar() {
        val listIcons: ArrayList<ImageView> = ArrayList()
        val iconFilter=ImageView(context)
        iconFilter.setImageResource(R.drawable.filter_outline)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            iconFilter.tooltipText = getTooltip(R.drawable.filter_outline)

        }
        resources.getDrawable(R.drawable.filter_outline, resources.newTheme())

        listIcons.add(iconFilter)
        binding.mainToolbar.toolbarIcons.adapter = ToolBarAdapterTooltip(listIcons, object : OnOptionsSelectedListener {
            override fun onOptionsItemSelected(item: Drawable) {
                when (item){
                    iconFilter.drawable-> {
                        binding.splashProgress.visibility = View.VISIBLE
                        iconFilter.drawable.setTint(ContextCompat.getColor(
                            context!!,if (filter=="1")  R.color.verdnatura_white else R.color.verdnatura_pumpkin_orange
                        ))
                       filter = if (filter=="1") "0" else "1"

                        viewModel.item_getSimilar(
                            itemFk,
                            getData(WAREHOUSEFK),
                            vIsShowedByType = filter,
                            vShipped = currentDay(),
                            user = getData(USER),
                            password = getData(PASSWORD)
                           )


                    }


                }


            }

        })
        binding.mainToolbar.toolbarIcons.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)

    }

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

    }


    override fun observeViewModel() {
        with(viewModel) {

            itemProposallist.observe(viewLifecycleOwner,  { it ->
                if (hideLoad) {
                     binding.splashProgress.visibility = View.GONE
                } else {
                    hideLoad = true
                }

                listProposal= ArrayList()
                if (!it.list.isNullOrEmpty()){
                    it.list.forEach {

                        listProposal.add(it)

                    }}


                adapter = ItemProposalAdapter(listProposal, object : OnItemProposalClickListener {
                    override fun onItemProposalClickListener(item: ItemProposal) {

                    }
                }, onPasillerosItemClickListener = pasillerosItemClickListener!!)

                binding.locationRecyclerview.adapter = adapter
                layoutManager =
                    LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
                binding.locationRecyclerview.layoutManager = layoutManager


            })

        }

    }
}


