package com.cpm.lorealbaautomation.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.cpm.lorealbaautomation.R;
import com.cpm.lorealbaautomation.gsonGetterSetter.countervolune.ReportCounterVolumeDetail;

import java.util.List;

public class VolumeDetailAdapter extends RecyclerView.Adapter<VolumeDetailAdapter.MyViewHolder> {
    LayoutInflater inflator;
    List<ReportCounterVolumeDetail> data;

    public VolumeDetailAdapter(Context context, List<ReportCounterVolumeDetail> data) {
        inflator = LayoutInflater.from(context);
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view = inflator.inflate(R.layout.row_counter_volume, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint({"SetTextI18n", "DefaultLocale"})
    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        final ReportCounterVolumeDetail current = data.get(position);
        setText(holder.tvParamCode, position, current.getParamCode(), inflator.getContext());
        setText(holder.tvParamName, position, current.getParamName(), inflator.getContext());
        setText(holder.tvRMTDVolume, position, current.getMTDVolume() != null ? current.getMTDVolume().toString() : "0", inflator.getContext());
        setText(holder.tvAvgPerDayVolume, position, current.getAvgPerDayVolume() != null ? current.getAvgPerDayVolume().toString() : "0", inflator.getContext());

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvAvgPerDayVolume, tvRMTDVolume, tvParamCode, tvParamName;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvAvgPerDayVolume = (TextView) itemView.findViewById(R.id.tvAvgPerDayVolume);
            tvRMTDVolume = (TextView) itemView.findViewById(R.id.tvRMTDVolume);
            tvParamCode = (TextView) itemView.findViewById(R.id.tvParamCode);
            tvParamName = (TextView) itemView.findViewById(R.id.tvParamName);
        }
    }

    @SuppressLint("DefaultLocale")
    private void setText(TextView tv, int pos, String value, Context context) {
        tv.setTextColor(ContextCompat.getColor(context, R.color.black));
        tv.setText(value);
        tv.setId(pos);
    }
}
