package com.dbs.dbsec.activity.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
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.consent.ConsentCreateParam;
import com.dbs.dbsec.consent.ConsentFacade;
import com.dbs.dbsec.customView.CustomAlertDialog;
import com.dbs.dbsec.httpTask.CallBack;
import com.dbs.dbsec.model.ConsentFormListVO;
import com.dbs.dbsec.model.PatientInfoVO;
import com.dbs.dbsec.util.Common;
import com.dbs.dbsec.util.ConsentConfig;

import java.util.ArrayList;
import java.util.HashMap;

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 static final String TAG = ConsentHistoryAdapter.class.getSimpleName();
  private Context mContext;
  private AppCompatActivity mActivity;
  private ConsentActivity   instance;

  public ArrayList<ConsentFormListVO> arrayList;

  private String userId = "";
  public  String type = "";

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

  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 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 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 consentStateEng = consentFormListVO.getConsentStateEng();
    String createUserId = consentFormListVO.getModifyUserId();

    GradientDrawable tvSubBg = (GradientDrawable) holder.tvSubText.getBackground().getCurrent();
    if (type.equals("write") || type.equals("patient")) {
      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 (consentStateEng.equals(ConsentConfig.TEMP)) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
      } else if (consentStateEng.equals(ConsentConfig.UNFINISHED)) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
      } else if (consentStateEng.equals(ConsentConfig.ELECTR_CMP)) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.typeRightColor));
      } else if (consentStateEng.equals(ConsentConfig.CERTIFY_CMP)) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.sexNAgeColor));
      } else if (consentStateEng.equals(ConsentConfig.PAPER_OUT)) {
        tvSubText.setColor(ContextCompat.getColor(mContext, R.color.paperOutcolor));
      }

      if (type.equals("patient")) {
        holder.tvPatientName.setVisibility(View.GONE);
      } else {
        holder.tvPatientName.setVisibility(View.VISIBLE);
      }
      holder.tvPatientName.setText(builder, TextView.BufferType.SPANNABLE);
      holder.tvSubText.setText(consentState);

    }
    holder.tvConsentName.setText(formName);
    holder.listLayer.setOnClickListener(onClickListener);
  }

  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      ViewHolder holder = (ViewHolder) v.getTag();
      final int position = holder.getLayoutPosition();

      ConsentFormListVO consentFormListVO = arrayList.get(position);
      String userId        = Common.getStringPreference(mContext, "userInfo", "userId");
      String idx           = consentFormListVO.getIdx();
      String formId        = consentFormListVO.getFormId();
      String formCd        = consentFormListVO.getFormCode();
      String formName      = consentFormListVO.getFormName();
      String formPrntNm    = consentFormListVO.getFormPrntNm();
      String printCnt      = consentFormListVO.getPrintCnt();
      String consentMstRid = consentFormListVO.getConsentMstRid();
      String consentState  = consentFormListVO.getConsentState();
      String ocrTag        = consentFormListVO.getOcrTag();
      String pid           = consentFormListVO.getPid();
      String clnDate       = consentFormListVO.getClnDate();
      String cretNo        = consentFormListVO.getCretNo();
      String ordType       = consentFormListVO.getOrdType();
      String deptCd        = consentFormListVO.getDeptCd();
      String mainDrId      = consentFormListVO.getMainDrId();
      String certPass      = consentFormListVO.getCertPass();

      if (!Common.checkNetwork(mContext)) {
        Common.networkErrorAlert(mActivity);
      } else {
        if (instance.centerMenu.patientInfoVO == null && type.equals("patient")) {
          instance.showSingButtonDialog(mContext.getString(R.string.emptyPatientInfo));
        } else {
          ConsentCreateParam consentCreateParam = new ConsentCreateParam(mContext, instance.centerMenu.patientInfoVO);
          HashMap<String, String> globalParam = consentCreateParam.globalParam(consentMstRid);
          ArrayList<HashMap<String, String>> consentArray = consentCreateParam.consentParam(consentMstRid, formId, formCd, formName, pid, ocrTag, clnDate, cretNo, ordType, deptCd, mainDrId);

          HashMap consentData = new HashMap();
          consentData.put("globalParam", globalParam);
          consentData.put("consentArray", consentArray);

          ConsentFacade consentFacade = new ConsentFacade(mContext, instance.indexPage, "", "");
          consentFacade.startConsent(consentData, certPass);
        }
      }

    }
  };

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


}

