package it.integry.integrywmsnative.gest.accettazione.ui;

import android.content.Context;
import android.text.Html;
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 it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.OnSingleClickListener;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.expansion.view.ExtendedSectionedRecyclerView;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.AccettazioneMainListGroupClientiBinding;
import it.integry.integrywmsnative.databinding.AccettazioneMainListGroupModelBinding;
import it.integry.integrywmsnative.databinding.AccettazioneMainListModelBinding;

public class MainListAccettazioneAdapter extends ExtendedSectionedRecyclerView<MainListAccettazioneListModel, MainListAccettazioneAdapter.SubheaderHolder, MainListAccettazioneAdapter.SingleItemViewHolder> {

    private Context mContext;

    private RunnableArgs<String> mOnGroupItemClicked;
    private RunnableArgs<MainListAccettazioneListModel> mOnItemChecked;

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        AccettazioneMainListModelBinding binding;

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

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        AccettazioneMainListGroupModelBinding binding;

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

    public MainListAccettazioneAdapter(Context context, ObservableArrayList<MainListAccettazioneListModel> myDataset) {
        super(myDataset);
        mContext = context;
    }

    public MainListAccettazioneAdapter setOnGroupItemClicked(RunnableArgs<String> onGroupItemClicked) {
        this.mOnGroupItemClicked = onGroupItemClicked;
        return this;
    }

    public MainListAccettazioneAdapter setOnItemChecked(RunnableArgs<MainListAccettazioneListModel> onItemChecked) {
        this.mOnItemChecked = onItemChecked;
        return this;
    }


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

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


    @Override
    public void onBindSubheaderViewHolder(SubheaderHolder subheaderHolder, int nextItemPosition) {
        subheaderHolder.binding.accettazioneMainListGroupHeader.setText(mDataset.get(nextItemPosition).getGroupTitle());

        subheaderHolder.binding.getRoot().setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View v) {
                if(mOnGroupItemClicked != null) mOnGroupItemClicked.run(mDataset.get(nextItemPosition).getGroupTitle());
            }
        });
    }

    @Override
    public void onBindItemViewHolder(SingleItemViewHolder holder, int itemPosition) {
        final MainListAccettazioneListModel ordine = mDataset.get(itemPosition);

        ordine.getSelectedObservable().resetOnPropertyChangedCallback();

        holder.binding.accettazioneMainListGroupItemContainerClientiOrd.removeAllViews();
        holder.binding.accettazioneMainListGroupItemContainerTestataOrd.setText(Html.fromHtml(ordine.getDescription()));

        for (MainListAccettazioneClienteListModel cliente : ordine.getClientiListModel()) {
            AccettazioneMainListGroupClientiBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.accettazione_main_list_group_clienti, holder.binding.accettazioneMainListGroupItemContainerClientiOrd, false);

            binding.accettazioneMainListGroupClientiComm.setText(cliente.getPrimaryText());

            if (!UtilityString.isNullOrEmpty(cliente.getRightText())) {
                binding.accettazioneMainListGroupClientiDatacons.setText(cliente.getRightText());
            } else {
                binding.accettazioneMainListGroupClientiDatacons.setText("");
            }

            holder.binding.accettazioneMainListGroupItemContainerClientiOrd.addView(binding.getRoot());
        }

        holder.binding.setSelected(ordine.getSelectedObservable());
        holder.binding.executePendingBindings();
        holder.binding.accettazioneMainListGroupItemContainerCheckBox.jumpDrawablesToCurrentState();

        ordine.getSelectedObservable().addOnPropertyChangedCallback(() -> {
            if(this.mOnItemChecked != null) this.mOnItemChecked.run(ordine);
        });

        holder.binding.getRoot().setOnClickListener(v -> {
            ordine.getSelectedObservable().set(!ordine.getSelectedObservable().get());
        });

    }


    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {
        if (getItemSize() == 1) return true;
        else if (getItemSize() > 1) {
            MainListAccettazioneListModel compare1 = this.mDataset.get(position);
            MainListAccettazioneListModel compare2 = this.mDataset.get(position + 1);

            if (UtilityString.equalsIgnoreCase(compare1.getGroupTitle(), compare2.getGroupTitle())) {
                return false;
            }
        }

        return true;
    }

    @Override
    public int getItemSize() {
        return mDataset.size();
    }

}
