package com.dbs.mplus.fatima.activity.activityEvent;

import android.content.Context;
import android.util.Log;

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

import com.dbs.mplus.fatima.R;
import com.dbs.mplus.fatima.activity.ConsentActivity;
import com.dbs.mplus.fatima.adapter.ConsentTempAdapter;
import com.dbs.mplus.fatima.httpTask.DataParser;
import com.dbs.mplus.fatima.httpTask.HttpCallBack;
import com.dbs.mplus.fatima.model.ConsentListVO;
import com.dbs.mplus.fatima.model.PatientInfoVO;
import com.dbs.mplus.fatima.util.ConsentConfig;
import com.dbs.mplus.fatima.util.Util;

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

public class CenterMenu {
  private static final String TAG = "CenterMenu";
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;
  public PatientInfoVO patientInfoVO = null;
  private LinearLayoutManager linearLayoutManager;

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

  public void getPatientInfo(HashMap<String, String> mData) {
    instance.showProgress();
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(final String result) {
        Log.e(TAG, "patientInfo result -- > " + result);
        mActivity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            if (result.equals("")) {
              instance.showSingButtonDialog(mContext.getString(R.string.failedPatientInfo));
            } else {
              patientInfoVO = DataParser.getPatientInfo(mContext, result);
              if (patientInfoVO == null) {
                instance.showSingButtonDialog(mContext.getString(R.string.failedPatientInfo));
              } else {
                setPatientInfo();
              }
            }

            instance.dismissProgress();
          }
        });
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

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

  private void setPatientInfo() {
    String pid         = patientInfoVO.getPid();
    String patientName = patientInfoVO.getPatientNm();
    String sa          = patientInfoVO.getSa();
    String juminNo     = patientInfoVO.getJuminNo();
    String adDate      = patientInfoVO.getAdDate();
    String mainDrName  = patientInfoVO.getMainDrNm();
    String dxNm        = patientInfoVO.getDxNm();
//    String alert       = patientInfoVO.getGbn();

    Log.e(TAG, "pid -- > " + pid);
    Log.e(TAG, "patientName -- > " + patientName);
    Log.e(TAG, "sa -- > " + sa);
    Log.e(TAG, "juminNo -- > " + juminNo);

    String patientInfo = pid + " " + patientName + " " + Util.editAgeToIdNum(sa) + "/" + juminNo.substring(0, 6);
    Log.e(TAG, "pid -- > " + pid);
    Log.e(TAG, "patientName -- > " + patientName);
    Log.e(TAG, "juminNo -- > " + juminNo);

    instance.tvPatientInfo.setText(patientInfo);
    instance.tvAdmissionDay.setText(Util.addSlashDate(adDate));
    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("");

    if (instance.consentCenterAdaper != null) {
      instance.consentCenterAdaper.arrayList.clear();
      instance.consentCenterAdaper.notifyDataSetChanged();
      instance.consentCenterAdaper = null;
    }
  }

  public void getConsentList(String pid, String startDt) {
    instance.showProgress();
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(String result) {
        Log.e(TAG, "result -- > " + result);
        if (!result.equals("")) {
          ArrayList<ConsentListVO> arrayList = DataParser.getConsentList(mContext, result);
          setConsentAdapter(arrayList);
          if (arrayList.size() > 0) {

          }
        }
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

    String consentState = "'UNFINISHED', 'TEMP'";
    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("patientCode", pid);
    mData.put("startDt", startDt);
    mData.put("endDt", Util.removeSlashStr(Util.nowDate()));
    mData.put("consetState", consentState);
    mData.put("jobkindcd", instance.mobile.equals("O") && (instance.jobKindCd.equals("1140") || instance.jobKindCd.equals("1113")) ? "ALL" : instance.jobKindCd);
    Log.e(TAG, "mData -- > " + mData);
    if (Util.checkNetwork(mContext) == true) {
      Util.callHttp(mContext, ConsentConfig.HOST_CONSENT, ConsentConfig.GET_CONSENT_LIST, mData, callBack);
    } else {
      instance.dismissProgress();
      Util.networkErrorAlert(mActivity);
    }
  }

  private void setConsentAdapter(final ArrayList<ConsentListVO> arrayList) {
    mActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {

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

        instance.dismissProgress();

      }
    });
  }
}
