package com.lemon.lifecenter.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lemon.lifecenter.common.LifeCenterController;
import com.lemon.lifecenter.common.LifeCenterFunction;
import com.lemon.lifecenter.common.LifeCenterSessionController;
import com.lemon.lifecenter.dto.LoginDTO;
import com.lemon.lifecenter.dto.PatientDTO;
import com.lemon.lifecenter.dto.PatientMemoDTO;
import com.lemon.lifecenter.dto.PatientPHRHistoryDTO;
import com.lemon.lifecenter.dto.PatientPHRLatestDTO;
import com.lemon.lifecenter.dto.PatientSymptomSimDTO;
import com.lemon.lifecenter.service.ClinicService;
import com.lemon.lifecenter.service.PHRService;
import com.lemon.lifecenter.service.PatientService;

@Controller
@RequestMapping("/clinic")
public class ClinicController extends LifeCenterController {

	@Autowired
	private ClinicService clinicService;

	@Autowired
	private PHRService phrService;

	@Autowired
	private PatientService patientService;

	final int pageSize = 30;
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@RequestMapping("/state")
	public ModelAndView clinicState(HttpServletRequest request,
			@RequestParam(value = "page", required = true, defaultValue = "1") int page,
			@RequestParam(value = "searchText", required = false, defaultValue = "") String searchText) {


		String centerCode = LifeCenterSessionController.getSession( request, "sesCenterCode" );

		PatientPHRLatestDTO dto = new PatientPHRLatestDTO();
		dto.setLimit((Integer.valueOf(page) - 1) * pageSize);
		dto.setLimitMax(pageSize);
		dto.setCenterCode(centerCode);
		dto.setSearchText(searchText);

		int total = clinicService.selectPHRLatestCount(dto);
		List<PatientPHRLatestDTO> result = new ArrayList<PatientPHRLatestDTO>();

		if (total > 0) {
			result = clinicService.selectPHRLatestList(dto);
		}

		ModelAndView mv = setMV("clinic/state");

		mv.addObject("searchText", searchText);
		mv.addObject("total", total);
		mv.addObject("items", result);

		return mv;
	}

	@RequestMapping("/info")
	public ModelAndView patientInfo(
			@RequestParam(value = "patientIdx", required = true, defaultValue = "") int patientIdx,
			@RequestParam(value = "phrType", required = true, defaultValue = "temperature") String phrType) {

		// 환자정보
		PatientDTO patientDto = new PatientDTO();
		patientDto.setPatientIdx(patientIdx);
		patientDto = patientService.selectPatientOne(patientDto);

		String jumin = patientDto.getJumin();
		jumin = jumin.substring(0, 4) + "년 " + jumin.substring(4, 6) + "월 " + jumin.substring(6, 8) + "일";
		patientDto.setJumin(jumin);
		
		patientDto.setPatientPhone(LifeCenterFunction.phone(patientDto.getPatientPhone()));
		patientDto.setGuardianPhone(LifeCenterFunction.phone(patientDto.getGuardianPhone()));
		
		String bloodPress = patientDto.getBloodPressureLevel();
		
		if (!bloodPress.equals("")) {
				String[] bloodPressureLevel = patientDto.getBloodPressureLevel().split("[|]");
				logger.error("bloodPress -- > " + bloodPressureLevel[0]);
				logger.error("bloodPress -- > " + bloodPressureLevel[1]);
		}

		// phr 정보
		PatientPHRHistoryDTO dto = new PatientPHRHistoryDTO();
		dto.setPhrType(phrType);
		dto.setPatientIdx(patientIdx);

		int total = phrService.selectPHRHistoryCount(dto);
		List<PatientPHRHistoryDTO> result = new ArrayList<PatientPHRHistoryDTO>();

		if (total > 0) {
			result = phrService.selectPHRHistoryList(dto);
		}

		ModelAndView mv = setMV("clinic/info");

		mv.addObject("patientIdx", patientIdx);
		mv.addObject("info", patientDto);
		mv.addObject("phrType", phrType);
		mv.addObject("phrTotal", total);
		mv.addObject("phrItems", result);

		return mv;
	}

