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

import android.app.DatePickerDialog;
import android.content.Context;
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.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

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

import com.dbs.mplus.fatima.R;
import com.dbs.mplus.fatima.activity.ConsentActivity;
import com.dbs.mplus.fatima.adapter.DeptSpinnerAdapter;
import com.dbs.mplus.fatima.adapter.PatientListAdapter;
import com.dbs.mplus.fatima.customView.CustomRadioGroup;
import com.dbs.mplus.fatima.httpTask.DataParser;
import com.dbs.mplus.fatima.httpTask.HttpCallBack;
import com.dbs.mplus.fatima.model.DeptListVO;
import com.dbs.mplus.fatima.model.DoctorListVO;
import com.dbs.mplus.fatima.model.PatientListVO;
import com.dbs.mplus.fatima.util.ConsentConfig;
import com.dbs.mplus.fatima.util.Util;

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

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

  private String patientState = "";
  private HashMap<String, String> patientSearchMap = null;

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

  public LeftMenu(AppCompatActivity mActivity, Context mContext) {
    this.mActivity = mActivity;
    this.mContext  = mContext;
    this.instance  = (ConsentActivity) mContext;
    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(mContext, R.style.DatePickerDialogTheme, onDateSetListener, year, month, day);
  }

  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.all);
      radioStr2 = mContext.getString(R.string.discharge);
      radioStr3 = mContext.getString(R.string.hospitalization);
      radioStr4 = mContext.getString(R.string.stay);
      radioStr5 = 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.all);
      radioStr2 = mContext.getString(R.string.stay);
      radioStr3 = mContext.getString(R.string.leaveClear);
      radioStr4 = mContext.getString(R.string.discharge);
      radioStr5 = mContext.getString(R.string.noWrite);
    } else if (indexPage.equals("P")) {
      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);
    }

    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);

      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);
    }

