package com.dbs.dbsec.consent;

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

import com.dbs.dbsec.R;
import com.dbs.dbsec.activity.ConsentActivity;
import com.dbs.dbsec.httpTask.CallBack;
import com.dbs.dbsec.httpTask.HttpCon;
import com.dbs.dbsec.httpTask.HttpRunnable;
import com.dbs.dbsec.model.PatientInfoVO;
import com.dbs.dbsec.util.Common;
import com.dbs.dbsec.util.ConsentConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

public class ConsentCreateParam {
  private static final String TAG = ConsentCreateParam.class.getSimpleName();

  private Context       mContext;
  private PatientInfoVO patientInfoVO;
  private String        indexPage = "";

  public ConsentCreateParam(Context mContext, PatientInfoVO patientInfoVO) {
    this.mContext = mContext;
    this.patientInfoVO = patientInfoVO;
    this.indexPage = ((ConsentActivity) mContext).indexPage;
  }

  public HashMap<String, String> globalParam(String consentMstRid) {
    HashMap<String, String> globalParam = new HashMap<String, String>();

    int rid;
    if (consentMstRid.equals("")) {
      rid = 0;
    } else {
      rid = Integer.parseInt(consentMstRid);
    }

    String userId     = Common.getStringPreference(mContext, "userInfo", "userId");
    String userName   = Common.getStringPreference(mContext, "userInfo", "userName");
    String userDeptCd = Common.getStringPreference(mContext, "userInfo", "deptCd");
    String drKind     = Common.getStringPreference(mContext, "userInfo", "drKind");

    if (rid == 0) {

      String ordTypeStr  = "";
      String ordDeptCd   = patientInfoVO.getOrdDeptCd();
      String deptEngAbbr = patientInfoVO.getDeptEngAbbr();
      String ordDeptNm   = patientInfoVO.getOrdDeptNm();
      String pid         = patientInfoVO.getPid();
      String cretNo      = patientInfoVO.getCretNo();
      String roomCd      = patientInfoVO.getRoomCd();
      String inDd        = patientInfoVO.getInDd();
      String dschDd      = patientInfoVO.getDschDd();
      String ordType     = patientInfoVO.getOrdType();
      String instCd      = patientInfoVO.getInstCd();
      String mainDrId    = patientInfoVO.getMainDrId();
      String erDrNm      = patientInfoVO.getErDrNm();
      String firstRgstDt = patientInfoVO.getFirstRgstDt();
      String sa          = patientInfoVO.getSa();
      String patientName = patientInfoVO.getPatientName();
      String juminNo     = patientInfoVO.getJuminNo();
      String zipCdAddr   = patientInfoVO.getZipCdAddr();
      String telNum      = patientInfoVO.getTelNum();
      String insuKind    = patientInfoVO.getInsuKind();
      String ward        = patientInfoVO.getWard();
      String dxCd        = patientInfoVO.getDxCd();
      String dxNm        = patientInfoVO.getDxNm();
      String gb          = patientInfoVO.getGb();
      String gbn         = patientInfoVO.getGbn();
      String opDeptCd    = patientInfoVO.getOpDeptCd();
      String opDeptNm    = patientInfoVO.getOpDeptNm();
      String opDrNm      = patientInfoVO.getOpDrNm();
      String opDrId      = patientInfoVO.getOpDrId();
      String opCnfmDd    = patientInfoVO.getOpCnfmDd();
      String perfDrNm    = patientInfoVO.getPerfDrNm();
      String perfDrFlag  = patientInfoVO.getPerfDrFlag();
      String perfDrDept  = patientInfoVO.getPerfDrDept();
      String opDiagNm    = patientInfoVO.getOpDiagNm();
      String opNm        = patientInfoVO.getOpNm();
      String anstDrNm1   = patientInfoVO.getAnstDrNm1();
      String anstDrFlag1 = patientInfoVO.getAnstDrFlag1();
      String anstDeptNm1 = patientInfoVO.getAnstDeptNm1();
      String anstDrNm2   = patientInfoVO.getAnstDrNm2();
      String anstDrFlag2 = patientInfoVO.getAnstDrFlag2();
      String anstDeptNm2 = patientInfoVO.getAnstDeptNm2();
      String anstDrNm3   = patientInfoVO.getAnstDrNm3();
      String anstDrFlag3 = patientInfoVO.getAnstDrFlag3();
      String anstDeptNm3 = patientInfoVO.getAnstDeptNm3();
      String opRsrvNo    = patientInfoVO.getOpRsrvNo();
      String orderNo     = patientInfoVO.getOrderNo();
      String printTime   = patientInfoVO.getPrintTime();
      String ocrCd       = patientInfoVO.getOcrCd();
      String inputId     = patientInfoVO.getInputId();
      String inputNm     = patientInfoVO.getInputNm();
      String signImg     = patientInfoVO.getSignImg();
      String bp          = patientInfoVO.getBp();
      String dm          = patientInfoVO.getDm();
      String heart       = patientInfoVO.getHeart();
      String kidney      = patientInfoVO.getKidney();
      String respiration = patientInfoVO.getRespiration();
      String hx          = patientInfoVO.getHx();
      String allergy     = patientInfoVO.getAllergy();
      String drug        = patientInfoVO.getDrug();
      String smoking     = patientInfoVO.getSmoking();
      String idio        = patientInfoVO.getIdio();
      String nacrotics   = patientInfoVO.getNacrotics();
      String airway      = patientInfoVO.getAirway();
      String hemorrhage  = patientInfoVO.getHemorrhage();
      String statusEt    = patientInfoVO.getStatusEt();

      if (ordType.equals("I")) {
        ordTypeStr = mContext.getString(R.string.admission);
      } else if (ordType.equals("O")) {
        ordTypeStr = mContext.getString(R.string.outPatient);
      } else if (ordType.equals("E")) {
        ordTypeStr = mContext.getString(R.string.emergency);
      } else if (ordType.equals("OP")) {
        ordTypeStr = mContext.getString(R.string.surgery);
      }

      globalParam.put("par_explDrNm", userName);
      globalParam.put("par_patientCretno", cretNo);
      globalParam.put("par_patientId", pid);
      globalParam.put("par_patientOrdtype", ordTypeStr);
      globalParam.put("par_patientNm", patientName);
      globalParam.put("par_patientSexAge", sa);
      globalParam.put("par_patientRrn", juminNo);
      globalParam.put("par_patientClnDeptCd", "");
      globalParam.put("par_patientClnDeptNm", "");
      globalParam.put("par_patientClnDt", inDd);
      globalParam.put("par_patientBirthday", "");
      globalParam.put("par_patientTelNo", "");
      globalParam.put("par_patientMpNo", telNum);
      globalParam.put("par_patientAddress", zipCdAddr);
      globalParam.put("par_patientWardNo", ward);
      globalParam.put("par_patientRoomNo", roomCd);
      globalParam.put("par_explMpNo", ""); // 설명의사 연락처
      globalParam.put("par_explAnstDrNm", "");
      globalParam.put("par_explAnstDrMpNo", "");
      globalParam.put("par_diagHngnm", opDrNm);
      globalParam.put("par_opNm", opNm);
      globalParam.put("par_opremFact", "");
      globalParam.put("par_opRsrvNo", opRsrvNo);
      globalParam.put("par_opCnFmDd", opCnfmDd);
//      globalParam.put("par_mainDr1Nm", mainDrNm);
      globalParam.put("par_prefDr1Dept", "");
      globalParam.put("par_geDr1Dept", "");
      globalParam.put("par_anstDr1Nm", "");
      globalParam.put("par_anstDr1PrefDrDept", "");
      globalParam.put("par_anstDr1GeDrDept", "");
      globalParam.put("par_userDeptCd", userDeptCd);
      globalParam.put("par_userId", userId);
      globalParam.put("par_userNm", userName);

      globalParam.put("ordType", ordType);
      globalParam.put("patientDeptCd", ordDeptCd);
      globalParam.put("mainDrId", mainDrId);
      globalParam.put("dSchDd", dschDd);
    }
    return globalParam;
  }

