package com.dbs.mplus.fatima.consent;

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

import com.dbs.mplus.fatima.httpTask.HttpCon;
import com.dbs.mplus.fatima.model.ConsentListVO;
import com.dbs.mplus.fatima.model.ConsentSetListVO;
import com.dbs.mplus.fatima.model.ConsentWriteListVO;
import com.dbs.mplus.fatima.model.PatientInfoVO;
import com.dbs.mplus.fatima.util.ConsentConfig;
import com.dbs.mplus.fatima.util.Util;

import org.json.JSONObject;

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

public class ConsentParameter {
  private static final String TAG = "ConsentParameter";

  public ConsentParameter() {

  }

  public static HashMap<String, String> makeGlobalParam(PatientInfoVO patientInfoVO, String signDoctorId, Context mContext) {
    HashMap<String, String> result = new HashMap<String, String>();

    if (patientInfoVO == null) {
    } else {
      String nowDate = Util.logTime();
      String signImage = "";

      HashMap<String, String> mData = new HashMap<String, String>();
      mData.put("userid", signDoctorId);

      HttpCon httpCon = new HttpCon(ConsentConfig.HOST_HOSPITAL, mData, ConsentConfig.GET_SIGN_IMAGE);

      try {
        String rts = httpCon.execute().get();
        JSONObject object = Util.xmltoJsonObject(rts);
        String res = Util.xmlToJsonString(object);
        ArrayList<HashMap<String, String>> mapArrayList = Util.parseJSON(mContext, res);
        if (mapArrayList.size() > 0) {
          signImage = mapArrayList.get(0).get("SignImage");
        }

      } catch (ExecutionException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      result.put("userid", signDoctorId);
      result.put("IO_device", "M");
      result.put("visitType", patientInfoVO.getVisitType());
      result.put("IO_signdate", nowDate);
      result.put("IO_signtime", nowDate);
      result.put("IO_printtime", nowDate);
      result.put("IO_maindr", patientInfoVO.getMainDrNm().equals("") ? "" : patientInfoVO.getMainDrNm());
      result.put("IO_SIGNIMG", signImage);

      // 2017.8.22 이전 작업)) 응급일 경우 ioerdrNm에 실제 주치의가 들어오기때문에 이걸로 변경해줌.
      // 2017.8.22 - 응급인 경우 조건 추가 (기존에 visitType 체크가 없어서 응급통해서 입원한경우(?) 문제됨)
      // 병원 담당자 확인 받고 작업한 내용임
      if (patientInfoVO.getVisitType().equals("E") && !patientInfoVO.getErDrNm().equals("")) {
        result.put("IO_maindr", patientInfoVO.getErDrNm());
      }
    }

    return result;
  }

  public static ArrayList<HashMap<String, Object>> makeNewConsentParam(PatientInfoVO patientInfoVO, ArrayList consentList, String userName, Context mContext) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    for (int i = 0; i < consentList.size(); i++) {
      ConsentSetListVO consentSetListVO = (ConsentSetListVO) consentList.get(i);

      String ocrTag = getOcrTag(mContext);
      HashMap<String, Object> consentItem = new HashMap<String, Object>();
      consentItem.put("newConsent", "true");
      consentItem.put("hospitalCretno", patientInfoVO.getCretNo());
      consentItem.put("ward", patientInfoVO.getWard());
      consentItem.put("roomNo", patientInfoVO.getRoomNo());
      consentItem.put("IO_ocr_cd", ocrTag);
      consentItem.put("outDate", patientInfoVO.getDschDd());
      consentItem.put("mainDrId", patientInfoVO.getMainDrId());

      consentItem.put("guid", consentSetListVO.getFormGuid());
      consentItem.put("IO_formname", consentSetListVO.getFormName());
      consentItem.put("IO_INPUTNM", userName);
      consentItem.put("formCd", consentSetListVO.getFormCd());
      consentItem.put("formRid", consentSetListVO.getFormRid());

      consentItem.put("IO_Pt_ID", patientInfoVO.getPid());
      consentItem.put("IO_sex_age_y_m", patientInfoVO.getSa());
      consentItem.put("IO_Pt_name", patientInfoVO.getPatientNm());
      consentItem.put("IO_JuminNo", patientInfoVO.getJuminNo());
      consentItem.put("IO_PT_birthday", patientInfoVO.getJuminNo().split("-")[0]);
      consentItem.put("IO_roomNo", patientInfoVO.getRoomNo());
      consentItem.put("IO_ADdate", Util.addSlashDate(patientInfoVO.getAdDate()));
      consentItem.put("IO_Dept", patientInfoVO.getDeptHngNm());
      consentItem.put("IO_Dept2", patientInfoVO.getDeptEngNm());
      consentItem.put("IO_DeptCd", patientInfoVO.getDeptCd());
//      consentItem.put("IO_OPdept", patientInfoVO.getDeptHngNm());
      consentItem.put("IO_OPdept", "");
      consentItem.put("IO_OPdr", patientInfoVO.getOpDrNm());
      consentItem.put("IO_Dx", patientInfoVO.getDxNm());
      consentItem.put("IO_bp", patientInfoVO.getBp());
      consentItem.put("IO_dm", patientInfoVO.getDm());
      consentItem.put("IO_heart", patientInfoVO.getHeart());
      consentItem.put("IO_kidney", patientInfoVO.getKidney());
      consentItem.put("IO_respiration", patientInfoVO.getRespiration());
      consentItem.put("IO_hx", patientInfoVO.getHx());
      consentItem.put("IO_allergy", patientInfoVO.getAllergy());
      consentItem.put("IO_drug", patientInfoVO.getDrug());
      consentItem.put("IO_smoking", patientInfoVO.getSmoking());
      consentItem.put("IO_idio", patientInfoVO.getIdio());
      consentItem.put("IO_nacrotics", patientInfoVO.getNacrotics());
      consentItem.put("IO_airway", patientInfoVO.getAirway());
      consentItem.put("IO_hemorrhage", patientInfoVO.getHemorrhage());
      consentItem.put("IO_status_etc", patientInfoVO.getStatusEtc());

      if (consentItem.get("ward").toString().length() > 0 && consentItem.get("roomNo").toString().length() > 0) {
        consentItem.put("IO_roomNo", consentItem.get("ward") + "/" + consentItem.get("roomNo"));
      } else {
        consentItem.put("IO_roomNo", "");
      }

      // 외과>소화기내과인 경우 시술의, 수술의 설정. 2017/1/3 고객요구사항
      if (patientInfoVO.getVisitType().equals("O") && patientInfoVO.getDeptCd().equals("2010300000")) {
        consentItem.put("IO_opdr", patientInfoVO.getMainDrNm());
      }

      result.add(consentItem);
    }

    return result;
  }

