package com.dbs.dbsec.activity.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
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.BuildConfig;
import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.activity.fragment.MarkerFragment;
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.RecyclerView;

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

  private static final String TAG = "ConsentListAdapter";
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;
  public ArrayList<ConsentFormListVO> arrayList;
//  private ConsentEvent consentEvent;

  private String apiType = "";

  public ConsentListAdapter(AppCompatActivity mActivity, Context mContext, ArrayList<ConsentFormListVO> arrayList,  String apiType) {
    this.mActivity = mActivity;
    this.mContext  = mContext;
    this.instance  = (ConsentActivity) mContext;
    this.arrayList = arrayList;
    this.apiType   = apiType;
  }

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

  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(parent.getContext()).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);

    GradientDrawable tvSubBg = (GradientDrawable) holder.tvSubText.getBackground().getCurrent();
    tvSubBg.setColor(ContextCompat.getColor(mActivity, R.color.typeRightColor));

    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();
    holder.tvConsentName.setText(formName);

    holder.tvPatientName.setVisibility(View.GONE);
    holder.tvTemp.setVisibility(View.GONE);

    String subText = "";
    if (apiType.equals(ConsentConfig.GET_USER_FORM_SET)) {
      subText = mContext.getString(R.string.delete);
    } else if (apiType.equals(ConsentConfig.GET_CONSENT_BY_SEARCH)) {
      subText = mContext.getString(R.string.add);
    }

    holder.tvSubText.setText(subText);

    holder.listLayer.setOnClickListener(onClickListener);
    holder.tvSubText.setOnClickListener(onClickListener);

    holder.position = position;
  }

  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      ViewHolder holder = (ViewHolder) v.getTag();
      final int position = holder.getLayoutPosition();
      final ConsentFormListVO consentFormListVO = arrayList.get(position);
      String formCd = consentFormListVO.getFormCode();

      if (v == holder.tvSubText) {
        String httpUrl = "";
        String alertMsg = "";

        if (apiType.equals(ConsentConfig.GET_CONSENT_BY_SEARCH)) {
          httpUrl = ConsentConfig.SET_USER_FORM_SET;
          alertMsg = mContext.getString(R.string.addMaker);
        } else {
          httpUrl = ConsentConfig.DEL_USER_FORM_SET;
          alertMsg = mContext.getString(R.string.deleteMaker);
        }

        instance.csAlert.show();
        instance.csAlert.setContent(alertMsg, "");

        final String finalUrl = httpUrl;
        final String finalFormCd = formCd;
        final String idx = consentFormListVO.getIdx();

        instance.csAlert.btnConfirm.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            CallBack callBack = new CallBack() {
              @Override
              public void result(final String result) {
                mActivity.runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                    instance.dismissProgress();
                    Log.e(TAG, "setOrDelUserFormSet -- > " + result);

                    ArrayList<HashMap<String, String>> resultArr = Common.parseJSON(mContext, result);
                    String code = resultArr.get(0).get("code");
//
                    if (!code.equals("00")) {
                      stop();
                    } else {
                      String rts = resultArr.get(0).get("data");
                      int res = Integer.parseInt(rts);
                      if (res == 0) {
                        stop();
                      } else {
                        if (apiType.equals(ConsentConfig.GET_USER_FORM_SET)) {
                          Log.e(TAG, "position -- > " + position);
                          arrayList.remove(position);
                          notifyItemRemoved(position);
                        }
                      }
                    }
                  }
                });

              }
              @Override
              public void stop() {
                String message = "";
                if (apiType.equals(ConsentConfig.GET_CONSENT_BY_SEARCH)) {
                  message = String.format(mContext.getString(R.string.failMakerEvent), mContext.getString(R.string.add));
                } else {
                  message= String.format(mContext.getString(R.string.failMakerEvent), mContext.getString(R.string.delete));
                }
                instance.showSingButtonDialog(message);
              }

              @Override
              public void error() {

              }
            };

            HashMap<String, String> mData = new HashMap<String, String>();
            mData.put("instCd", BuildConfig.INST_CD);
            mData.put("userId", Common.getStringPreference(mContext, "userInfo", "userId"));
            mData.put("formCd", finalFormCd);

            if (apiType.equals(ConsentConfig.GET_USER_FORM_SET)) {
              mData.put("idx", idx);
            }
            Log.e(TAG, "mData -- > " + mData);

            if (Common.checkNetwork(mContext) == true) {
              instance.showProgress();
              Common.callHttp(mContext, finalUrl, mData, callBack);
            } else {
              Common.networkErrorAlert(mActivity);
            }
            instance.csAlert.dismiss();
          }
        });

        instance.csAlert.btnCancel.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            instance.csAlert.dismiss();
          }
        });

      }



    }
  };


}
