package it.integry.integrywmsnative.gest.ultime_consegne_cliente;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Calendar;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.view.ExtendedSectionedRecyclerView;
import it.integry.integrywmsnative.core.rest.model.DocumentoResoDTO;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.core.utility.UtilityExceptions;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.FragmentMainUltimeConsegneClienteListHeaderBinding;
import it.integry.integrywmsnative.databinding.FragmentMainUltimeConsegneClienteListSingleItemBinding;

public class UltimeConsegneMainListAdapter extends ExtendedSectionedRecyclerView<DocumentoResoDTO, UltimeConsegneMainListAdapter.SubheaderHolder, UltimeConsegneMainListAdapter.SingleItemViewHolder> {

    private Context mContext;
    private RunnableArgs<DocumentoResoDTO> mOnSelectionChanged;



    static class SubheaderHolder extends RecyclerView.ViewHolder {

        FragmentMainUltimeConsegneClienteListHeaderBinding mBinding;

        SubheaderHolder(FragmentMainUltimeConsegneClienteListHeaderBinding binding) {
            super(binding.getRoot());
            this.mBinding = binding;
        }

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentMainUltimeConsegneClienteListSingleItemBinding mBinding;

        SingleItemViewHolder(FragmentMainUltimeConsegneClienteListSingleItemBinding binding) {
            super(binding.getRoot());
            this.mBinding = binding;
        }
    }




    public UltimeConsegneMainListAdapter(Context context, ObservableArrayList<DocumentoResoDTO> mutableDataSet) {
        super(mutableDataSet);
        this.mContext = context;
    }


    @Override
    public SingleItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        FragmentMainUltimeConsegneClienteListSingleItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fragment_main_ultime_consegne_cliente__list_single_item, parent, false);
        return new SingleItemViewHolder(binding);
    }

    @Override
    public SubheaderHolder onCreateSubheaderViewHolder(ViewGroup parent, int viewType) {
        FragmentMainUltimeConsegneClienteListHeaderBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.fragment_main_ultime_consegne_cliente__list_header, parent, false);
        return new SubheaderHolder(binding);
    }

    @Override
    public void onBindItemViewHolder(final SingleItemViewHolder holder, final int position) {
        final DocumentoResoDTO consegna = this.mDataset.get(position);

        holder.mBinding.setConsegna(consegna);

        holder.mBinding.descriptionMain.setText(UtilityString.isNullOrEmpty(consegna.getIndirizzo()) ? "" : consegna.getIndirizzo());

        if(consegna.getDataInizTraspD() != null) {
            holder.mBinding.subDescriptionMain.setText(String.format(mContext.getText(R.string.shipped_on).toString(), UtilityDate.formatDate(consegna.getDataInizTraspD(), UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN)));
        }

        holder.mBinding.badge1.setText(String.valueOf(consegna.getNumDoc()));

        try {
            Calendar calendarNow = UtilityDate.getCalendarInstance();
            Calendar calendarDataDoc = UtilityDate.getCalendarInstance();
            calendarDataDoc.setTime(consegna.getDataDocD());

            String dataDocString = "";

            if(calendarDataDoc.get(Calendar.YEAR) == calendarNow.get(Calendar.YEAR)) {
                dataDocString = UtilityDate.formatDate(consegna.getDataDocD(), UtilityDate.COMMONS_DATE_FORMATS.DM_HUMAN);
            } else {
                dataDocString = UtilityDate.formatDate(consegna.getDataDocD(), UtilityDate.COMMONS_DATE_FORMATS.DMY_HUMAN);
            }

            holder.mBinding.date.setText(dataDocString);


            holder.mBinding.compilatoDa.setText(consegna.getCompilatoDa());
            holder.mBinding.compilatoDa.setVisibility(UtilityString.isNullOrEmpty(consegna.getCompilatoDa()) ? View.GONE : View.VISIBLE);
        } catch (Exception ex) {
            UtilityExceptions.defaultException(mContext, ex);
        }


        holder.mBinding.getRoot().setOnClickListener(v -> {
            consegna.setSelected(!consegna.isSelected());

            if(this.mOnSelectionChanged != null) mOnSelectionChanged.run(consegna);
        });

    }

    @Override
    public void onBindSubheaderViewHolder(SubheaderHolder subheaderHolder, int nextItemPosition) {
        subheaderHolder.mBinding.title.setText(this.mDataset.get(nextItemPosition).getRagSoc());
    }

    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {

        if(!this.mDataset.get(position).getCodAnag().equalsIgnoreCase(this.mDataset.get(position+1).getCodAnag())) {
            return true;
        }

        return false;
    }


    public void setOnSelectionChanged(RunnableArgs<DocumentoResoDTO> mSelectionChanged) {
        this.mOnSelectionChanged = mSelectionChanged;
    }

}
