package com.dbs.mplus.controller.mobile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.dbs.mplus.exception.mobile.ApiCallException;
import com.dbs.mplus.exception.mobile.MobileControllerException;
import com.dbs.mplus.framework.exception.MPlusServiceException;
import com.dbs.mplus.framework.service.menu.ServiceMenu;
import com.dbs.mplus.framework.template.AbstractController;
import com.dbs.mplus.framework.util.ResponseUtil;
import com.dbs.mplus.service.MPlusConfigureService;
import com.dbs.mplus.service.api.MPlusApi;
import com.dbs.mplus.service.api.MPlusApiCallService;

/**
 * 동의서
 * 
 * @author siyoung.ahn
 *
 */
@Controller
@RequestMapping("/mobile/consent")
@ServiceMenu(menuId="consent", description="동의서")
public class ConsentController extends AbstractController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private MPlusApiCallService apiCallService;
	@Autowired 
	private MPlusConfigureService configureService; 

	/**
	 * 동의서 리스트 조회
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/getConsentList.json", method = RequestMethod.POST)
	public Map<String, Object> getConsentList(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			return ResponseUtil.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_GETCONSENTLIST, reqMap));
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}

	/**
	 * 주치의별 동의서 리스트 조회
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/getUnfinishedListPerDoctor.json", method = RequestMethod.POST)
	public Map<String, Object> getUnfinishedListPerDoctor(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			return ResponseUtil.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_GETUNFINISHEDLISTPERDOCTOR, reqMap));
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}

	/**
	 * 동의서 찾기
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/getConsentBySearch.json", method = RequestMethod.POST)
	public Map<String, Object> getConsentBySearch(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			return ResponseUtil.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_GETCONSENTBYSEARCH, reqMap));
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}

	/**
	 * 동의서 카테고리 조회
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/getCategoryForDropdown.json", method = RequestMethod.POST)
	public Map<String, Object> getCategoryForDropdown(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			return ResponseUtil
					.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_GETCATEGORYFORDROPDOWN, reqMap));
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}

	/**
	 * OCR Tag 획득하기
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/getOcrTag.json", method = RequestMethod.POST)
	public Map<String, Object> getOcrTag(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			return ResponseUtil.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_GETOCRTAG, reqMap));
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}
	
	/**
	 * 동의서 상태 체크
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/checkConsentState.json", method = RequestMethod.POST)
	public Map<String, Object> checkConsentState(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			return ResponseUtil.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_CHECKCONSENTSTATE, reqMap));
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}
	
	/**
	 * 동의서 이미지 정보 조회
	 * 
	 * @param request
	 * @param response
	 * @param reqMap
	 */
	@RequestMapping(value = "/getConsentImage.json", method = RequestMethod.POST)
	public Map<String, Object> getConsentImage(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, Object> reqMap) throws MobileControllerException {
		// 접근권한 및 파라미터 정리
		try {
			reqMap = super.isAccessableMenu(this.getClass(), request, reqMap);
		} catch (MPlusServiceException ex) {
			throw new MobileControllerException(ex.getCode(), ex.getMessage());
		}

		// 실행
		try {
			// return ResponseUtil.wrapResultMap(this.apiCallService.execute(MPlusApi.CONSENT_GETCONSENTIMAGE, reqMap));
			List<Map<String, Object>> result = (List<Map<String, Object>>) this.apiCallService.execute(MPlusApi.CONSENT_GETCONSENTIMAGE, reqMap);
			List<Map<String, Object>> imageList = new ArrayList<Map<String, Object>>();
			String consentImagePath = configureService.getConsentImagePath();
			
			if( result != null ) {
				for( int i = 0; i < result.size(); i++ ) {
					StringBuilder sb = new StringBuilder();
					sb.append(consentImagePath);
					String imagePath = (String) result.get(i).get("imagePath");
					imagePath = imagePath.replaceAll("\\\\", "");
					sb.append(imagePath);
					sb.append(result.get(i).get("imageFilename"));
					
					Map<String, Object> imageMap = new HashMap<String, Object>();
					imageMap.put("imagePath", sb.toString());
					imageMap.put("consentImageRid", result.get(i).get("consentImageRid"));
					imageList.add(imageMap);
				}
			}
			
			return ResponseUtil.wrapResultMap(imageList);
			
		} catch (ApiCallException ex) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("예외발생", ex);
			}

			throw new MobileControllerException("mplus.error.if.system", "시스템 호출 오류가 발생했습니다 (IF)");
		}
	}
}