package com.dbs.dbsec.activity.activityEvent;

import android.content.Context;
import android.os.Build;
import android.telecom.Call;
import android.util.Log;

import com.dbs.dbsec.BuildConfig;
import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.activity.adapter.ConsentTempAdapter;
import com.dbs.dbsec.httpTask.CallBack;
import com.dbs.dbsec.httpTask.ParseJsonData;
import com.dbs.dbsec.model.ConsentFormListVO;
import com.dbs.dbsec.model.PatientInfoVO;
import com.dbs.dbsec.model.PatientListVO;
import com.dbs.dbsec.util.Common;
import com.dbs.dbsec.util.ConsentConfig;

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

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

public class CenterMenu {
  private static final String TAG = CenterMenu.class.getSimpleName();
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;
  private LinearLayoutManager linearLayoutManager;

  public PatientInfoVO patientInfoVO = null;


  public CenterMenu(Context mContext, AppCompatActivity mActivity) {
    this.mActivity = mActivity;
    this.mContext = mContext;
    this.instance = (ConsentActivity) mContext;
  }

  public void getPatientInfo(HashMap<String, String> mData) {

    CallBack callBack = new CallBack() {
      @Override
      public void result(final String result) {
        Log.e(TAG, "getPatientInfo -- > " + result);
        mActivity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            ArrayList<HashMap<String, String>> arrayList = Common.parseJSON(mContext, result);
            String code = arrayList.get(0).get("code");

            if (!code.equals("00")) {
              stop();
            } else {
              patientInfoVO = ParseJsonData.getInstance(mContext).getPatientInfo(arrayList);
              setPatientInfo();
            }
          }
        });
      }

      @Override
      public void stop() {
        instance.dismissProgress();
      }

      @Override
      public void error() {

      }
    };

    if (Common.checkNetwork(mContext) == true) {
      instance.showProgress();
      Common.callHttp(mContext, ConsentConfig.GET_PATIENT_INFO, mData, callBack);
    } else {
      instance.dismissProgress();
      Common.networkErrorAlert(mActivity);
    }
  }

  private void setPatientInfo() {
    instance.dismissProgress();

    String pid = patientInfoVO.getPid();
    String patientName = patientInfoVO.getPatientName();
    String sa = patientInfoVO.getSa();
    String juminNo = patientInfoVO.getJuminNo();
    String inDd = patientInfoVO.getInDd();
    String mainDrName = patientInfoVO.getMainDrName();
    String dxNm = patientInfoVO.getDxNm();
    String alert = patientInfoVO.getGbn();

    String patientInfo = pid + " " + patientName + " " + sa + " " + juminNo.substring(0, 6);

    instance.tvPatientInfo.setText(patientInfo);
    instance.tvAdmissionDay.setText(inDd);
    instance.tvChargeDoctor.setText(mainDrName);
    instance.tvDiagnosisName.setText(dxNm);
    instance.tvPatientAlert.setText(alert);
  }

  public void clearPatientInfo() {
    patientInfoVO = null;
    instance.tvPatientInfo.setText("");
    instance.tvAdmissionDay.setText("");
    instance.tvChargeDoctor.setText("");
    instance.tvDiagnosisName.setText("");
    instance.tvPatientAlert.setText("");


    if (instance.consentCenterAdapter != null) {

      instance.consentCenterAdapter.arrayList.clear();
      instance.consentCenterAdapter.notifyDataSetChanged();
      instance.consentCenterAdapter = null;
    }
  }

  public void getConsentList(String pid, String startDt) {

    CallBack callBack = new CallBack() {
      @Override
      public void result(final String result) {

        Log.e(TAG, "getConsentList -- > " + result);
        mActivity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            ArrayList<HashMap<String, String>> arrayList = Common.parseJSON(mContext, result);
            String code = arrayList.get(0).get("code");

            ArrayList<ConsentFormListVO> consentArr = ParseJsonData.getInstance(mContext).getConsentFormList(arrayList);
            setConsentAdapter(consentArr);
          }
        });
      }
      @Override
      public void stop() {
        instance.dismissProgress();
      }
      @Override
      public void error() {
      }
    };

    String consentState = "'TEMP', 'UNFINISHED', 'ELECTR_CMP'";
    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("patientCode", pid);
    mData.put("startDt", startDt);
    mData.put("endDt", Common.removeSlashStr(Common.nowDate()));
    mData.put("consentState", consentState);
    mData.put("instCd", BuildConfig.INST_CD);

    Log.e(TAG, "centerConsentTempList -- > " + mData);

    if (Common.checkNetwork(mContext)) {
      instance.showProgress();
      Common.callHttp(mContext, ConsentConfig.GET_CONSENT_LIST, mData, callBack);
    } else {
      Common.networkErrorAlert(mActivity);
    }
  }

  private void setConsentAdapter(final ArrayList<ConsentFormListVO> arrayList) {
    mActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        instance.dismissProgress();
        boolean contains = arrayList.contains("ELECTR_CMP");
        if (contains == true) {
          instance.tvTempCenterTxt.setText(mContext.getString(R.string.noWriteTempList2));
        } else {
          instance.tvTempCenterTxt.setText(mContext.getString(R.string.noWriteTempList));
        }

        if (instance.consentCenterAdapter == null) {
          instance.consentCenterAdapter = new ConsentTempAdapter(mActivity, mContext, arrayList);
          linearLayoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
          instance.tempRecyclerView.setLayoutManager(linearLayoutManager);
          instance.tempRecyclerView.setAdapter(instance.consentCenterAdapter);
        } else {
          instance.consentCenterAdapter.arrayList.clear();
          instance.consentCenterAdapter.arrayList = arrayList;
          instance.consentCenterAdapter.checkMap.clear();
          instance.consentCenterAdapter.notifyDataSetChanged();
        }
      }
    });

  }
}
