package it.integry.integrywmsnative.gest.prod_rientro_merce.order_list;

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

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import androidx.recyclerview.widget.RecyclerView;

import com.zhukic.sectionedrecyclerview.SectionedRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import it.integry.integrywmsnative.R;
import it.integry.integrywmsnative.core.expansion.OnListGeneralChangedCallback;
import it.integry.integrywmsnative.core.expansion.OnSingleClickListener;
import it.integry.integrywmsnative.core.expansion.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.FragmentProdRientroMerceOrderListListGroupModelBinding;
import it.integry.integrywmsnative.databinding.FragmentProdRientroMerceOrderListListModelBinding;

public class ProdRientroMerceOrderListAdapter extends SectionedRecyclerViewAdapter<ProdRientroMerceOrderListAdapter.SubheaderHolder, ProdRientroMerceOrderListAdapter.SingleItemViewHolder> {

    private final Context mContext;
    private final List<ProdRientroMerceOrderListModel> mDataset = new ArrayList<>();
    private View mEmptyView;

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

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        FragmentProdRientroMerceOrderListListModelBinding mBinding;

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

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        FragmentProdRientroMerceOrderListListGroupModelBinding mBinding;

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

    public ProdRientroMerceOrderListAdapter(Context context, ObservableArrayList<ProdRientroMerceOrderListModel> mutableDataSet) {
        this.mContext = context;

        mutableDataSet.addOnListChangedCallback(new OnListGeneralChangedCallback() {
            @Override
            public void onChanged(ObservableList sender) {
                mDataset.clear();
                mDataset.addAll(sender);
                notifyDataSetChanged();
                notifyDataChanged();
                checkIfEmpty();
            }
        });
    }

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

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

    public ProdRientroMerceOrderListAdapter setEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
        this.checkIfEmpty();
        return this;
    }

    private void checkIfEmpty() {
        if (mEmptyView != null) {
            final boolean emptyViewVisible = getItemCount() == 0;
            mEmptyView.setVisibility(emptyViewVisible ? View.VISIBLE : View.GONE);
        }
    }

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

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

    @Override
    public void onBindSubheaderViewHolder(ProdRientroMerceOrderListAdapter.SubheaderHolder subheaderHolder, int nextItemPosition) {
        ProdRientroMerceOrderListModel 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 ProdRientroMerceOrderListAdapter.SingleItemViewHolder holder, final int position) {
        ProdRientroMerceOrderListModel listModel = this.mDataset.get(position);

        switch (listModel.getIcon()) {
            case START:
                holder.mBinding.ordineStateIcon.setImageResource(R.drawable.ic_play_circle_filled_24dp);
                holder.mBinding.ordineStateIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.green_800), android.graphics.PorterDuff.Mode.SRC_IN);
                break;
            case PAUSE:
                holder.mBinding.ordineStateIcon.setImageResource(R.drawable.ic_pause_circle_filled_24dp);
                holder.mBinding.ordineStateIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.yellow_800), android.graphics.PorterDuff.Mode.SRC_IN);
                break;
            case SCHEDULED:
            default:
                holder.mBinding.ordineStateIcon.setImageResource(R.drawable.ic_clock_circle_outline_24dp);
                holder.mBinding.ordineStateIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.blue_700), android.graphics.PorterDuff.Mode.SRC_IN);
                break;
        }

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

        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.executePendingBindings();

        holder.mBinding.getRoot().setOnClickListener(v -> {
            if (this.mOnItemChecked != null) this.mOnItemChecked.run(listModel);
        });
    }

    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {

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

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

        return true;
    }

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

}