package it.integry.integrywmsnative.gest.ordini_uscita_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.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.FragmentMainOrdiniUscitaListGroupModelBinding;
import it.integry.integrywmsnative.databinding.FragmentMainOrdiniUscitaListModelBinding;

public class OrdiniUscitaElencoAdapter extends ExtendedSectionedRecyclerView<OrdiniUscitaElencoListModel, OrdiniUscitaElencoAdapter.SubheaderHolder, OrdiniUscitaElencoAdapter.SingleItemViewHolder> {

    private Context mContext;
    private RunnableArgs<String> mOnGroupItemClicked;
    private RunnableArgs<OrdiniUscitaElencoListModel> mOnItemChecked;

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        FragmentMainOrdiniUscitaListModelBinding mBinding;

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

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentMainOrdiniUscitaListGroupModelBinding mBinding;

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

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

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

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


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

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

    @Override
    public void onBindSubheaderViewHolder(OrdiniUscitaElencoAdapter.SubheaderHolder subheaderHolder, int nextItemPosition) {
        OrdiniUscitaElencoListModel 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 OrdiniUscitaElencoAdapter.SingleItemViewHolder holder, final int position) {
        OrdiniUscitaElencoListModel 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) {
            OrdiniUscitaElencoListModel compare1 = this.mDataset.get(position);
            OrdiniUscitaElencoListModel compare2 = this.mDataset.get(position + 1);

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

        return true;
    }

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

}