//    if (instance.jobKindCd.indexOf("03") == 0 || instance.mobile.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";
//        }
//      }
//    }

    if (instance.jobKindCd.indexOf("03") == 0) {
      instance.radioButton1.setChecked(true);
      patientState = "";
      if (indexPage.equals("OP")) {
        patientState = "A";
      } else if (indexPage.equals("SR")) {
        patientState = "I";
      }

    } else {
      if (indexPage.equals("OP")) {
        instance.radioButton1.setChecked(true);
        patientState = "A";
      } else {
        if (indexPage.equals("SR")) {
          instance.radioButton1.setChecked(true);
          patientState = "I";
        } else {
          patientState = "C";
          instance.radioButton5.setChecked(true);
        }

      }
    }

    Log.e(TAG, "draw radioButton patientState -- > " + patientState);
    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);
    }
  };

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

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

  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 EditText.OnEditorActionListener onEditorActionListener = new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
        if (v == instance.etPatientId) {
          getPatientList();
        }
      }
      return false;
    }
  };

  public void getWardList() {
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(String result) {
        if (result.equals("")) {
          stop();
        } else {
          ArrayList<DeptListVO> arrayList = DataParser.getWardNDeptList(mContext, 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.all)));
        setSpinnerAdapter(arrayList, ConsentConfig.GET_WARD_LIST);
      }

      @Override
      public void error() {

      }
    };

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

  public void getDeptList() {
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(String result) {
        if (result.equals("")) {
          stop();
        } else {
          ArrayList<DeptListVO> arrayList = DataParser.getWardNDeptList(mContext, 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.mobile);
    if (Util.checkNetwork(mContext) == true) {
      Util.callHttp(mContext, ConsentConfig.HOST_HOSPITAL, ConsentConfig.GET_DEPT_LIST, mData, callBack);
    } else {
      instance.dismissProgress();
      Util.networkErrorAlert(mActivity);
    }
  }

  private void getDoctorList(String deptCd) {
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(String result) {
        if (result.equals("")) {
          stop();
        } else {
          ArrayList<DoctorListVO> arrayList = DataParser.getDoctorList(mContext, 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("srchdd", getDate());
    mData.put("orddeptcd", deptCd);
    if (Util.checkNetwork(mContext) == true) {
      Util.callHttp(mContext, ConsentConfig.HOST_HOSPITAL, ConsentConfig.GET_DOCTOR_LIST, mData, callBack);
    } else {
      instance.dismissProgress();
      Util.networkErrorAlert(mActivity);
    }
  }

  public void getInitPatientList() {
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(String result) {
        if (result.equals("")) {
        } else {
          ArrayList<PatientListVO> arrayList = DataParser.getPatientList(mContext, result);
          setPatientListAdapter(arrayList);
        }
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

    String url = "";
    if (instance.mobile.equals("I")) {
      url = ConsentConfig.GET_IN_PATIENT_LIST;
    } else if (instance.mobile.equals("O")) {
      url = ConsentConfig.GET_OUT_PATIENT_LIST;
    } else if (instance.mobile.equals("P")) {
      url = ConsentConfig.GET_OP_PATIENT_LIST;
    } else if (instance.mobile.equals("E")) {
      url = ConsentConfig.GET_ER_PATIENT_LIST;
    } else if (instance.mobile.equals("SR")) {
      url = ConsentConfig.GET_SR_PATIENT_LIST;
    }

    instance.showProgress();
    HashMap<String, String> mData = new HashMap<String, String>();
    mData.put("srchdd", getDate());
    mData.put("patstat", patientState);
    mData.put("jobkindcd", instance.jobKindCd);
    mData.put("pid", instance.etPatientId.getText().toString());
    mData.put("orddeptcd", instance.dutplcedeptCd);
    mData.put("doctorid", "");
    mData.put("ordtype", patientState);
    if (Util.checkNetwork(mContext) == true) {
      Util.callHttp(mContext, ConsentConfig.HOST_HOSPITAL, url, mData, callBack);
    } else {
      instance.dismissProgress();
      Util.networkErrorAlert(mActivity);
    }
  }

  private void getPatientList() {
    HttpCallBack callBack = new HttpCallBack() {
      @Override
      public void result(String result) {
        if (result.equals("")) {
        } else {
          ArrayList<PatientListVO> arrayList = DataParser.getPatientList(mContext, result);
          setPatientListAdapter(arrayList);
        }
      }

      @Override
      public void stop() {

      }

      @Override
      public void error() {

      }
    };

    String url = "";
    if (instance.mobile.equals("I")) {
      url = ConsentConfig.GET_IN_PATIENT_LIST;
    } else if (instance.mobile.equals("O")) {
      url = ConsentConfig.GET_OUT_PATIENT_LIST;
    } else if (instance.mobile.equals("P")) {
      url = ConsentConfig.GET_OP_PATIENT_LIST;
    } else if (instance.mobile.equals("E")) {
      url = ConsentConfig.GET_ER_PATIENT_LIST;
    } else if (instance.mobile.equals("SR")) {
      url = ConsentConfig.GET_SR_PATIENT_LIST;
    }

    instance.showProgress();
    patientSearchMap.put("srchdd", getDate());
    patientSearchMap.put("patstat", patientState);
    patientSearchMap.put("jobkindcd", instance.jobKindCd);
    patientSearchMap.put("pid", instance.etPatientId.getText().toString());
    patientSearchMap.put("ordtype", patientState);
    patientSearchMap.put("jobkindcd", instance.jobKindCd);
    Log.e(TAG, "url -- > " + url);
    Log.e(TAG, "patientsearchMap -- > " + patientSearchMap);
    if (Util.checkNetwork(mContext) == true) {
      Util.callHttp(mContext, ConsentConfig.HOST_HOSPITAL, url, patientSearchMap, 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.dutplcedeptCd.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.dutplcedeptCd.equals(_deptCd)) {
              instance.deptSpinner.setSelection(i);
            }
          }

        } else if (apiType.equals(ConsentConfig.GET_DOCTOR_LIST)) {
          instance.dismissProgress();
          instance.doctorSpinner.setAdapter(deptSpinnerAdapter);
          instance.doctorSpinner.setOnItemSelectedListener(onItemSelectedListener);

        }
      }
    });
  }

  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) {
        patientSearchMap.put("wardcd",deptCd);
      } else if (parent == instance.deptSpinner) {
        if (!deptCd.equals("")) {
          instance.showProgress();
          getDoctorList(deptCd);
        } else {
          ArrayList<DoctorListVO> arrayList = new ArrayList<DoctorListVO>();
          arrayList.add(new DoctorListVO("", mContext.getString(R.string.doctorListEmpty)));
          setSpinnerAdapter(arrayList, ConsentConfig.GET_DOCTOR_LIST);
        }

        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 (instance.mobile.equals("I")) {
        if (checkedId == instance.radioButton1.getId()) {
          patientState = "";
        } else if (checkedId == instance.radioButton2.getId()) {
          patientState = "D";
        } else if (checkedId == instance.radioButton3.getId()) {
          patientState = "P";
        } else if (checkedId == instance.radioButton4.getId()) {
          patientState = "F";
        } else if (checkedId == instance.radioButton5.getId()) {
          patientState = "C";
        }

      } else if (instance.mobile.equals("O")) {
        if (checkedId == instance.radioButton1.getId()) {
          patientState = "";
        } else if (checkedId == instance.radioButton2.getId()) {
          patientState = "0";
        } else if (checkedId == instance.radioButton3.getId()) {
          patientState = "1";
        } else if (checkedId == instance.radioButton4.getId()) {
          patientState = "2";
        } else if (checkedId == instance.radioButton5.getId()) {
          patientState = "C";
        }
      } else if (instance.mobile.equals("R")) {
        if (checkedId == instance.radioButton1.getId()) {
          patientState = "";
        } else if (checkedId == instance.radioButton2.getId()) {
          patientState = "1";
        } else if (checkedId == instance.radioButton3.getId()) {
          patientState = "2";
        } else if (checkedId == instance.radioButton4.getId()) {
          patientState = "3";
        } else if (checkedId == instance.radioButton5.getId()) {
          patientState = "C";
        }
      } else if (instance.mobile.equals("P")) {
        if (checkedId == instance.radioButton1.getId()) {
          patientState = "A";
        } else if (checkedId == instance.radioButton2.getId()) {
          patientState = "M";
        } else if (checkedId == instance.radioButton3.getId()) {
          patientState = "R";
        } else if (checkedId == instance.radioButton4.getId()) {
          patientState = "I";
        } else if (checkedId == instance.radioButton5.getId()) {
          patientState = "F";
        }
      } else if (instance.mobile.equals("SR")) {
        if (checkedId == instance.radioButton1.getId()) {
          patientState = "I";
        } else if (checkedId == instance.radioButton2.getId()) {
          patientState = "O";
        } else if (checkedId == instance.radioButton3.getId()) {
          patientState = "ER";
        }
      }
    }
  };

  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.mobile);
          instance.patientRecyclerView.setAdapter(patientListAdapter);
//      patientListAdapter.setOnClickEvent(onClickEvent);
        } else {
          patientListAdapter.arrayList.clear();
          patientListAdapter.arrayList = arrayList;
          patientListAdapter.click = false;
          patientListAdapter.notifyDataSetChanged();
        }
      }
    });

  }
}
