package com.bankaudit.rest.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.RestController;

import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.dto.SequenceDto;
import com.bankaudit.model.MaintReportSequenceProcess;
import com.bankaudit.service.MaintReportSequenceProcessService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/maintReportSequenceProcess")
public class MaintReportSequenceProcessController {

	@Autowired
	MaintReportSequenceProcessService maintReportSequenceProcessService;

	
@PostMapping(path = "/chapter", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus createChapter(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.saveChapter(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage(e.getMessage());
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	@PutMapping(path = "/chapter", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus updateChapter(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.updateChapter(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	
@PostMapping(path = "/section", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus createSection(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.saveSection(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	@PutMapping(path = "/section", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus updateSection(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.updateSection(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	
@PostMapping(path = "/product", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus createProduct(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.saveProduct(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	@PutMapping(path = "/product", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus updateProduct(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.updateProduct(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	
@PostMapping(path = "/process", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus createProcess(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.saveProcess(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	@PutMapping(path = "/process", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus updateProcess(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.updateProcess(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	
@PostMapping(path = "/checklist", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus createCheckList(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.saveCheckList(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	@PutMapping(path = "/checklist", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus updateCheckList(@RequestBody List<MaintReportSequenceProcess> maintReportSequenceProcesslist) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (maintReportSequenceProcesslist != null && !maintReportSequenceProcesslist.isEmpty()) {
			try {
				maintReportSequenceProcessService.updateCheckList(maintReportSequenceProcesslist);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				if (e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		return serviceStatus;
	}

	
@RequestMapping(value = "/initiateSequence", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus initiateMaintReportSequenceProcessValues(
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "auditTypeCode") String auditTypeCode, 
			@RequestParam(required = true, value = "maker") String maker
			) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (!BankAuditUtil.isEmptyString(auditTypeCode) && legalEntityCode > 0) {
			try {
				 maintReportSequenceProcessService.initiateMaintReportSequenceProcess(legalEntityCode,auditTypeCode,maker);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved ");
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
	
@RequestMapping(value = "/getDynamicValues", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus getMaintReportSequenceProcessValues(
			/* @PathVariable("createOrUpdate")String createOrUpdate, */
			@RequestParam(required = true, value = "legalEntityCode") String legalEntityCode,
			@RequestParam(required = true, value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required = false, value = "auditGroupCode") String auditGroupCode,
			@RequestParam(required = false, value = "auditSubGroupCode") String auditSubGroupCode,
			@RequestParam(required = false, value = "activityId") String activityId,
			@RequestParam(required = false, value = "processId") String processId,
			@RequestParam(required = false, value = "findingId") String findingId, @RequestParam(required = false, value = "status") String status, @RequestParam(required = false, value = "maker") String maker) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (!BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(legalEntityCode)) {
			try {
				List<SequenceDto> values = maintReportSequenceProcessService.getMaintReportSequenceProcessMappingValues(legalEntityCode,
						auditTypeCode, auditGroupCode, auditSubGroupCode, activityId, processId, findingId, status,maker);
				
				if (values != null && !values.isEmpty()) {
					serviceStatus.setResult(values);
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved ");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage(" No records found ");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}

	
@GetMapping(value = "/getAll", produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getMaintReportSequenceProcessList(@RequestParam("draw") String drawStr, @RequestParam("length") String length,
			@RequestParam("start") String start, @RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") String orderColumnStr, @RequestParam("order[0][dir]") String orderDirection,
			@RequestParam("legalEntityCode") String legalEntityCodeStr, @RequestParam("auditTypeCode") String auditTypeCode) {

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length) && !BankAuditUtil.isEmptyString(start)
				&& !BankAuditUtil.isEmptyString(legalEntityCodeStr)) {
			try {
				Integer legalEntityCode = Integer.parseInt(legalEntityCodeStr);
				Integer draw = Integer.parseInt(drawStr);
				Integer size = Integer.parseInt(length);
				Integer page = Integer.parseInt(start) / size;
				Integer orderColumn = null;
				if (!BankAuditUtil.isEmptyString(orderColumnStr)) {
					orderColumn = Integer.parseInt(orderColumnStr);
				}
				dataTableResponse = maintReportSequenceProcessService.getMaintReportSequenceProcess(legalEntityCode, search, orderColumn,
						orderDirection, page, size, auditTypeCode);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				e.printStackTrace();
			}

		} else {
			dataTableResponse = new DataTableResponse();
			dataTableResponse.setError("invalid data");
		}

		return dataTableResponse;
	}

	@DeleteMapping(value = "/deleteMaintReportSequenceProcess", produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus deleteActivityProcessFindingMappingValues(
			/* @PathVariable("createOrUpdate")String createOrUpdate, */
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required = false, value = "auditGroupCode") String auditGroupCode,
			@RequestParam(required = false, value = "status") String status) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null && !BankAuditUtil.isEmptyString(auditTypeCode)) {
			try {
				maintReportSequenceProcessService.deleteRejectedMaintReportSequenceProcessByAuditGroupCode(legalEntityCode, auditTypeCode,
						auditGroupCode);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully retrieved ");

			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}

	/* 
@RequestMapping(value = "/getDynamicValuesList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus getMaintReportSequenceProcessValuesList(
			/* @PathVariable("createOrUpdate")String createOrUpdate, * /
			@RequestParam(required = true, value = "legalEntityCode") String legalEntityCode,
			@RequestParam(required = true, value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required = false, value = "auditGroupCode") String auditGroupCode,
			@RequestParam(required = false, value = "auditSubGroupCode") String auditSubGroupCode,
			@RequestParam(required = false, value = "activityId") String activityId,
			@RequestParam(required = false, value = "processId") String processId,
			@RequestParam(required = false, value = "findingId") String findingId, @RequestParam(required = false, value = "status") String status) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (!BankAuditUtil.isEmptyString(legalEntityCode)) {
			try {
				List<String> values = maintReportSequenceProcessService.getActivityProcessFindingMappingValues(legalEntityCode, auditTypeCode,
						auditGroupCode, auditSubGroupCode, activityId, processId, findingId, "C");
				if (values != null && !values.isEmpty()) {
					serviceStatus.setResult(values.toString());
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successfully retrieved ");
				} else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage(" No records found ");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	} */

	
@GetMapping(value = "/draftMaintReportSequenceProcess", produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus draftMaintReportSequenceProcess(
			/* @PathVariable("createOrUpdate")String createOrUpdate, */
			@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode,
			@RequestParam(required = true, value = "auditTypeCode") String auditTypeCode,
			@RequestParam(required = true, value = "status") String status) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (legalEntityCode != null && !BankAuditUtil.isEmptyString(auditTypeCode) && !BankAuditUtil.isEmptyString(status)) {
			try {
				boolean result = maintReportSequenceProcessService.getDraftMaintReportSequenceProcess(legalEntityCode, auditTypeCode, status);
				serviceStatus.setResult(result);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully retrieved ");

			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	

	
@PostMapping(value = "/authoriseMaintReportSequenceProcess", produces = MediaType.APPLICATION_JSON_VALUE)
	public ServiceStatus authoriseMaintReportSequenceProcess(@RequestBody MaintReportSequenceProcess mrsp) {
		ServiceStatus serviceStatus = new ServiceStatus();
		if (mrsp.getLegalEntityCode() != null && !BankAuditUtil.isEmptyString(mrsp.getAuditTypeCode()) && !BankAuditUtil.isEmptyString(mrsp.getChecker())) {
			try {
				maintReportSequenceProcessService.authoriseMaintReportSequenceProcess(mrsp.getLegalEntityCode(), mrsp.getAuditTypeCode(), mrsp.getChecker(), mrsp.getAuthRejRemarks());
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully retrieved ");
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		} else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		return serviceStatus;
	}
	
	
}
