package com.dbs.dbsec.activity.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
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.CustomDeleteAlert;
import com.dbs.dbsec.httpTask.CallBack;
import com.dbs.dbsec.model.ConsentFormListVO;
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.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

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

  private static final String TAG = ConsentTempAdapter.class.getSimpleName();
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;
  private String userId = "";
  private CustomDeleteAlert customDeleteAlert = null;

  public ArrayList<ConsentFormListVO> arrayList;
  public HashMap<Integer, Object> checkMap;


  public ConsentTempAdapter(AppCompatActivity mActivity, Context mContext, ArrayList<ConsentFormListVO> arrayList) {
    this.mActivity = mActivity;
    this.mContext = mContext;
    this.instance = ((ConsentActivity) mContext);
    this.arrayList = arrayList;
    this.userId = Common.getStringPreference(mContext, "userInfo", "userId");
    this.checkMap = new HashMap<Integer, Object>();
  }

  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 ConsentTempAdapter.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 formName = consentFormListVO.getFormName();
    String consentState = consentFormListVO.getConsentState();
    String consentStateEng = consentFormListVO.getConsentStateEng();
    String modifyUserId = consentFormListVO.getModifyUserId();

    GradientDrawable tvTempBg = (GradientDrawable) holder.tvTemp.getBackground().getCurrent();
    GradientDrawable tvSubBg = (GradientDrawable) holder.tvSubText.getBackground().getCurrent();

    if (consentStateEng.equals(ConsentConfig.TEMP)) {
      tvTempBg.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
      tvSubBg.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
    } else if (consentStateEng.equals(ConsentConfig.UNFINISHED)) {
      tvTempBg.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
      tvSubBg.setColor(ContextCompat.getColor(mContext, R.color.tempColor));
    }else if (consentStateEng.equals(ConsentConfig.ELECTR_CMP)) {
      tvTempBg.setColor(ContextCompat.getColor(mContext, R.color.typeRightColor));
      tvSubBg.setColor(ContextCompat.getColor(mContext, R.color.typeRightColor));
    }
    holder.tvPatientName.setVisibility(View.GONE);
    holder.tvTemp.setText(consentState);

    if (modifyUserId.equals(userId)) {
      holder.tvTemp.setVisibility(View.VISIBLE);
      holder.tvSubText.setText(mContext.getString(R.string.delete));
      tvSubBg.setColor(ContextCompat.getColor(mContext, R.color.alertColor));
      holder.tvSubText.setOnClickListener(onClickListener);
    } else if (consentStateEng.equals(ConsentConfig.UNFINISHED)) {
      holder.tvTemp.setVisibility(View.VISIBLE);
      holder.tvSubText.setText(mContext.getString(R.string.delete));
      tvSubBg.setColor(ContextCompat.getColor(mContext, R.color.alertColor));
      holder.tvSubText.setOnClickListener(onClickListener);
    } else {
      holder.tvTemp.setVisibility(View.GONE);
      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);
      final String userId        = Common.getStringPreference(mContext, "userInfo", "userId");
      String idx           = consentFormListVO.getIdx();
      String formId        = consentFormListVO.getFormId();
      String formCd        = consentFormListVO.getFormCode();
      final String formName      = consentFormListVO.getFormName();
      String formPrntNm    = consentFormListVO.getFormPrntNm();
      String printCnt      = consentFormListVO.getPrintCnt();
      final 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 (v == holder.listLayer) {
        if (!Common.checkNetwork(mContext)) {
          Common.networkErrorAlert(mActivity);
        } else {
          if (instance.centerMenu.patientInfoVO == null) {
            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);
          }
        }
      } else if (v == holder.tvSubText) {
        String msg = String.format(mContext.getString(R.string.deleteConsent), formName);
        instance.csAlert.show();
        instance.csAlert.setContent(msg, "");
        instance.csAlert.btnConfirm.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {


            CallBack callBack = new CallBack() {
              @Override
              public void result(final String result) {
                Log.e(TAG, "saveTempDelete -- > " + result);
                instance.dismissProgress();
                customDeleteAlert.dismiss();
                instance.runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                    ArrayList<HashMap<String, String>> resultArr = Common.parseJSON(mContext, result);
                    String code = resultArr.get(0).get("code");
                    if (!code.equals("00")) {
                      stop();
                    } else {
                      arrayList.remove(position);
                      notifyItemRemoved(position);
                    }
                  }
                });
              }

              @Override
              public void stop() {
                String message = String.format(mContext.getString(R.string.deleteConsentFailed), formName);
                instance.showSingButtonDialog(message);
              }

              @Override
              public void error() {

              }
            };
            HashMap<String, String> mData = new HashMap<String, String>();
            mData.put("userId", userId);
            mData.put("consentMstRid", consentMstRid);
            customDeleteAlert = new CustomDeleteAlert(mContext, mData, callBack);
            customDeleteAlert.show();
          }
        });
        instance.csAlert.btnCancel.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            instance.csAlert.dismiss();
          }
        });
      }
    }
  };

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