  public static ArrayList<HashMap<String, Object>> makeConsent(PatientInfoVO patientInfoVO, ArrayList consentList, String userName, String type, Context mContext) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();

    for (int i = 0; i < consentList.size(); i ++) {
      ConsentListVO consentListVO = null;
      ConsentWriteListVO consentWriteListVO = null;

      String orderNo = "";
      String orderCd = "";
      String ocrNumber = "";
      String cretNo = "";
      String consentState = "";
      String consentMstRid = "";
      String consentName = "";
      String formCd = "";
      String formRid = "";
      String formGuid = "";

      if (type.equals("patient")) {
        consentListVO = (ConsentListVO) consentList.get(i);
        orderNo = consentListVO.getOrderNo();
        orderCd = consentListVO.getOrderCd();
        ocrNumber = consentListVO.getOcrNumber();
        cretNo = consentListVO.getCretno();
        consentState = consentListVO.getConsentState();
        consentMstRid = consentListVO.getConsentMstRid();
        consentName = consentListVO.getConsentName();
        formCd = consentListVO.getFormCd();
        formRid = consentListVO.getFormRid();
        formGuid = consentListVO.getFormGuid();
      } else {
        consentWriteListVO = (ConsentWriteListVO) consentList.get(i);
        orderNo = consentWriteListVO.getOrderNo();
        orderCd = consentWriteListVO.getOrderCd();
        ocrNumber = consentWriteListVO.getOcrNumber();
        cretNo = consentWriteListVO.getCretNo();
        consentState = consentWriteListVO.getConsentState();
        consentMstRid = consentWriteListVO.getConsentMstRid();
        consentName = consentWriteListVO.getFormNm();
        formCd = consentWriteListVO.getFormCd();
        formRid = consentWriteListVO.getFormRid();
        formGuid = consentWriteListVO.getFormGuid();
      }

      HashMap<String, Object> consentItem = new HashMap<String, Object>();

//      String ocrTag = getOcrTag();
      String newConsent = "false";
      if (consentState.equals(ConsentConfig.UNFINISHED)) {
        newConsent = "true";
      }

      consentItem.put("newConsent", newConsent);
      consentItem.put("hospitalCretno", patientInfoVO.getCretNo());
      consentItem.put("ward", patientInfoVO.getWard());
      consentItem.put("roomNo", patientInfoVO.getRoomNo());
      consentItem.put("orderNo", orderNo);
      consentItem.put("orderCd", orderCd);
      consentItem.put("IO_ocr_cd", ocrNumber);
      consentItem.put("consentCretno", cretNo);
      consentItem.put("outDate", patientInfoVO.getDschDd());
      consentItem.put("mainDrId", patientInfoVO.getMainDrId());
      consentItem.put("consentState", consentState);

      consentItem.put("rid", consentMstRid);
      consentItem.put("IO_formname", consentName);
      consentItem.put("IO_INPUTNM", userName);
      consentItem.put("formCd", formCd);
      consentItem.put("formRid", formRid);

      consentItem.put("IO_Pt_ID", patientInfoVO.getPid());
      consentItem.put("IO_sex_age_y_m", patientInfoVO.getSa());
      consentItem.put("IO_Pt_name", patientInfoVO.getPatientNm());
      consentItem.put("IO_JuminNo", patientInfoVO.getJuminNo());
      consentItem.put("IO_PT_birthday", patientInfoVO.getJuminNo().split("-")[0]);
      consentItem.put("IO_ADdate", patientInfoVO.getAdDate());
      consentItem.put("IO_Dept", patientInfoVO.getDeptHngNm());
      consentItem.put("IO_Dept2", patientInfoVO.getDeptEngNm());
      consentItem.put("IO_DeptCd", patientInfoVO.getDeptCd());
      consentItem.put("IO_OPdept", patientInfoVO.getOpDeptNm());
      consentItem.put("IO_OPdr", patientInfoVO.getOpDrNm());
      consentItem.put("IO_Dx", patientInfoVO.getDxNm());
      consentItem.put("IO_bp", patientInfoVO.getBp());
      consentItem.put("IO_dm", patientInfoVO.getDm());
      consentItem.put("IO_heart", patientInfoVO.getHeart());
      consentItem.put("IO_kidney", patientInfoVO.getKidney());
      consentItem.put("IO_respiration", patientInfoVO.getRespiration());
      consentItem.put("IO_hx", patientInfoVO.getHx());
      consentItem.put("IO_allergy", patientInfoVO.getAllergy());
      consentItem.put("IO_drug", patientInfoVO.getDrug());
      consentItem.put("IO_smoking", patientInfoVO.getSmoking());
      consentItem.put("IO_idio", patientInfoVO.getIdio());
      consentItem.put("IO_nacrotics", patientInfoVO.getNacrotics());
      consentItem.put("IO_airway", patientInfoVO.getAirway());
      consentItem.put("IO_hemorrhage", patientInfoVO.getHemorrhage());
      consentItem.put("IO_status_etc", patientInfoVO.getStatusEtc());

      if (newConsent.equals("true")) {
        consentItem.put("guid", formGuid);
      }

      if (consentItem.get("ward").toString().length() > 0 && consentItem.get("roomNo").toString().length() > 0) {
        consentItem.put("IO_roomNo", consentItem.get("ward") + "/" + consentItem.get("roomNo"));
      } else {
        consentItem.put("IO_roomNo", "");
      }

      if (patientInfoVO.getVisitType().equals("O") && patientInfoVO.getDeptCd().equals("2010300000")) {
        consentItem.put("IO_OPdr", patientInfoVO.getMainDrNm());
      }

      result.add(consentItem);
    }

    return result;
  }

  private static String getOcrTag(Context mContext) {
    String ocrTag = "";
    HashMap<String, String> mData = new HashMap<String, String>();
    HttpCon httpCon = new HttpCon(ConsentConfig.HOST_HOSPITAL, mData, ConsentConfig.GET_OCR_TAG);
    try {
      String result = httpCon.execute().get();
      JSONObject object = Util.xmltoJsonObject(result);
      String res = Util.xmlToJsonString(object);
      ArrayList<HashMap<String, String>> mapArrayList = Util.parseJSON(mContext, res);
      ocrTag = mapArrayList.get(0).get("GetOcrTagResult");
    } catch (ExecutionException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return ocrTag;
  }
}
