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

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

import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.ObservableArrayList;
import androidx.recyclerview.widget.RecyclerView;

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.FragmentMainAccettazioneBollaListGroupModelBinding;
import it.integry.integrywmsnative.databinding.FragmentMainAccettazioneBollaListModelBinding;

public class MainAccettazioneBolleElencoAdapter extends ExtendedSectionedRecyclerView<MainAccettazioneBolleElencoListModel, MainAccettazioneBolleElencoAdapter.SubheaderHolder, MainAccettazioneBolleElencoAdapter.SingleItemViewHolder> {

    private final Context mContext;
    private RunnableArgs<String> mOnGroupItemClicked;
    private RunnableArgs<MainAccettazioneBolleElencoListModel> mOnItemChecked;

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        FragmentMainAccettazioneBollaListModelBinding mBinding;

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

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentMainAccettazioneBollaListGroupModelBinding mBinding;

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

    public MainAccettazioneBolleElencoAdapter(Context context, ObservableArrayList<MainAccettazioneBolleElencoListModel> mutableDataSet) {
        super(mutableDataSet);
        mContext = context;
    }

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

    public MainAccettazioneBolleElencoAdapter setOnItemChecked(RunnableArgs<MainAccettazioneBolleElencoListModel> mOnItemChecked) {
        this.mOnItemChecked = mOnItemChecked;
        return this;
    }

    @Override
    public MainAccettazioneBolleElencoAdapter.SubheaderHolder onCreateSubheaderViewHolder(ViewGroup parent, int viewType) {
        FragmentMainAccettazioneBollaListModelBinding binding = FragmentMainAccettazioneBollaListModelBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new MainAccettazioneBolleElencoAdapter.SubheaderHolder(binding);
    }

    @Override
    public MainAccettazioneBolleElencoAdapter.SingleItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        FragmentMainAccettazioneBollaListGroupModelBinding binding = FragmentMainAccettazioneBollaListGroupModelBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new MainAccettazioneBolleElencoAdapter.SingleItemViewHolder(binding);
    }

    @Override
    public void onBindSubheaderViewHolder(MainAccettazioneBolleElencoAdapter.SubheaderHolder subheaderHolder, int nextItemPosition) {
        MainAccettazioneBolleElencoListModel pickingObjectDTO = this.mDataset.get(nextItemPosition);

        subheaderHolder.mBinding.groupTitle.setVisibility(UtilityString.isNullOrEmpty(pickingObjectDTO.getGroupTitle()) ? View.GONE : View.VISIBLE);
        subheaderHolder.mBinding.groupTitle.setText(Html.fromHtml(pickingObjectDTO.getGroupTitle()));

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

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

        listModel.getSelectedObservable().resetOnPropertyChangedCallback();

        if(listModel.getEtichettaColor() != null)
            holder.mBinding.emptyView.setBackgroundColor(listModel.getEtichettaColor());
        else holder.mBinding.emptyView.setBackgroundColor(ResourcesCompat.getColor(mContext.getResources(), android.R.color.transparent, null));

        if(!UtilityString.isNullOrEmpty(listModel.getDescription())) {
            holder.mBinding.descrizione.setText(Html.fromHtml(listModel.getDescription()));
            holder.mBinding.descrizione.setVisibility(View.VISIBLE);
        } else holder.mBinding.descrizione.setVisibility(View.GONE);

        if(!UtilityString.isNullOrEmpty(listModel.getSubDescription())) {
            holder.mBinding.subDescrizione.setText(Html.fromHtml(listModel.getSubDescription()));
            holder.mBinding.subDescrizione.setVisibility(View.VISIBLE);
        } else holder.mBinding.subDescrizione.setVisibility(View.GONE);

        if(!UtilityString.isNullOrEmpty(listModel.getRightDescription())) {
            holder.mBinding.rightDescrizione.setText(Html.fromHtml(listModel.getRightDescription()));
            holder.mBinding.rightDescrizione.setVisibility(View.VISIBLE);
        } else holder.mBinding.rightDescrizione.setVisibility(View.GONE);

        if(!UtilityString.isNullOrEmpty(listModel.getRightSubDescription())) {
            holder.mBinding.rightSubDescrizione.setText(Html.fromHtml(listModel.getRightSubDescription()));
            holder.mBinding.rightSubDescrizione.setVisibility(View.VISIBLE);
        } else holder.mBinding.rightSubDescrizione.setVisibility(View.GONE);

        holder.mBinding.setSelected(listModel.getSelectedObservable());
        holder.mBinding.executePendingBindings();
        holder.mBinding.checkbox.jumpDrawablesToCurrentState();

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

        holder.mBinding.getRoot().setOnClickListener(v -> {
            listModel.getSelectedObservable().set(!listModel.getSelectedObservable().get());
        });
    }
    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {

        if (getItemSize() == 1) return true;
        else if (getItemSize() > 1) {
            MainAccettazioneBolleElencoListModel compare1 = this.mDataset.get(position);
            MainAccettazioneBolleElencoListModel compare2 = this.mDataset.get(position + 1);

            return !UtilityString.equalsIgnoreCase(compare1.getGroupTitle(), compare2.getGroupTitle());
        }

        return true;
    }
}
