package es.verdnatura.presentation.view.feature.delivery.fragments

import android.view.View
import es.verdnatura.R
import es.verdnatura.databinding.FragmentInfoDeliveryBinding
import es.verdnatura.presentation.base.BaseFragment
import es.verdnatura.presentation.view.feature.delivery.viewmodels.DeliveryViewModel

class InfoFragment(var title: String) :
    BaseFragment<FragmentInfoDeliveryBinding, DeliveryViewModel>(DeliveryViewModel::class) {
    companion object {
        fun newInstance(title: String) = InfoFragment(title)
    }

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

    override fun init() {
        super.init()
        binding.mainToolbar.toolbarTitle.text = title
        ma.hideBottomNavigation(View.GONE)
        viewModel.companyGetFiscaldata(mobileApplication.userId!!)
        viewModel.getInfoFreelance(mobileApplication.userId!!)
        setEvents()
    }

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

    override fun observeViewModel() {
        with(viewModel) {

            companyInfoList.observe(viewLifecycleOwner) {
                if (it.list.isNotEmpty()) {
                    binding.itemName.text = it.list[0]?.name
                    binding.itemNif.text = it.list[0]?.nif
                    binding.itemAdress.text = it.list[0]?.street + "-" + it.list[0]?.city
                }

            }
            freelanceInfoList.observe(viewLifecycleOwner) {
                if (it.list.isNotEmpty()) {
                    binding.linearInfoFreelance.visibility=View.VISIBLE
                    binding.freelanceName.text = it.list[0]?.name
                    binding.freelanceNif.text = it.list[0]?.nif
                    binding.freelanceAddress.text = it.list[0]?.street + "-" + it.list[0]?.city
                }
            }
        }

    }
}
