package com.dbs.consent.demo.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.dbs.consent.demo.BuildConfig;
import com.dbs.consent.demo.R;
import com.dbs.consent.demo.model.ConsentImageVO;
import com.github.chrisbanes.photoview.PhotoView;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ConsentImageAdapter extends RecyclerView.Adapter<ConsentImageAdapter.ViewHolder> {

  private static final String TAG = "ConsentImageAdapter";
  private ArrayList<ConsentImageVO> arrayList;

  public ConsentImageAdapter(ArrayList<ConsentImageVO> arrayList) {
    this.arrayList = arrayList;

  }

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

  public class ViewHolder extends RecyclerView.ViewHolder {
    public PhotoView ivConsent;

    public ViewHolder(@NonNull View itemView) {
      super(itemView);
    }
  }

  @NonNull
  @Override
  public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.consent_image_list_item, parent, false);
    ViewHolder holder = new ViewHolder(view);

    holder.ivConsent = (PhotoView) view.findViewById(R.id.ivConsent);

    return holder;
  }

  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ConsentImageVO consentImageList = arrayList.get(position);
    String imagePath     = consentImageList.getImagePath();
    String imageFileName = consentImageList.getImageFileName();
//    String imageUrl      = BuildConfig.CONSENT_SERVER_URL + imagePath + "/" + imageFileName;
    String imageUrl      = BuildConfig.CONSENT_SERVER_URL + "UPLOAD" + "/" + imagePath + "/" + imageFileName;
    Log.e(TAG, "imageUrl -- > " + imageUrl);

    Picasso.get().load(imageUrl).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(holder.ivConsent);
  }

}
