package com.dbs.consentServer.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

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

import com.dbs.consentServer.DTO.hospitalDTO.PatientInfoDTO;
import com.dbs.consentServer.DTO.hospitalDTO.PatientListDTO;
import com.dbs.consentServer.VO.hospitalVO.ConsentFormCategoryVO;
import com.dbs.consentServer.VO.hospitalVO.GetDoctorVO;
import com.dbs.consentServer.VO.hospitalVO.GetPatientInfoVO;
import com.dbs.consentServer.VO.hospitalVO.GetPatientVO;
import com.dbs.consentServer.VO.hospitalVO.GetUserDeptVO;
import com.dbs.consentServer.VO.hospitalVO.GetWardDeptVO;

import com.dbs.consentServer.service.HospitalService;
import com.dbs.consentServer.util.CommonUtils;

@Api(tags = {"2. HospitalService "})
@RestController
@RequestMapping("/hospitalSvc")
public class HospitalSvc {

  private final Logger logger = LoggerFactory.getLogger(this.getClass());
  @Autowired
  private HospitalService hospitalService;

  /**
   * 기본정보 조회
   * @param instCd
   * @return
   */
  @RequestMapping(path="/GetBaseData", method=RequestMethod.POST)
  public ArrayList<HashMap<String, String>> GetBaseData(String instCd) {
    ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    String baseData = hospitalService.hospitalMapper.getBaseData(instCd);

    if (baseData == null) {
      baseData = "";
    }

    if (baseData.equals("")) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", baseData);
    }

