package com.jasamedika.medifirst2000.controller;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.controller.base.IBaseRestController;
import com.jasamedika.medifirst2000.controller.base.LocaleController;
import com.jasamedika.medifirst2000.dao.LoginUserDao;
import com.jasamedika.medifirst2000.dao.RuanganDao;
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.MapPegawaiToModulAplikasiService;
import com.jasamedika.medifirst2000.service.ModulAplikasiService;
import com.jasamedika.medifirst2000.service.ObjekModulAplikasiService;
import com.jasamedika.medifirst2000.service.PegawaiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import com.jasamedika.medifirst2000.vo.MapLoginUserToRuanganVO;
import com.jasamedika.medifirst2000.vo.MapPegawaiToModulAplikasiVO;
import com.jasamedika.medifirst2000.vo.ModulAplikasiSVO;
import com.jasamedika.medifirst2000.vo.ModulAplikasiVO;
import com.jasamedika.medifirst2000.vo.ObjekModulAplikasiVO;
import com.jasamedika.medifirst2000.vo.PegawaiVO;

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

@RestController
@RequestMapping({"/test-tanpa-auth/modul-aplikasi", "/modul-aplikasi"})
public class MenuDinamicController extends LocaleController<ModulAplikasiSVO> implements IBaseRestController<ModulAplikasiSVO> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MenuDinamicController.class);
	
	@Autowired
	ModulAplikasiService<ModulAplikasiVO> modulAplikasiService;
	
	@Autowired
	ObjekModulAplikasiService<ObjekModulAplikasiVO> objekModulAplikasiService;
	
	@Autowired
	MapLoginUserToRuanganService<MapLoginUserToRuanganVO> mapLoginUserToRuanganService;
	
	@Autowired
	LoggingSystemAsynchronous loggingSystemAsynchronous;
	
	@Autowired
	MapPegawaiToModulAplikasiService<MapPegawaiToModulAplikasiVO> mapPegawaiToModulAplikasiService;
	
	@Autowired
	LoginUserService loginUserService;
	
	@Autowired
	PegawaiService pegawaiService;
	
	@Autowired
	private LoginUserDao loginUserDao;
	
	@Autowired 
	RuanganDao ruanganDao;
	
	@RequestMapping(value="/keluar-ruangan/{modulaplikasiId:[0-9]+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<String> keluarRuangan(@PathVariable("modulaplikasiId") int modulaplikasiId, HttpServletRequest request){
		try{
			LoginUser loginUser = loginUserService.getLoginUser();
			Integer idPegawai = 0;
			if (CommonUtil.isNotNullOrEmpty(loginUser.getPegawai())){
				idPegawai = loginUser.getPegawai().getId();
			}
			loggingSystemAsynchronous.saveSignInLog(modulaplikasiId, 0, loginUser.getNamaUser(), idPegawai);
			Pegawai pegawai = loginUser.getPegawai();
			return RestUtil.defaultJsonResponse(pegawai);
		}catch(Exception e){
			LOGGER.info("Got exception {} in get objek modul aplikasi", e);
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, "Gagal pilih ruangan.");
			return RestUtil.getJsonHttptatus(HttpStatus.UNAUTHORIZED, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/pilih-ruangan/{modulaplikasiId:[0-9]+}/{ruanganId:[0-9]+}", method = RequestMethod.GET, produces = MediaType.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 (CommonUtil.isNotNullOrEmpty(loginUser.getPegawai())){
				idPegawai = loginUser.getPegawai().getId();
			}
			Pegawai pegawai = loginUser.getPegawai(); //loginUserService.updateRuanganPegawaiPerLoginPerpindahRuangan(ruanganId);
			loggingSystemAsynchronous.saveSignInLog(modulaplikasiId, ruanganId, loginUser.getNamaUser(), idPegawai);
			return RestUtil.defaultJsonResponse(pegawai);
		}catch(Exception e){
			LOGGER.info("Got exception {} in get objek modul aplikasi", e);
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, "Gagal pilih ruangan.");
			return RestUtil.getJsonHttptatus(HttpStatus.UNAUTHORIZED, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/get-all-sub-system", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ModulAplikasiVO>> getAllSubSystem(){					
		return RestUtil.defaultJsonResponse(modulAplikasiService.findAllSubSytemOrderByModulNoUrutAsc(), mapHeaderMessage) ;
	}
		
	@RequestMapping(value = "/get-all-sub-modul/{kdModulAplikasi:[0-9]+}/show", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ModulAplikasiVO>> getAllSubModul(@PathVariable("kdModulAplikasi") int kdModulAplikasi, HttpServletRequest request){	
		//loggingSystemAsynchronous.saveSignInLog(kdModulAplikasi, request, null);
		return RestUtil.defaultJsonResponse(modulAplikasiService.findAllSubModulOrderByModulNoUrutAsc(kdModulAplikasi));
	}
	
	/// OBJEK MODUL = FORM INPUT
	@RequestMapping(value = "/get-all-objek-modul/{idUserLogin:[0-9]+}/{kdModulAplikasi:[0-9]+}/show", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ObjekModulAplikasiVO>> getAllObjekModul(@PathVariable("idUserLogin") int idUserLogin, @PathVariable("kdModulAplikasi") int kdModulAplikasi){		
		try{
			//LoginUserVO loginUserVo = loginUserService.findById(idUserLogin);
			LoginUser loginUser = loginUserDao.findOne(idUserLogin);
			
			Pegawai pegawai = loginUser.getPegawai();
			Integer pegawaiId = pegawai.getId();
			
			List<MapPegawaiToModulAplikasiVO> lVO = mapPegawaiToModulAplikasiService.findByPegawaiIdAndModulAplikasiId(pegawaiId, kdModulAplikasi);
			
			if (CommonUtil.isNullOrEmpty(lVO)) {
				addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, "Anda tidak punya izin akses");
				return RestUtil.getJsonHttptatus(HttpStatus.UNAUTHORIZED, mapHeaderMessage);				
			}
			
			return RestUtil.defaultJsonResponse(objekModulAplikasiService.findAllModulAplikasi(kdModulAplikasi));
			
		}catch(Exception e){
			LOGGER.info("Got exception {} in get objek modul aplikasi", e);
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, "Anda tidak punya izin akses");
			return RestUtil.getJsonHttptatus(HttpStatus.UNAUTHORIZED, mapHeaderMessage);
		}
	}
	
	@RequestMapping(value = "/get-all-default-objek-modul", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<List<ObjekModulAplikasiVO>> getAllDefaultObjekModulAplikasi(){
		return RestUtil.defaultJsonResponse(objekModulAplikasiService.findByKdObjekModulAplikasiHeadIsNullOrderByNoUrutAsc());
	}
	
	@RequestMapping(value = "/get-all-unit-kerja/{idUserLogin:[0-9]+}/{kdModulAplikasi:[0-9]+}/show", method = RequestMethod.GET, produces = MediaType.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 (CommonUtil.isNullOrEmpty(ruangannya)){
				addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, "Anda tidak punya izin akses");
				return RestUtil.getJsonHttptatus(HttpStatus.UNAUTHORIZED, mapHeaderMessage);
			}
		
			return RestUtil.defaultJsonResponse(ruangannya);
		}catch(Exception e){
			LOGGER.info("Got exception {} in get objek modul aplikasi", e);
			addHeaderMessage(Constants.MessageInfo.ERROR_MESSAGE, "Anda tidak punya izin akses");
			return RestUtil.getJsonHttptatus(HttpStatus.UNAUTHORIZED, mapHeaderMessage);
		}
	}
	
	
	@Override	
	public ResponseEntity<Collection<ModulAplikasiSVO>> getAllVOWithQueryString(HttpServletRequest request, Integer page, Integer limit, String sort, String dir) {
		return null;
	}
	

	@Override
	public ResponseEntity<ModulAplikasiSVO> getVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<String> addVO(ModulAplikasiSVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> editVO(ModulAplikasiSVO vo) {
		return null;
	}

	@Override
	public ResponseEntity<String> deleteVO(Integer id) {
		return null;
	}

	@Override
	public ResponseEntity<List<ModulAplikasiSVO>> getAllVO() {
		return null;
	}



}