	@RequestMapping("/api/state")
	public @ResponseBody List<PatientPHRLatestDTO> state(HttpServletRequest request,
			@RequestParam(value = "page", required = true, defaultValue = "1") int page,
			@RequestParam(value = "searchText", required = false, defaultValue = "") String searchText) {

		String centerCode = LifeCenterSessionController.getSession( request, "sesCenterCode" );

		PatientPHRLatestDTO dto = new PatientPHRLatestDTO();
		dto.setLimit((Integer.valueOf(page) - 1) * pageSize);
		dto.setLimitMax(pageSize);
		dto.setCenterCode(centerCode);
		dto.setSearchText(searchText);

		int total = clinicService.selectPHRLatestCount(dto);
		List<PatientPHRLatestDTO> result = new ArrayList<PatientPHRLatestDTO>();

		if (total > 0) {
			result = clinicService.selectPHRLatestList(dto);
		}

		return result;
	}

	@RequestMapping("/api/phrDatas")
	public @ResponseBody List<PatientPHRHistoryDTO> phrDatas(
			@RequestParam(value = "patientIdx", required = true, defaultValue = "") int patientIdx,
			@RequestParam(value = "phrType", required = true, defaultValue = "temperature") String phrType) {

		PatientPHRHistoryDTO dto = new PatientPHRHistoryDTO();
		dto.setPatientIdx(patientIdx);
		dto.setPhrType(phrType);

		return phrService.selectPHRHistoryList(dto);

	}

	@RequestMapping(value = "/api/phrData", method = RequestMethod.POST)
	public @ResponseBody String insertPhrData(@ModelAttribute("dto") final PatientPHRHistoryDTO dto) {

		try {
			phrService.insertPHR(dto);

			JSONObject json = new JSONObject();

			json.put("code", "00");
			json.put("message", "");

			return json.toString();
		} catch (Exception e) {
			JSONObject json = new JSONObject();

			json.put("code", "01");
			json.put("message", e.getLocalizedMessage());

			return json.toString();
		}
	}

	@RequestMapping("/api/symptomDatas")
	public @ResponseBody List<PatientSymptomSimDTO> symptomDatas(
			@RequestParam(value = "patientIdx", required = true, defaultValue = "") int patientIdx) {

		PatientSymptomSimDTO dto = new PatientSymptomSimDTO();
		dto.setPatientIdx(patientIdx);

		return phrService.selectSymptomList(dto);

	}

	@RequestMapping(value = "/api/symptomData", method = RequestMethod.POST)
	public @ResponseBody String symptomData(@ModelAttribute("dto") final PatientSymptomSimDTO dto) {

		try {
			phrService.insertSymptom(dto);

			JSONObject json = new JSONObject();

			json.put("code", "00");
			json.put("message", "");

			return json.toString();
		} catch (Exception e) {
			JSONObject json = new JSONObject();

			json.put("code", "01");
			json.put("message", e.getLocalizedMessage());

			return json.toString();
		}
	}

	@RequestMapping("/api/memoDatas")
	public @ResponseBody List<PatientMemoDTO> memoDatas(
			@RequestParam(value = "patientIdx", required = true, defaultValue = "") int patientIdx) {

		PatientMemoDTO dto = new PatientMemoDTO();
		dto.setPatientIdx(patientIdx);

		return clinicService.selectMemoList(dto);
	}

	@RequestMapping(value = "/api/memoData", method = RequestMethod.POST)
	public @ResponseBody String memoData(@ModelAttribute("dto") final PatientMemoDTO dto) {

		try {
			clinicService.insertMemo(dto);

			JSONObject json = new JSONObject();

			json.put("code", "00");
			json.put("message", "");

			return json.toString();
		} catch (Exception e) {
			JSONObject json = new JSONObject();

			json.put("code", "01");
			json.put("message", e.getLocalizedMessage());

			return json.toString();
		}
	}
}
