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

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.provider.CalendarContract;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatRadioButton;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.dbs.mplus.knuh.BuildConfig;
import com.dbs.mplus.knuh.R;
import com.dbs.mplus.knuh.activity.ConsentActivity;
import com.dbs.mplus.knuh.adapter.DeptSpinnerAdapter;
import com.dbs.mplus.knuh.adapter.PatientListAdapter;
import com.dbs.mplus.knuh.customView.CustomRadioGroup;
import com.dbs.mplus.knuh.httpTask.CallBack;
import com.dbs.mplus.knuh.httpTask.SoapParser;
import com.dbs.mplus.knuh.model.DeptListVO;
import com.dbs.mplus.knuh.model.DoctorListVO;
import com.dbs.mplus.knuh.model.PatientListVO;
import com.dbs.mplus.knuh.util.ConsentConfig;
import com.dbs.mplus.knuh.util.Util;

import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import javax.security.auth.callback.Callback;

public class LeftMenu {

  private static final String TAG = "LeftMenu";
  private AppCompatActivity mActivity;
  private Context mContext;
  private ConsentActivity instance;

  private DatePickerDialog dateDialog;
  private DeptSpinnerAdapter deptSpinnerAdapter;
  public PatientListAdapter patientListAdapter;
  private LinearLayoutManager linearLayoutManager;

  private HashMap<String, String> patientSearchMap = null;

  public String patientState = "";

  private boolean pidSortFlag         = false;
  private boolean patientNameSortFlag = false;
  private boolean wardSortFlag        = false;

  public LeftMenu(Context context, AppCompatActivity activity) {
    this.mContext   = context;
    this.mActivity  = activity;
    this.instance   = (ConsentActivity) context;
    this.patientSearchMap = new HashMap<String, String>();

    int year        = Util.toDayDate()[0];
    int month       = Integer.parseInt(Util.plusZero(Util.toDayDate()[1] - 1));
    int day         = Integer.parseInt(Util.plusZero(Util.toDayDate()[2]));
    this.dateDialog = new DatePickerDialog(context, R.style.DatePickerDialogTheme, onDateSetListener, year, month, day);
  }

