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

import android.content.Context
import android.content.SharedPreferences
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import es.verdnatura.R
import es.verdnatura.databinding.BuyersFragmentBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.common.OnAjustesItemClickListener
import es.verdnatura.presentation.common.OnBuyerSelectedListener
import es.verdnatura.presentation.common.OnOptionsSelectedListener
import es.verdnatura.presentation.common.ToolBarAdapterTooltip
import es.verdnatura.presentation.view.component.CustomDialog
import es.verdnatura.presentation.view.feature.ajustes.adapter.AjustesAdapter
import es.verdnatura.presentation.view.feature.ajustes.model.AjustesItemVO
import es.verdnatura.presentation.view.feature.calidad.model.BuyerVO
import es.verdnatura.presentation.view.feature.main.activity.MainActivity


class BuyersFragment : BaseFragment<BuyersFragmentBinding,BuyersViewModel>(BuyersViewModel::class) {

   // private lateinit var customDialog: CustomDialog
    private var onBuyerSelectedListener : OnBuyerSelectedListener? = null
    private var buyersAdapter: AjustesAdapter? = null

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

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

    override fun onAttach(context: Context) {
        if (context is OnBuyerSelectedListener) onBuyerSelectedListener = context
        super.onAttach(context)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        viewModel.itemShelvingBuyerGet(user = getData(USER),password = getData(PASSWORD))
        super.onCreate(savedInstanceState)
    }

    override fun init() {
       // customDialog = CustomDialog(requireContext())
        ma.hideBottomNavigation(View.GONE)
        binding.splashProgress.visibility = View.VISIBLE
        binding.mainToolbar.toolbarTitle.text = getString(R.string.quality)

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

    private fun setToolBar(){


        val listIcons: ArrayList<ImageView> = ArrayList()
        val iconReload = ImageView(context)
        iconReload.setImageResource(R.drawable.ic_autorenew_black_24dp)

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

        }
        listIcons.add(iconReload)

        binding.mainToolbar.toolbarIcons.adapter = ToolBarAdapterTooltip(listIcons,object: OnOptionsSelectedListener {
            override fun onOptionsItemSelected(item: Drawable) {
                when(item){
                    iconReload.drawable->{
                        binding.splashProgress.visibility= View.VISIBLE
                        viewModel.itemShelvingBuyerGet(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) {
            buyersList.observe(viewLifecycleOwner, Observer {
                binding.splashProgress.visibility = View.GONE
                createBuyerList(it.list)
            })
        }
    }

    private fun createBuyerList(list:List<BuyerVO>){
        var buyers = ArrayList<AjustesItemVO>()
        list.forEach { buyer ->
            if (!buyer.isError){
                try {
                    buyers.add(
                        AjustesItemVO(
                            id = buyer.userFk.toInt(),
                            title = buyer.nickname,
                            sectorFk = 0,
                            warehouse = 0,
                        printerFk=0,
                        printerSelected ="",
                            action=false
                        )
                    )
                }catch (e:Exception){}
            }
        }
        buyersAdapter = AjustesAdapter(buyers,object: OnAjustesItemClickListener {
            override fun onAjustesItemClickListener(item: AjustesItemVO) {
                onBuyerSelectedListener?.onBuyerSelected(item.id.toString())
            }
        })
        binding.buyersItems.adapter = buyersAdapter
        binding.buyersItems.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
    }

}