package com.dbs.mcare.web.controller;

import javax.validation.Valid;
import javax.xml.namespace.QName;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dbs.mcare.web.request.consent.GetCategoryForDropdown;
import com.dbs.mcare.web.request.consent.GetConsentBySearch;
import com.dbs.mcare.web.request.consent.GetConsentImage;
import com.dbs.mcare.web.request.consent.GetConsentList;
import com.dbs.mcare.web.request.consent.SaveComplete;
import com.dbs.mcare.web.request.consent.SaveDelete;
import com.dbs.mcare.web.request.consent.SaveImageSignInfo;
import com.dbs.mcare.web.request.consent.SaveImageUploadInfo;
import com.dbs.mcare.web.request.consent.SaveMedicalHistory;
import com.dbs.mcare.web.request.consent.SavePrintOut;
import com.dbs.mcare.web.request.consent.SaveTempData;
import com.dbs.mplus.fatimadeagu.inf.ArrayOfCategoryForDropdownVO;
import com.dbs.mplus.fatimadeagu.inf.ArrayOfConsentBySearchVO;
import com.dbs.mplus.fatimadeagu.inf.ArrayOfConsentImageVO;
import com.dbs.mplus.fatimadeagu.inf.ArrayOfConsentVO;
import com.dbs.mplus.fatimadeagu.inf.CheckConsentState;
import com.dbs.mplus.fatimadeagu.inf.ConsentSvc;
import com.dbs.mplus.fatimadeagu.inf.ConsentSvcSoap;

/**
 * 
 * @author sangmin
 *
 */
@RestController
@RequestMapping("/consent")
public class ConsentController {

	private final QName serviceName = new QName("http://tempuri.org/", "ConsentSvc");

	private final ConsentSvc consentService = new ConsentSvc(ConsentSvc.WSDL_LOCATION, serviceName);
	
	/**
	 * GetCategoryForDropdown 	동의서 하위 카테고리 조회 
	 * @param p
	 * @return
	 */
	@RequestMapping("/getCategoryForDropdown")
	public ArrayOfCategoryForDropdownVO categoryForDropdown(@RequestBody @Valid GetCategoryForDropdown p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		ArrayOfCategoryForDropdownVO categoryList = port.getCategoryForDropdown(p.getCategoryId());
		return categoryList;
	}
	
	/**
	 * GetConsentList	환자/상태에 따른 동의서 목록 검색
	 * @param 
	 * @return
	 */
	@RequestMapping("/getConsentList")
	public ArrayOfConsentVO consentList(@RequestBody @Valid GetConsentList p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		ArrayOfConsentVO consentList = port.getConsentList(p.getPatientCode(), p.getStartDt(), p.getEndDt(), p.getConsentState(), p.getJobKindCd());
		return consentList;
	}
	
	/**
	 * GetConsentBySearch 	동의서 찾기(조회)
	 * @param 
	 * @return
	 */
	@RequestMapping("/getConsentBySearch")
	public ArrayOfConsentBySearchVO getConsentBySearch(@RequestBody @Valid GetConsentBySearch p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		ArrayOfConsentBySearchVO consentList = port.getConsentBySearch(p.getCategoryId(), p.getKeyWord());
		return consentList;
	}
	
	/**
	 * GetConsentImage 	동의서 저장된 이미지 내역 조회
	 * @param consent
	 * @return
	 */
	@RequestMapping("/getConsentImage")
	public ArrayOfConsentImageVO getConsentImage(@RequestBody @Valid GetConsentImage p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		ArrayOfConsentImageVO consentImage = port.getConsentImage(p.getConsentMstRid());
		return consentImage;
	}
	
	/**
	 * SaveDelete 	동의서 삭제/폐기 데이터를 저장 
	 * @return
	 */
	@RequestMapping("/saveDelete")
	public String saveDelete(@RequestBody @Valid SaveDelete p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		Integer returnValue = port.saveDelete(p.getUserId(), p.getConsentMstRidInt(), p.getPatientCode(), p.getClnDeptCode(), p.getWard(), p.getRoomcd(), p.getFormRid(), p.getRewriteConsentMstRid(), p.getReissueConsentMstRid(), p.getConsentState(), p.getDeviceIdentNo(), p.getReasonForUseN());
		return returnValue.toString();
	}

	/**
	 * SaveImageUploadInfo 	동의서 서명 완료일 경우 이미지 데이터 저장
	 * @return
	 */
	@RequestMapping("/saveImageUploadInfo")
	public Integer saveImageUploadInfo(@RequestBody @Valid SaveImageUploadInfo p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		Integer returnValue = port.saveImageUploadInfo(p.getConsentState(), p.getImageFileName(), p.getConsentMstRid(), p.getUserId(), p.getFormRid());
		return returnValue;
	}

	/**
	 * SaveImageSignInfo 	서명완료된 이미지의 hash 정보 저장
	 * @return
	 */
//	@RequestMapping("/saveImageSignInfo")
/*	public Object saveImageSignInfo(@RequestBody @Valid SaveImageSignInfo p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		port.saveImageSignInfo(p.getConsentState(), p.getConsentMstRid(), p.getConsentImageRid(), p.getUserId(), p.getCertTarget());
		return null;
	}*/

	/**
	 * SaveMedicalHistory 	동의서의 기왕력 데이터 저장
	 * @return
	 */
	@RequestMapping("/saveMedicalHistory")
	public String saveMedicalHistory(@RequestBody @Valid SaveMedicalHistory p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		String returnValue = port.saveMedicalHistory(p.getPatientCode(), p.getClnDate(), p.getBp(), p.getDm(), p.getHeart(), p.getKidney(), p.getRespiration(), p.getHx(), p.getAllergy(), p.getDrug(), p.getSmoking(), p.getIdio(), p.getNacrotics(), p.getAirway(), p.getHemorrhage(), p.getStatusEtc(), p.getUserId());
		return returnValue;
	}
	
	/**
	 * CheckConsentState 	동의서 상태 체크
	 * @return
	 */
	@RequestMapping("/checkConsentState")
	public String checkConsentState(@RequestBody @Valid CheckConsentState p) {
		ConsentSvcSoap port = consentService.getConsentSvcSoap12();
		int returnValue = port.checkConsentState(p.getConsentmstrid(), p.getConsentState());
		return String.valueOf(returnValue);
	}
}