  @SuppressLint("RestrictedApi")
  public void setRadioGroupDraw(String indexPage) {
    instance.radioGroup = new CustomRadioGroup(mContext);

    instance.radioGroupParams = new CustomRadioGroup.LayoutParams(CustomRadioGroup.LayoutParams.WRAP_CONTENT, CustomRadioGroup.LayoutParams.WRAP_CONTENT);
    instance.radioGroupParams.addRule(RelativeLayout.CENTER_IN_PARENT);

    String radioStr1 = "";
    String radioStr2 = "";
    String radioStr3 = "";
    String radioStr4 = "";
    String radioStr5 = "";
    if (indexPage.equals("I")) {
      radioStr1 = mContext.getString(R.string.stay);
      radioStr2 = mContext.getString(R.string.hospitalization);
      radioStr3 = mContext.getString(R.string.discharge);
      radioStr4 = mContext.getString(R.string.noWrite);

    } else if (indexPage.equals("O")) {
      radioStr1 = mContext.getString(R.string.all);
      radioStr2 = mContext.getString(R.string.accept);
      radioStr3 = mContext.getString(R.string.waiting);
      radioStr4 = mContext.getString(R.string.complete);
      radioStr5 = mContext.getString(R.string.noWrite);
    } else if (indexPage.equals("E")) {
      radioStr1 = mContext.getString(R.string.stay);
      radioStr2 = mContext.getString(R.string.discharge);
      radioStr3 = mContext.getString(R.string.leaveClear);
      radioStr4 = mContext.getString(R.string.noWrite);
    } else if (indexPage.equals("OP")) {
      radioStr1 = mContext.getString(R.string.all);
      radioStr2 = mContext.getString(R.string.notYet);
      radioStr3 = mContext.getString(R.string.ready);
      radioStr4 = mContext.getString(R.string.ing);
      radioStr5 = mContext.getString(R.string.complete);
    } else if (indexPage.equals("SR")) {
      radioStr1 = mContext.getString(R.string.admission);
      radioStr2 = mContext.getString(R.string.outPatient);
      radioStr3 = mContext.getString(R.string.emergency);
    }

    if (indexPage.equals("O") || indexPage.equals("OP")) {
      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioButton1 = new AppCompatRadioButton(mContext);
      instance.radioButton1.setText(radioStr1);
      instance.radioButton1.setTextSize(14);
      instance.radioButton1.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton1);

      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioParams.addRule(RelativeLayout.RIGHT_OF, instance.radioButton1.getId());
      instance.radioButton2 = new AppCompatRadioButton(mContext);
      instance.radioButton2.setText(radioStr2);
      instance.radioButton2.setTextSize(14);
      instance.radioButton2.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton2);

      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioParams.addRule(RelativeLayout.RIGHT_OF, instance.radioButton2.getId());
      instance.radioButton3 = new AppCompatRadioButton(mContext);
      instance.radioButton3.setText(radioStr3);
      instance.radioButton3.setTextSize(14);
      instance.radioButton3.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton3);

      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioParams.addRule(RelativeLayout.BELOW, instance.radioButton3.getId());
      instance.radioButton4 = new AppCompatRadioButton(mContext);
      instance.radioButton4.setText(radioStr4);
      instance.radioButton4.setTextSize(14);
      instance.radioButton4.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton4);

      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioParams.addRule(RelativeLayout.BELOW, instance.radioButton2.getId());
      instance.radioParams.addRule(RelativeLayout.RIGHT_OF, instance.radioButton4.getId());
      instance.radioButton5 = new AppCompatRadioButton(mContext);
      instance.radioButton5.setText(radioStr5);
      instance.radioButton5.setTextSize(14);
      instance.radioButton5.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton5);
    } else {
      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioButton1 = new AppCompatRadioButton(mContext);
      instance.radioButton1.setText(radioStr1);
      instance.radioButton1.setTextSize(14);
      instance.radioButton1.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton1);

      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioParams.addRule(RelativeLayout.RIGHT_OF, instance.radioButton1.getId());
      instance.radioButton2 = new AppCompatRadioButton(mContext);
      instance.radioButton2.setText(radioStr2);
      instance.radioButton2.setTextSize(14);
      instance.radioButton2.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton2);

      instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      instance.radioParams.addRule(RelativeLayout.RIGHT_OF, instance.radioButton2.getId());
      instance.radioButton3 = new AppCompatRadioButton(mContext);
      instance.radioButton3.setText(radioStr3);
      instance.radioButton3.setTextSize(14);
      instance.radioButton3.setLayoutParams(instance.radioParams);
      instance.radioGroup.addView(instance.radioButton3);

      if (!indexPage.equals("SR")) {
        instance.radioParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        instance.radioParams.addRule(RelativeLayout.RIGHT_OF, instance.radioButton3.getId());
        instance.radioButton4 = new AppCompatRadioButton(mContext);
        instance.radioButton4.setText(radioStr4);
        instance.radioButton4.setTextSize(14);
        instance.radioButton4.setLayoutParams(instance.radioParams);
        instance.radioGroup.addView(instance.radioButton4);
      }

    }

    if (instance.jobKindCd.indexOf("03") == 0 || instance.indexPage.equals("SR")) {
      instance.radioButton1.setChecked(true);
      patientState = "A";
    } else {
      if (indexPage.equals("SR")) {
        instance.radioButton1.setChecked(true);
        patientState = "A";
      } else {
        if (indexPage.equals("I") || indexPage.equals("E")) {
          instance.radioButton4.setChecked(true);
          patientState = "C";
        } else if (indexPage.equals("O")) {
          instance.radioButton5.setChecked(true);
          patientState = "3";
        } else if (indexPage.equals("OP")) {
          instance.radioButton1.setChecked(true);
          patientState = "A";
        }
      }
    }

    instance.radioLayout.addView(instance.radioGroup, instance.radioGroupParams);
  }

  private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
      String date = year + "/" + Util.plusZero(month + 1) + "/" + Util.plusZero(dayOfMonth);
      setDate(date);
    }
  };

  public View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      if (instance.dateLayout == v) {
        //진료 일자 선택
        dateDialog.show();
      } else if (instance.patientSearchLayout == v) {
        //등록번호 검색 input 옆의 조회 버튼
        getPatientList();
      }
    }
  };

  public View.OnClickListener sortOnClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      if (patientListAdapter != null) {

        if (instance.llPatientSort == v) {
          Collections.sort(patientListAdapter.arrayList, new Comparator<PatientListVO>() {
            @Override
            public int compare(PatientListVO o1, PatientListVO o2) {
              if (pidSortFlag == false) {
                return (int) (Double.parseDouble(o1.getPid()) - Double.parseDouble(o2.getPid()));
              } else {
                return (int) (Double.parseDouble(o2.getPid()) - Double.parseDouble(o1.getPid()));
              }
            }
          });

          if (pidSortFlag == false) {
            instance.column1.setText(mContext.getString(R.string.patientId) + " ▲");
            pidSortFlag = true;
          } else {
            instance.column1.setText(mContext.getString(R.string.patientId) + " ▼");
            pidSortFlag = false;
          }

          instance.column4.setText(mContext.getString(R.string.patientName));
          if (instance.indexPage.equals("I")) {
            instance.column6.setText(mContext.getString(R.string.wardRoom));
          }
        } else if (instance.llPatientNmSort == v) {
          Collections.sort(patientListAdapter.arrayList, new Comparator<PatientListVO>() {
            @Override
            public int compare(PatientListVO o1, PatientListVO o2) {
              if (patientNameSortFlag == false) {
                return o1.getHngNm().compareTo(o2.getHngNm());
              } else {
                return o2.getHngNm().compareTo(o1.getHngNm());
              }
            }
          });

          if (patientNameSortFlag == false) {
            instance.column4.setText(mContext.getString(R.string.patientName) + " ▲");
            patientNameSortFlag = true;
          } else {
            instance.column4.setText(mContext.getString(R.string.patientName) + " ▼");
            patientNameSortFlag = false;
          }

          instance.column1.setText(mContext.getString(R.string.patientId));
          if (instance.indexPage.equals("I")) {
            instance.column6.setText(mContext.getString(R.string.wardRoom));
          }
        } else if (instance.llRoomSort == v) {
          if (instance.indexPage.equals("I")) {
            Collections.sort(patientListAdapter.arrayList, new Comparator<PatientListVO>() {
              @Override
              public int compare(PatientListVO o1, PatientListVO o2) {
                if (wardSortFlag == false) {
                  return o1.getRoomCd().compareTo(o2.getRoomCd());
                } else {
                  return o2.getRoomCd().compareTo(o1.getRoomCd());
                }
              }
            });

            if (wardSortFlag == false) {
              instance.column6.setText(mContext.getString(R.string.wardRoom) + " ▲");
              wardSortFlag = true;
            } else {
              instance.column6.setText(mContext.getString(R.string.wardRoom) + " ▼");
              wardSortFlag = false;
            }

            instance.column1.setText(mContext.getString(R.string.patientId));
            instance.column4.setText(mContext.getString(R.string.patientName));
          }
        }



        linearLayoutManager.scrollToPositionWithOffset(0, 0);
        patientListAdapter.notifyDataSetChanged();
      }
    }
  };

  public EditText.OnEditorActionListener onEditorActionListener = new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      if (actionId == EditorInfo.IME_ACTION_DONE) {
        if (v == instance.etPatientId) {
          getPatientList();
        }
      }
      return false;
    }
  };

  private void setDate(String date) {
    instance.tvMainDate.setText(date);
  }

  public String getDate() {
    return Util.removeSlashStr(instance.tvMainDate.getText().toString().trim());
  }

  /**
  * 병동 리스트
  * @author jksong
  * @since 2020-01-16 오전 11:50
  **/
  public void getWardList() {
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
        if (result == null || result.toString().indexOf("null") > 0) {
          stop();
        } else {
          ArrayList<DeptListVO> arrayList = SoapParser.getWardNDeptList(result);
          if (arrayList.size() == 0) {
            stop();
          } else {
            setSpinnerAdapter(arrayList, ConsentConfig.GET_WARD_LIST);
          }
        }
      }

      @Override
      public void stop() {
        ArrayList<DeptListVO> arrayList = new ArrayList<DeptListVO>();
        arrayList.add(new DeptListVO("", mContext.getString(R.string.wardListEmpty)));
        setSpinnerAdapter(arrayList, ConsentConfig.GET_WARD_LIST);
      }

      @Override
      public void error() {

      }
    };

    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("instCd", BuildConfig.INST_CD);

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

  }

  /**
  * 진료과 리스트
  * @author jksong
  * @since 2020-01-16 오전 11:50
  **/
  public void getDeptList() {
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
//        Log.e(TAG, "getDeptList result -- > " + result);
        if (result == null || result.toString().indexOf("null") > 0) {
          stop();
        } else {
          ArrayList<DeptListVO> arrayList = SoapParser.getWardNDeptList(result);
          if (arrayList.size() == 0) {
            stop();
          } else {
            setSpinnerAdapter(arrayList, ConsentConfig.GET_DEPT_LIST);
          }
        }

      }

      @Override
      public void stop() {
        ArrayList<DeptListVO> arrayList = new ArrayList<DeptListVO>();
        arrayList.add(new DeptListVO("", mContext.getString(R.string.deptListEmpty)));
        setSpinnerAdapter(arrayList, ConsentConfig.GET_DEPT_LIST);
      }

      @Override
      public void error() {

      }
    };

    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("ordType", instance.indexPage);
    mData.put("instCd", BuildConfig.INST_CD);

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

  }

  public void getDoctorList(String ordDeptCd) {
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
        Log.e(TAG, "getDoctorList result -- > " + result);
        if (result == null || result.toString().indexOf("null") > 0) {
          stop();
        } else {
          ArrayList<DoctorListVO> arrayList = SoapParser.getDoctorList(result);
          if (arrayList.size() == 0) {
            stop();
          } else {
            setSpinnerAdapter(arrayList, ConsentConfig.GET_DOCTOR_LIST);
          }
        }

      }

      @Override
      public void stop() {
        ArrayList<DoctorListVO> arrayList = new ArrayList<DoctorListVO>();
        arrayList.add(new DoctorListVO("", mContext.getString(R.string.doctorListEmpty), "", ""));
      }

      @Override
      public void error() {

      }
    };

    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("instCd", BuildConfig.INST_CD);
    mData.put("srchDd", getDate());
    mData.put("ordDeptCd", ordDeptCd);

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

  private void setSpinnerAdapter(final ArrayList arrayList, final String apiType) {
    mActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        deptSpinnerAdapter = new DeptSpinnerAdapter(mActivity, arrayList, apiType);
        if (apiType.equals(ConsentConfig.GET_WARD_LIST)) {
          instance.wardSpinner.setAdapter(deptSpinnerAdapter);
          instance.wardSpinner.setOnItemSelectedListener(onItemSelectedListener);

          for (int i = 0; i < arrayList.size(); i++) {
            DeptListVO deptListVO = (DeptListVO) arrayList.get(i);
            String _deptCd = deptListVO.getDeptCd();
            if (instance.deptCd.equals(_deptCd)) {
              instance.wardSpinner.setSelection(i);
            }
          }
        } else if (apiType.equals(ConsentConfig.GET_DEPT_LIST)) {
          instance.deptSpinner.setAdapter(deptSpinnerAdapter);
          instance.deptSpinner.setOnItemSelectedListener(onItemSelectedListener);

          for (int i = 0; i < arrayList.size(); i++) {
            DeptListVO deptListVO = (DeptListVO) arrayList.get(i);
            String _deptCd = deptListVO.getDeptCd();
            if (instance.deptCd.equals(_deptCd)) {
              instance.deptSpinner.setSelection(i);
            }
          }
        } else if (apiType.equals(ConsentConfig.GET_DOCTOR_LIST)) {
          instance.dismissProgress();
          instance.doctorSpinner.setAdapter(deptSpinnerAdapter);
          instance.doctorSpinner.setOnItemSelectedListener(onItemSelectedListener);

          for (int i = 0; i < arrayList.size(); i++) {
            DoctorListVO doctorListVO = (DoctorListVO) arrayList.get(i);
            String _doctorId = doctorListVO.getDoctorId();
            if (instance.userId.equals(_doctorId)) {
              instance.doctorSpinner.setSelection(i);
            }
          }

        }
      }
    });
  }

  private Spinner.OnItemSelectedListener onItemSelectedListener = new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      DeptListVO deptListVO     = null;
      DoctorListVO doctorListVO = null;
      String deptCd         = "";
      String deptNm         = "";
      String doctorId       = "";
      String drKind         = "";

      if (parent != instance.doctorSpinner) {
        deptListVO = (DeptListVO) parent.getAdapter().getItem(position);
        deptCd = deptListVO.getDeptCd();
        deptNm = deptListVO.getDeptNm();
      }

      if (parent == instance.wardSpinner) {
        Log.e(TAG, "wardSpinner deptCd -- > " + deptCd);
        patientSearchMap.put("wardCd",deptCd);
      } else if (parent == instance.deptSpinner) {
        Log.e(TAG, "deptCd -- > " + deptCd);
        Log.e(TAG, "deptNm -- > " + deptNm);
        if (!deptCd.equals("")) {
          instance.showProgress();
          getDoctorList(deptCd);
        } else {
          ArrayList<DoctorListVO> arrayList = new ArrayList<DoctorListVO>();
          arrayList.add(new DoctorListVO("", mContext.getString(R.string.deptListEmpty), "", ""));
          setSpinnerAdapter(arrayList, ConsentConfig.GET_DOCTOR_LIST);
        }

        Log.e(TAG, "deptSpinner deptCd -- > " + deptCd);
        patientSearchMap.put("ordDeptCd", deptCd);

      } else if (parent == instance.doctorSpinner) {
        doctorListVO = (DoctorListVO) parent.getAdapter().getItem(position);
        doctorId = doctorListVO.getDoctorId();
        drKind   = doctorListVO.getDrKind();

        patientSearchMap.put("doctorId", doctorId);
        patientSearchMap.put("drKind", drKind);
      }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
  };

  public CustomRadioGroup.OnCheckedChangeListener onCheckedChangeListener = new CustomRadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CustomRadioGroup group, int checkedId) {
      if (checkedId == instance.radioButton1.getId()) {
        patientState = "A";
      } else if (checkedId == instance.radioButton2.getId()) {
        patientState = "0";
      } else if (checkedId == instance.radioButton3.getId()) {
        patientState = "1";
      } else if (checkedId == instance.radioButton4.getId()) {
        if (instance.indexPage.equals("I") || instance.indexPage.equals("ER")) {
          patientState = "C";
        } else {
          patientState = "2";
        }
      } else if (checkedId == instance.radioButton5.getId()) {
        if (instance.indexPage.equals("OP")) {
          patientState = "3";
        } else {
          patientState = "C";
        }
      }

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

    }
  };
  
  /**
  * 로그인시 사용자 정보로 환자 리스트를 최초 처음만 조회해준다
  * @author jksong
  * @since 2020-02-24 오후 12:59
  **/
  public void getInitPatientList() {
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
        ArrayList<PatientListVO> arrayList = SoapParser.getPatientList(result);
        if (arrayList.size() > 0) {
          setPatientListAdapter(arrayList);
        }
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

    String srchDd       = getDate();
    String ordDeptCd    = Util.getStringPreference(mContext, "userInfo", "deptCd");
    String doctorId     = Util.getStringPreference(mContext, "userInfo", "userId");
    String pid          = "";
    String patientState = this.patientState;
    String instCd       = BuildConfig.INST_CD;
    String wardCd       = "";
    String userId       = Util.getStringPreference(mContext, "userInfo", "userId");
    String myPatient    = "N";
    String jobKindCd    = Util.getStringPreference(mContext, "userInfo", "jobKindCd");
    String drKind       = Util.getStringPreference(mContext, "userInfo", "drKind");
    String selectFlag   = instance.indexPage;
    String srchYn       = instance.indexPage.equals("SR") ? "Y" : "N";

    if (jobKindCd.indexOf("03") == 0) {
    } else {
      if (selectFlag.equals("I")) {
        wardCd = Util.getStringPreference(mContext, "userInfo", "deptCd");
      }
    }

    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("srchDd", srchDd);
    mData.put("ordDeptCd", ordDeptCd);
    mData.put("doctorId", doctorId);
    mData.put("pid", pid);
    mData.put("patientState", patientState);
    mData.put("instCd", instCd);
    mData.put("wardCd", wardCd);
    mData.put("userId", userId);
    mData.put("myPatient", myPatient);
    mData.put("jobKindCd", jobKindCd);
    mData.put("drKind", drKind);
    mData.put("selectFlag", selectFlag);
    mData.put("srchYN",  srchYn);

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

  private void getPatientList() {

    boolean emptyData = false;
    String wardCd    = patientSearchMap.get("wardCd") == null ? "" : patientSearchMap.get("wardCd");
    String deptCd    = patientSearchMap.get("ordDeptCd") == null ? "" : patientSearchMap.get("ordDeptCd");
    String myPatient = instance.chkMyPatient.isChecked() == true ? "Y" : "N";
    String message   = "";

    if (instance.indexPage.equals("I")) {
      if (wardCd.equals("") && deptCd.equals("") && instance.etPatientId.getText().toString().trim().equals("")) {
        emptyData = true;
        message = mContext.getString(R.string.inPateintSearchEmptyData);
      }

    } else if (instance.indexPage.equals("O")) {
      if (deptCd.equals("") && instance.etPatientId.getText().toString().trim().equals("")) {
        emptyData = true;
        message = mContext.getString(R.string.patientsearchEmptyData);
      }
    } else if (instance.indexPage.equals("SR")) {
      String pidStr = instance.etPatientId.getText().toString();
      if (pidStr.equals("")) {
        emptyData = true;
        message   = mContext.getString(R.string.emptyPatientId);
      }
    }

    if (emptyData == true) {
      instance.showSingButtonDialog(message);

      return;
    }

    instance.showProgress();
    CallBack callBack = new CallBack() {
      @Override
      public void result(SoapObject result) {
        Log.e(TAG, "getPatientList result -- > " + result);
        ArrayList<PatientListVO> arrayList = SoapParser.getPatientList(result);
        setPatientListAdapter(arrayList);
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

    String srchYN = "N";
    if (instance.indexPage.equals("SR")) {
      srchYN = "Y";
    }

    patientSearchMap.put("srchDd", getDate());
    patientSearchMap.put("pid", instance.etPatientId.getText().toString().trim());
    patientSearchMap.put("patientState", patientState);
    patientSearchMap.put("instCd", BuildConfig.INST_CD);
    patientSearchMap.put("userId", instance.userId);
    patientSearchMap.put("myPatient", myPatient);
    patientSearchMap.put("jobKindCd", instance.jobKindCd);
    patientSearchMap.put("selectFlag", instance.indexPage);
    patientSearchMap.put("srchYN", srchYN);

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

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

  private void setPatientListAdapter(final ArrayList<PatientListVO> arrayList) {
    mActivity.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        instance.dismissProgress();
        instance.centerMenu.clearPatientInfo();
        if (arrayList.size() == 0) {
          instance.patientEmpty.setVisibility(View.VISIBLE);
        } else {
          instance.patientEmpty.setVisibility(View.GONE);
        }
        if (patientListAdapter == null) {
          linearLayoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
          instance.patientRecyclerView.setLayoutManager(linearLayoutManager);
          patientListAdapter = new PatientListAdapter(mActivity, mContext, arrayList, instance.indexPage);
          instance.patientRecyclerView.setAdapter(patientListAdapter);
//      patientListAdapter.setOnClickEvent(onClickEvent);
        } else {
          patientListAdapter.arrayList.clear();
          patientListAdapter.arrayList = arrayList;
          patientListAdapter.click = false;
          patientListAdapter.notifyDataSetChanged();
        }
      }
    });

  }
}
