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

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
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.RunnableArgss;
import it.integry.integrywmsnative.core.model.MtbColt;
import it.integry.integrywmsnative.core.utility.UtilityNumber;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.PickingResiMainListItemBinding;
import it.integry.integrywmsnative.databinding.PickingResiMainListItemHeaderBinding;
import it.integry.integrywmsnative.gest.picking_resi.rest.WithdrawableDtbDocr;

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

    private Context mContext;
    private final List<PickingResiListModel> mDataset = new ArrayList<>();
    private RunnableArgss<WithdrawableDtbDocr, MtbColt> mOnItemClicked;

    static class SubheaderHolder extends RecyclerView.ViewHolder {
        PickingResiMainListItemHeaderBinding mBinding;

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

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {
        PickingResiMainListItemBinding mBinding;

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

    public PickingResiListAdapter(AppCompatActivity context, ObservableArrayList<PickingResiListModel> mutableDataSet) {
        this.mContext = context;

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

    public void setOnItemClicked(RunnableArgss<WithdrawableDtbDocr, MtbColt> onItemClicked) {
        this.mOnItemClicked = onItemClicked;
    }

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

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


    @Override
    public void onBindSubheaderViewHolder(PickingResiListAdapter.SubheaderHolder subheaderHolder, int nextItemPosition) {
        PickingResiListModel pickingResiListModel = this.mDataset.get(nextItemPosition);

        subheaderHolder.mBinding.groupTitle.setVisibility(UtilityString.isNullOrEmpty(pickingResiListModel.getGroupTitle()) ? View.GONE : View.VISIBLE);
        subheaderHolder.mBinding.groupTitle.setText(pickingResiListModel.getGroupTitle());
    }

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

        if (pickingResiListModel.getQtaEvasa().subtract(pickingResiListModel.getQtaTot()).floatValue() >= 0) {
            holder.mBinding.getRoot().setBackgroundColor(mContext.getResources().getColor(R.color.green_500_with_alpha));
        } else if (pickingResiListModel.getQtaEvasa().floatValue() > 0) {
            holder.mBinding.getRoot().setBackgroundColor(mContext.getResources().getColor(R.color.orange_600_with_alpha));
        } else if (position % 2 == 1) {
            holder.mBinding.getRoot().setBackgroundColor(Color.WHITE);
        } else {
            holder.mBinding.getRoot().setBackgroundColor(mContext.getResources().getColor(R.color.letturaFacilitataBG));
        }

        holder.mBinding.deactivatedOverBg.setVisibility(!pickingResiListModel.isActive() ? View.VISIBLE : View.GONE);
        holder.mBinding.getRoot().setAlpha(!pickingResiListModel.isActive() ? 0.8f : 1);
        holder.mBinding.badge1.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), !pickingResiListModel.isActive() ? R.drawable.badge_gray_round_corner : R.drawable.badge1_round_corner, null));
        holder.mBinding.qtaEvasa.setTextColor(ResourcesCompat.getColor(mContext.getResources(), !pickingResiListModel.isActive() ? R.color.gray_600 : R.color.green_700, null));

        holder.mBinding.descrizione.setText(pickingResiListModel.getDescrizione());
        holder.mBinding.descrizione.setTextColor(pickingResiListModel.isDescrizionePresente() ? Color.BLACK : Color.GRAY);


        holder.mBinding.badge1.setText(pickingResiListModel.getBadge1());

        holder.mBinding.qtaEvasa.setText(UtilityNumber.decimalToString(pickingResiListModel.getQtaEvasa()));
        holder.mBinding.qtaTot.setText(UtilityNumber.decimalToString(pickingResiListModel.getQtaTot()));

        holder.mBinding.untMis.setText(pickingResiListModel.getUntMis());
        holder.mBinding.untMis.setVisibility(UtilityString.isNullOrEmpty(pickingResiListModel.getUntMis()) ? View.GONE : View.VISIBLE);

        holder.mBinding.getRoot().setOnClickListener(v -> {
            if (this.mOnItemClicked != null)
                this.mOnItemClicked.run(pickingResiListModel.getOriginalModel(), pickingResiListModel.getSourceMtbColt());
        });
    }

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

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

        return true;
    }


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