package com.utopiaindustries.qualitychecker.ui.adapter;

import static com.utopiaindustries.qualitychecker.ui.adapter.VocAdapter.*;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.utopiaindustries.qualitychecker.R;
import com.utopiaindustries.qualitychecker.models.VoiceOfCustomer;

import java.util.List;

public class VocAdapter extends
        RecyclerView.Adapter<VocViewHolder> {

    private final List<VoiceOfCustomer> vocs;
    private final Context context;

    public VocAdapter (List<VoiceOfCustomer> vocs, Context context) {
        this.vocs = vocs;
        this.context = context;
    }

    @NonNull
    @Override
    public VocViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.voc_item, parent, false);
        return new VocViewHolder( view , context);
    }

    @Override
    public void onBindViewHolder(@NonNull VocViewHolder holder, int position) {
        VoiceOfCustomer voc = vocs.get( position );
        holder.bind( voc );
    }

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

    public static class VocViewHolder extends RecyclerView.ViewHolder {

        private final TextView asin, fnsku,health,item,markeplace;
        private final ImageView imageView;
        private final Context context;

        public VocViewHolder(@NonNull View itemView, Context context) {
            super(itemView);
            this.context = context;
            asin = itemView.findViewById( R.id.asin );
            fnsku = itemView.findViewById( R.id.fnsku );
            health = itemView.findViewById( R.id.health );
            item = itemView.findViewById( R.id.item );
            markeplace = itemView.findViewById( R.id.marketplace );
            imageView = itemView.findViewById( R.id.voc_item_imageview );
        }

        public void bind( VoiceOfCustomer voc ){
            asin.setText( voc.getAsin() );
            fnsku.setText( voc.getFnsku() );
            health.setText( voc.getPcxHealth() );
            item .setText( voc.getMostCommonReturnReasonBucket() );
            markeplace.setText( voc.getMarketplace() );
            Glide.with( context  )
                 .load( voc.getImageUrl() )
                 .into( imageView );

        }
    }
}
