package com.dbs.consent.demo.activity.activityEvent;

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

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

import com.dbs.consent.demo.BuildConfig;
import com.dbs.consent.demo.R;
import com.dbs.consent.demo.activity.ConsentActivity;
import com.dbs.consent.demo.adapter.ConsentHistoryAdapter;
import com.dbs.consent.demo.httpTask.CallBack;
import com.dbs.consent.demo.httpTask.SoapParser;
import com.dbs.consent.demo.model.ConsentFormListVO;
import com.dbs.consent.demo.model.PatientInfoVO;
import com.dbs.consent.demo.util.ConsentConfig;
import com.dbs.consent.demo.util.Util;

import org.ksoap2.serialization.SoapObject;

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;
  //  private ConsentHistoryAdapter consentHistoryAdapter;
  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) {

    instance.showProgress();
    CallBack callBack = new CallBack() {
      @Override
      public void result(final SoapObject result) {
        Log.e(TAG, "result -- > " + result);

        mActivity.runOnUiThread(new Runnable() {
          @Override
          public void run() {
            patientInfoVO = SoapParser.getPatientInfo(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)) {
      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.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);
//    instance.history.setLeftDate(Util.addSlashDate(inDd));
  }

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


    if (instance.consentCenterAdaper != null) {

      instance.consentCenterAdaper.arrayList.clear();
      instance.consentCenterAdaper.notifyDataSetChanged();
      instance.consentCenterAdaper = null;

      Log.e(TAG, "instance.consentCenterAdaper -- > " + instance.consentCenterAdaper);

    }
  }

  public void getConsentList(String pid, String startDt) {
//    GET_CONSENT_LIST
    instance.showProgress();
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
        Log.e(TAG, "getConsentList result -- > " + result);
        ArrayList<ConsentFormListVO> arrayList = SoapParser.getConsentList(result);
        setConsentAdapter(arrayList);
      }

      @Override
      public void stop() {

      }

      @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", Util.removeSlashStr(Util.nowDate()));
    mData.put("consentState", consentState);
    mData.put("instCd", BuildConfig.INST_CD);

    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<ConsentFormListVO> arrayList) {
    mActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {

        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.consentCenterAdaper == null) {
          instance.consentCenterAdaper = new ConsentHistoryAdapter(mActivity, mContext, arrayList, "centerTemp");
          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.notifyDataSetChanged();
//          instance.tempRecyclerView.scrollToPosition(0);
        }

        instance.dismissProgress();

      }
    });

  }
}
