package com.dbs.dbsec.activity.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.model.ConsentFormListVO;
import com.dbs.dbsec.util.Common;

import java.util.ArrayList;

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

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

  private final String TAG = ConsentHistoryAdapter.class.getSimpleName();

  private AppCompatActivity mActivity;
  private Context           mContext;
  private ConsentActivity   instance;
  public ArrayList<ConsentFormListVO> arrayList;

//  private CustomDeleteAlert

  private String userId = "";
  public String apiType = "";

  public ConsentHistoryAdapter(AppCompatActivity mActivity, Context mContext, ArrayList<ConsentFormListVO> arrayList, String apiType) {
    this.mActivity = mActivity;
    this.mContext  = mContext;
    this.arrayList = arrayList;
    this.instance  = (ConsentActivity) mContext;
    this.apiType   = apiType;
    this.userId    = Common.getStringPreference(mContext, "userInfo", "userId");
  }

  @Override
  public int getItemCount() {
    return 0;
  }

  public class ViewHolder extends RecyclerView.ViewHolder {

    public RelativeLayout listLayer;
    public TextView       tvConsentName;
    public TextView       tvPatientName;
    public TextView       tvTemp;
    public TextView       tvSubText;

    public int position = 0;

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

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

    holder.listLayer     = (RelativeLayout) view.findViewById(R.id.listLayer);
    holder.tvConsentName = (TextView) view.findViewById(R.id.tvConsentName);
    holder.tvPatientName = (TextView) view.findViewById(R.id.tvPatientName);
    holder.tvTemp        = (TextView) view.findViewById(R.id.tvTemp);
    holder.tvSubText     = (TextView) view.findViewById(R.id.tvSubText);

    holder.listLayer.setTag(holder);
    holder.tvSubText.setTag(holder);

    return holder;
  }

  @Override
  public void onBindViewHolder(@NonNull ConsentHistoryAdapter.ViewHolder holder, int position) {
    ConsentFormListVO consentFormListVO = arrayList.get(position);
    String pid          = consentFormListVO.getPid();
    String patientName  = consentFormListVO.getPatientName();
    String formName     = consentFormListVO.getFormName();
    String consentState = consentFormListVO.getConsentState();
    String createUserId = consentFormListVO.getModifyUserId();

    GradientDrawable tvSubBg = (GradientDrawable) holder.tvSubText.getBackground().getCurrent();
    if (apiType.equals("patient") || apiType.equals("write")) {

      tvSubBg.setColor(ContextCompat.getColor(mContext, R.color.typeRightColor));

      SpannableStringBuilder builder = new SpannableStringBuilder();
      SpannableString colorSpannable = new SpannableString(pid);
      colorSpannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.commonColor)), 0, pid.length(), 0);
      builder.append(colorSpannable);
      builder.append(" " + patientName);

      holder.tvTemp.setVisibility(View.GONE);

      GradientDrawable tvSubText = (GradientDrawable) holder.tvSubText.getBackground().getCurrent();

      if (consentState.equals(mContext.getString(R.string.cert))) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.sexNAgeColor));
      } else if (consentState.equals(mContext.getString(R.string.check))) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.typeRightColor));
      } else {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
      }

      if (apiType.equals("patient")) {
        holder.tvPatientName.setVisibility(View.GONE);
      } else {
        holder.tvPatientName.setVisibility(View.VISIBLE);
      }

      holder.tvPatientName.setText(builder, TextView.BufferType.SPANNABLE);
      holder.tvSubText.setText(consentState);
    }

  }


}