  public ArrayList<HashMap<String, String>> consentParam(String consentMstRid, String formId, String formCode, String formName, String pid, String ocrTag, String clnDate, String cretNo, String ordType, String deptCd, String mainDrId) {
    ArrayList<HashMap<String, String>> consentArray = new ArrayList<>();
    HashMap<String, String> consentParam = new HashMap<>();
    int rid = Integer.parseInt(consentMstRid);

    consentParam.put("consentMstRid", consentMstRid);
    consentParam.put("formId", formId);
    consentParam.put("formCode", formCode);

    if (patientInfoVO != null) {
      consentParam.put("par_mainDr1Nm", patientInfoVO.getMainDrName());
    }

    if (rid == 0) {
      if (ocrTag.equals("") || ocrTag.isEmpty()) {
        HashMap<String, String> mData = new HashMap<String, String>();
        HttpCon httpCon = new HttpCon(ConsentConfig.GET_OCR_TAG, mData);
        try {
          String result = httpCon.execute().get().trim();
          ocrTag = result;

          consentParam.put("par_formNm", formName);
          consentParam.put("par_ocrCd", ocrTag);

        } catch (ExecutionException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    } else {
      consentParam.put("par_patientClnDt", clnDate);
      consentParam.put("par_patientId", pid);
      consentParam.put("ordType", ordType);
      consentParam.put("par_ocrCd", ocrTag);
      consentParam.put("patientDeptCd", deptCd);
      consentParam.put("mainDrId", mainDrId);
      consentParam.put("par_patientCretno", cretNo);
    }

    consentArray.add(consentParam);
    return consentArray;
  }
}
