package com.jasamedika.medifirst2000.controller;

import com.jasamedika.medifirst2000.constants.Constants;
import com.jasamedika.medifirst2000.dto.KomponenGajiDto;
import com.jasamedika.medifirst2000.dto.SlipGajiDto;
import com.jasamedika.medifirst2000.service.SlipGajiService;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.ResourceUtils;
import com.jasamedika.medifirst2000.util.rest.RestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/slip-gaji")
public class SlipGajiController {
	@Autowired
	private SlipGajiService slipGajiService;

	@RequestMapping(value = "/init", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> init() {
		try {
			slipGajiService.init();
			return new ResponseEntity<>(true, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> save(@RequestBody @Valid SlipGajiDto dto) {
		try {
			slipGajiService.save(dto);
			return new ResponseEntity<>(true, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/unggah", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> save(@RequestBody List<SlipGajiDto> dtoList) {
		try {
			slipGajiService.save(dtoList);
			return new ResponseEntity<>(true, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/detail/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> updateDetail(@RequestBody @Valid SlipGajiDto dto) {
		try {
			slipGajiService.updateDetail(dto);
			return new ResponseEntity<>(true, HttpStatus.CREATED);
		} catch (Exception e) {
			return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/get/{pegawaiId}/{bulan}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> get(@PathVariable Integer pegawaiId, @PathVariable Long bulan) {
		SlipGajiDto entity = slipGajiService.get(pegawaiId, bulan);
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}

	@RequestMapping(value = "/template/generate/{bulan}", method = RequestMethod.GET)
	public ResponseEntity<Object> generateTemplate(@PathVariable Long bulan, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String fileNamePath = slipGajiService.writeExcel(bulan);
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment; filename=" + fileNamePath);
			Map<String, Object> result = new HashMap<>();
			result.put("filename", fileNamePath);
			return new ResponseEntity<>(result, HttpStatus.OK);
		} catch (Exception e) {
			return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping(value = "/template/download", method = RequestMethod.GET)
	public ResponseEntity<Resource> downloadTemplate(@RequestParam(value = "filename") String fileName,
			HttpServletRequest request) {
		Resource resource;
		Map<String, String> mapHeaderMessage = new HashMap<>();
		if (CommonUtil.isNotNullOrEmpty(fileName)) {
			try {
				resource = ResourceUtils.loadFile(fileName);
			} catch (Exception e) {
				mapHeaderMessage.put(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
				return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
			}
			String contentType;
			try {
				contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
			} catch (Exception e) {
				mapHeaderMessage.put(Constants.MessageInfo.ERROR_MESSAGE, e.getMessage());
				return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
			}
			if (CommonUtil.isNullOrEmpty(contentType))
				contentType = "application/octet-stream";
			return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
					.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + resource.getFilename())
					.body(resource);
		} else {
			return RestUtil.getJsonHttptatus(HttpStatus.INTERNAL_SERVER_ERROR, mapHeaderMessage);
		}
	}

	@RequestMapping(value = "/komponen/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Object> get() {
		List<KomponenGajiDto> entity = slipGajiService.listKomponen();
		return new ResponseEntity<>(entity, HttpStatus.OK);
	}
}