    result.add(map);
    return result;
  }

  /**
   * 기본 탭 설정(입원, 외래 등)
   * @param userId 사용자 아이디
   * @param status 상태 (입원 : I)
   * @param instCd
   */
  @RequestMapping(path="updateUserSetUp", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> updateUserSetup(String userId, String status, String instCd) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    HashMap<String, String> params = new HashMap<String, String>();

    params.put("instCd", instCd);
    params.put("status", status);
    params.put("userId", userId);
    
    int rts = hospitalService.updateUserSetUp(params);

    if (rts == 0) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", rts);
    }

    result.add(map);

    return result;
  }
  
  /**
   * 로그인 프로세스
   * @param userId 사용자 ID
   * @param userPw 사용자 PW
   * @param dutInstCd 기관코드
   * @param certSucc 인증여부(문자열로 내려옴, true면 공인인증서 로그인을 통해 들어온것)
   * @return
   */
  @RequestMapping(path="/doLogin", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> doLogin(final String userId, final String userPw, final String dutInstCd, final String certSucc) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();

    String encryptPw = hospitalService.hospitalMapper.doLogin(userId);

    if (encryptPw == null) {
      encryptPw = "";
    }

    if (certSucc.equals("false")) {
      if (!encryptPw.equals(userPw)) {
        map.put("code", "02");
        return result;
      }
    }

    String sysCd = "HIS031";
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("userId", userId);
    params.put("sysCd", sysCd);
    ArrayList<GetUserDeptVO> userDeptList = hospitalService.hospitalMapper.getUserDeptList(params);

    map.put("code", "00");
    map.put("data", userDeptList);
    result.add(map);
    return result;
  }

  /**
   * 병동 리스트
   * @param instCd
   * @return
   */
  @RequestMapping(path="/getWardList", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> getWardList(final String instCd) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    final ArrayList<GetWardDeptVO> wardList = hospitalService.getWardList(instCd);
    
    if (wardList.size() == 0) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", wardList);
    }

    result.add(map);
    return result;
  }

  /**
   * 진료과 리스트
   * @param instCd
   * @param ordType
   * @return
   */
  @RequestMapping(path="getDeptList", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> getDeptList(String instCd, String ordType) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<>();
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("instCd", instCd);
    params.put("ordType", ordType);

    ArrayList<GetWardDeptVO> deptList = hospitalService.getDeptList(params);

    if (deptList.size() == 0) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", deptList);
    }

    result.add(map);
    return result;
  }

  /**
   * 진료과에 따른 진료의 리스트
   * @param instCd
   * @param ordDeptCd
   * @param srchDd
   * @return
   */
  @RequestMapping(path="getDoctorList", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> getDoctorList(String instCd, String ordDeptCd, String srchDd) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("instCd", instCd);
    params.put("ordDeptCd", ordDeptCd);
    params.put("srchDd", srchDd);

    ArrayList<GetDoctorVO> doctorList = hospitalService.getDoctorList(params);
    if (doctorList.size() == 0) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", doctorList);
    }

    result.add(map);
    return result;
  }

  /**
   * 환자 리스트 조회
   * @param patientListDTO
   * @return
   */
  @RequestMapping(path="getPatientList", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> getPatientList(PatientListDTO patientListDTO) {

    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    ArrayList<GetPatientVO> patientList = new ArrayList<>();

    if (patientListDTO == null) {
      map.put("code", "01");
      map.put("data", patientList);
      result.add(map);
      return result;
    }

    String selectFlag   = patientListDTO.getSelectFlag();
    String patientState = patientListDTO.getPatientState();
    String myPatient    = patientListDTO.getMyPatient();

    logger.debug("patientState -- > " + patientListDTO.getPatientState());

    if (selectFlag.equals("SR")) {
      if (patientState.equals("A")) {
        selectFlag = "I";
      } else if (patientState.equals("0")) {
        selectFlag = "O";
      } else if (patientState.equals("1")) {
        selectFlag = "1";
      }

      patientListDTO.setPatientState("");
    }

    if (myPatient.equals("")) {
      myPatient = "N";
    }

    patientListDTO.setMyPatient(myPatient);

    patientList = hospitalService.getPatientList(patientListDTO);

    if (patientList.size() == 0) {
      map.put("code", "02");
    } else {
      map.put("code", "00");
    }

    map.put("data", patientList);
    result.add(map);
    return result;
  }

  /**
   * 환자상세 정보 조회
   * @param patientInfoDTO
   * @return
   */
  @RequestMapping(path="getPatientInfo", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> getPatientInfo(PatientInfoDTO patientInfoDTO) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    ArrayList<GetPatientInfoVO> patientInfo = new ArrayList<GetPatientInfoVO>();

    if (patientInfoDTO == null) {
      map.put("code", "01");
      map.put("data", patientInfo);
      result.add(map);
      return result;
    }

    String opRsrvNo = patientInfoDTO.getOpRsrvNo() == null ? "" : patientInfoDTO.getOpRsrvNo();
    patientInfoDTO.setOpRsrvNo(opRsrvNo);

    patientInfo = hospitalService.getPatientInfo(patientInfoDTO);
    
    if (patientInfo.size() == 0) {
      map.put("code", "02");
    } else {
      map.put("code", "00");
    }

    map.put("data", patientInfo);
    result.add(map);
    return result;
  }

  /**
   * 사용자 즐겨찾기 추가
   * 리턴이 0이면 실패 1이면 성공
   * @param userId 사용자 아이디
   * @param formCd 서식 코드
   * @param instCd 기관 코드
   */
  @RequestMapping(path="setUserFormSetList", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> setUserFormSetList(String userId, String formCd, String instCd) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("userId", userId);
    params.put("formCd", formCd);
    params.put("instCd", instCd);

    logger.debug("params -- > " + params);
    int rCount = hospitalService.setUserFormSet(params);
    logger.debug("result -- > " + result);

    if (rCount == 0) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", rCount);
    }

    result.add(map);

    return result;
  }

  /**
   * 즐겨찾기 삭제
   * 리턴이 0이면 실패 1이면 성공
   * @param idx
   * @param userId
   * @param formCd
   * @param instCd
   * @return
   */
  @RequestMapping(path="delUserFormSet", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> delUserFormSet(String idx, String userId, String formCd, String instCd) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    HashMap<Object, Object> params = new HashMap<Object, Object>();

    logger.debug("idx -- > " + idx);
    logger.debug("idx getClass-- > " + idx.getClass());

    boolean intChk = CommonUtils.isNumeric(idx);

    if (intChk != true) {
      map.put("code", "01");
      result.add(map);
      return result;
    }
    
    params.put("idx", Integer.parseInt(idx));
    params.put("userId", userId);
    params.put("formCd", formCd);
    params.put("instCd", instCd);

    int rCount = hospitalService.delUserFormSet(params);

    if (rCount == 0) {
      map.put("code", "02");
    } else {
      map.put("code", "00");
      map.put("data", rCount);
    }

    result.add(map);

    return result;
  }

  @RequestMapping(path="getConsentFormCategory", method=RequestMethod.POST)
  public ArrayList<HashMap<String, Object>> getConsentFormCategory() {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    ArrayList<ConsentFormCategoryVO> categoryList = new ArrayList<ConsentFormCategoryVO>();

    categoryList = hospitalService.getConsentFormCategory();

    if (categoryList.size() == 0) {
      map.put("code", "01");
    } else {
      map.put("code", "00");
      map.put("data", categoryList);
    }

    result.add(map);

    return result;
  }
}