package it.integry.integrywmsnative.gest.accettazione_ordini_picking.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.RunnableArgs;
import it.integry.integrywmsnative.core.utility.UtilityDate;
import it.integry.integrywmsnative.core.utility.UtilityNumber;
import it.integry.integrywmsnative.core.utility.UtilityString;
import it.integry.integrywmsnative.databinding.AccettazioneOrdineInevasoMainListGroupHeaderBinding;
import it.integry.integrywmsnative.databinding.AccettazioneOrdineInevasoMainListGroupItemBinding;
import it.integry.integrywmsnative.gest.accettazione_ordini_picking.dto.PickingObjectDTO;

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

    private final Context mContext;
    private final boolean mShowSecondaryUntMis;
    private final List<AccettazioneOrdiniPickingListModel> mDataset = new ArrayList<>();

    private RunnableArgs<PickingObjectDTO> mOnItemClicked;

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        AccettazioneOrdineInevasoMainListGroupHeaderBinding mBinding;

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

    }

    static class SingleItemViewHolder extends RecyclerView.ViewHolder {

        AccettazioneOrdineInevasoMainListGroupItemBinding mBinding;

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


    public AccettazioneOrdiniPickingListAdapter(AppCompatActivity context, ObservableArrayList<AccettazioneOrdiniPickingListModel> mutableDataSet, boolean showSecondaryUntMis) {
        this.mContext = context;
        this.mShowSecondaryUntMis = showSecondaryUntMis;

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

    public void setOnItemClicked(RunnableArgs<PickingObjectDTO> onItemClicked) {
        this.mOnItemClicked = onItemClicked;
    }


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

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


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

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

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

        if (pickingObjectDTO.getQtaEvasa().subtract(pickingObjectDTO.getQtaTot()).floatValue() >= 0) {
            holder.mBinding.getRoot().setBackgroundColor(mContext.getResources().getColor(R.color.green_500_with_alpha));
        } else if (pickingObjectDTO.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(!pickingObjectDTO.isActive() ? View.VISIBLE : View.GONE);
        holder.mBinding.getRoot().setAlpha(!pickingObjectDTO.isActive() ? 0.8f : 1);
        holder.mBinding.badge1.setBackground(ResourcesCompat.getDrawable(mContext.getResources(), !pickingObjectDTO.isActive() ? R.drawable.badge_round_corner : R.drawable.badge1_round_corner, null));
        holder.mBinding.qtaEvasa.setTextColor(ResourcesCompat.getColor(mContext.getResources(), !pickingObjectDTO.isActive() ? R.color.gray_600 : R.color.green_700, null));

        holder.mBinding.descrizione.setText(pickingObjectDTO.getDescrizione());
        holder.mBinding.descrizione.setTextColor(pickingObjectDTO.isDescrizionePresente() ? Color.BLACK : Color.GRAY);
        holder.mBinding.descrizione.setVisibility(UtilityString.isNullOrEmpty(pickingObjectDTO.getDescrizione()) ? View.GONE : View.VISIBLE);

        holder.mBinding.descrizioneSub1.setText(pickingObjectDTO.getSubDescrizione1());
        holder.mBinding.descrizioneSub1.setVisibility(UtilityString.isNullOrEmpty(pickingObjectDTO.getSubDescrizione1()) ? View.GONE : View.VISIBLE);

        holder.mBinding.badge1.setText(pickingObjectDTO.getBadge1());
        holder.mBinding.badge2.setText(pickingObjectDTO.getBadge2());
        holder.mBinding.badge2.setVisibility(UtilityString.isNullOrEmpty(pickingObjectDTO.getBadge2()) ? View.GONE : View.VISIBLE);

        holder.mBinding.badge3.setText(pickingObjectDTO.getPosizione());
        holder.mBinding.badge3Layout.setVisibility(UtilityString.isNullOrEmpty(pickingObjectDTO.getPosizione()) ? View.GONE : View.VISIBLE);


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

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

        //Secondary Unt Mis
        holder.mBinding.secondaryUntMis.setVisibility(mShowSecondaryUntMis ? View.VISIBLE : View.GONE);

        if(mShowSecondaryUntMis) {
            holder.mBinding.secQtaEvasa.setText(UtilityNumber.decimalToString(pickingObjectDTO.getSecQtaEvasa()));
            holder.mBinding.secQtaTot.setText(UtilityNumber.decimalToString(pickingObjectDTO.getSecQtaTot()));

            holder.mBinding.secUntMis.setText(pickingObjectDTO.getSecUntMis());
            holder.mBinding.secUntMis.setVisibility(UtilityString.isNullOrEmpty(pickingObjectDTO.getSecUntMis()) ? View.GONE : View.VISIBLE);
        }

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

    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {

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

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

        return true;
    }

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

}
