package com.dbs.mplus.fatima.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.mplus.fatima.BuildConfig;
import com.dbs.mplus.fatima.R;
import com.dbs.mplus.fatima.model.ConsentImageListVO;
import com.dbs.mplus.fatima.util.ConsentConfig;
import com.dbs.mplus.fatima.util.Util;
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<ConsentImageListVO> arrayList;

  public ConsentImageAdapter(ArrayList<ConsentImageListVO> 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) {
    ConsentImageListVO consentImageListVO = arrayList.get(position);
    String imagePath = consentImageListVO.getImagePath();
    String imageName = consentImageListVO.getImageFileName();
    String imageUrl = imagePath + imageName;

    String loadUrl = BuildConfig.CONSENT_SERVER_URL + "DataTempImage/" + Util.removeReversSlashStr(imageUrl);
    Log.e(TAG, "imageUrl -- > " + imageUrl);
    Log.e(TAG, "imagePath -- " + imagePath);
    Log.e(TAG, "imageName -- " + imageName);
    Log.e(TAG, "imageName -- " + loadUrl);
//    http:\\192.168.100.71\image_a05\fnuImage\202010\26\108162237.jpg
//    http://192.168.100.124:8070/DataTempImage/192.168.100.71image_a05fnuImage20201026108162234.jpg
    Picasso.get().load(loadUrl).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(holder.ivConsent);
  }
}
