package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.entities.LoginUser;
import com.jasamedika.medifirst2000.entities.Pegawai;
import com.jasamedika.medifirst2000.logging.hibernate.async.LoggingSystemAsynchronous;
import com.jasamedika.medifirst2000.service.LoginUserService;
import com.jasamedika.medifirst2000.service.MapLoginUserToRuanganService;
import com.jasamedika.medifirst2000.service.ModulAplikasiService;
import com.jasamedika.medifirst2000.service.ObjekModulAplikasiService;
import com.jasamedika.medifirst2000.vo.MapLoginUserToRuanganVO;
import com.jasamedika.medifirst2000.vo.ModulAplikasiSVO;
import com.jasamedika.medifirst2000.vo.ModulAplikasiVO;
import com.jasamedika.medifirst2000.vo.ObjekModulAplikasiVO;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.jasamedika.medifirst2000.constants.Constants.MessageInfo.ERROR_MESSAGE;
import static com.jasamedika.medifirst2000.util.CommonUtil.isNotNullOrEmpty;
import static com.jasamedika.medifirst2000.util.CommonUtil.isNullOrEmpty;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.defaultJsonResponse;
import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonHttpStatus;
import static org.slf4j.LoggerFactory.getLogger;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Controller class for menu modul
 * 
 * @author Syamsu
 */

@RestController
@RequestMapping("/modul-aplikasi")
public class MenuDinamicController extends LocaleController<ModulAplikasiSVO> {

	private static final Logger LOGGER = getLogger(MenuDinamicController.class);

	@Autowired
	ModulAplikasiService<ModulAplikasiVO> modulAplikasiService;

	@Autowired
	ObjekModulAplikasiService<ObjekModulAplikasiVO> objekModulAplikasiService;

	@Autowired
	MapLoginUserToRuanganService<MapLoginUserToRuanganVO> mapLoginUserToRuanganService;

	@Autowired
	LoggingSystemAsynchronous loggingSystemAsynchronous;

	@Autowired
	LoginUserService loginUserService;

	@RequestMapping(value = "/keluar-ruangan/{modulaplikasiId:[0-9]+}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> keluarRuangan(@PathVariable("modulaplikasiId") int modulaplikasiId,
			HttpServletRequest request) {
		try {
			LoginUser loginUser = loginUserService.getLoginUser();
			Integer idPegawai = 0;
			if (isNotNullOrEmpty(loginUser.getPegawai()))
				idPegawai = loginUser.getPegawai().getId();
			loggingSystemAsynchronous.saveSignInLog(modulaplikasiId, 0, loginUser.getNamaUser(), idPegawai);
			Pegawai pegawai = loginUser.getPegawai();
			return defaultJsonResponse(pegawai);
		} catch (Exception e) {
			LOGGER.info("Got exception {} in getLoginUser(keluarRuangan)", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, "Gagal pilih ruangan.");
			return getJsonHttpStatus(UNAUTHORIZED, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/pilih-ruangan/{modulaplikasiId:[0-9]+}/{ruanganId:[0-9]+}", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<String> pilihRuangan(@PathVariable("modulaplikasiId") int modulaplikasiId,
			@PathVariable("ruanganId") int ruanganId, HttpServletRequest request) {
		try {
			LoginUser loginUser = loginUserService.getLoginUser();
			Integer idPegawai = 0;
			if (isNotNullOrEmpty(loginUser.getPegawai()))
				idPegawai = loginUser.getPegawai().getId();
			Pegawai pegawai = loginUser.getPegawai();
			loggingSystemAsynchronous.saveSignInLog(modulaplikasiId, ruanganId, loginUser.getNamaUser(), idPegawai);
			return defaultJsonResponse(pegawai);
		} catch (Exception e) {
			LOGGER.info("Got exception {} in getLoginUser(pilihRuangan)", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, "Gagal pilih ruangan.");
			return getJsonHttpStatus(UNAUTHORIZED, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-sub-system", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ModulAplikasiVO>> getAllSubSystem() {
		return defaultJsonResponse(modulAplikasiService.findAllSubSytemOrderByModulNoUrutAsc(), mapHeaderMessage);
	}

	@RequestMapping(value = "/get-all-sub-modul/{kdModulAplikasi:[0-9]+}/show", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ModulAplikasiVO>> getAllSubModul(@PathVariable("kdModulAplikasi") int kdModulAplikasi,
			HttpServletRequest request) {
		return defaultJsonResponse(modulAplikasiService.findAllSubModulOrderByModulNoUrutAsc(kdModulAplikasi));
	}

	@RequestMapping(value = "/get-all-default-objek-modul", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ObjekModulAplikasiVO>> getAllDefaultObjekModulAplikasi() {
		return defaultJsonResponse(objekModulAplikasiService.findByKdObjekModulAplikasiHeadIsNullOrderByNoUrutAsc());
	}

	@RequestMapping(value = "/get-all-unit-kerja/{idUserLogin:[0-9]+}/{kdModulAplikasi:[0-9]+}/show", method = GET, produces = APPLICATION_JSON_VALUE)
	public ResponseEntity<List<Map<String, Object>>> getAllUnitKerjaByUserLogin(
			@PathVariable("idUserLogin") int idUserLogin, @PathVariable("kdModulAplikasi") int kdModulAplikasi) {
		try {
			List<Map<String, Object>> ruangannya = mapLoginUserToRuanganService
					.findCurrentRoomByUserLoginAndModulAplikasi(idUserLogin, kdModulAplikasi);
			if (isNullOrEmpty(ruangannya)) {
				addHeaderMessage(ERROR_MESSAGE, "Anda tidak punya izin akses");
				return getJsonHttpStatus(UNAUTHORIZED, mapHeaderMessage);
			}
			return defaultJsonResponse(ruangannya);
		} catch (Exception e) {
			LOGGER.info("Got exception {} in findCurrentRoomByUserLoginAndModulAplikasi", e.getMessage());
			addHeaderMessage(ERROR_MESSAGE, "Anda tidak punya izin akses");
			return getJsonHttpStatus(UNAUTHORIZED, mapHeaderMessage);
		}
	}

}
