/*
 * 
 */
package com.bankaudit.rest.controller;

import java.util.List;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.constants.BankAuditConstant;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.dto.MaintActionItemDto;
import com.bankaudit.model.MaintActionItemWrk;
import com.bankaudit.service.MaintActionItemsService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;

 

@RestController

@RequestMapping("/api/maintActionItem")
public class MaintActionItemController {
	
	static final Logger logger = Logger.getLogger(MaintActionItemController.class);
	
	@Autowired
	MaintActionItemsService maintActionItemsService;
	
	
@RequestMapping(value = "/getAll", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getAll(
			@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")Integer legalEntityCode ){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				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 = maintActionItemsService.getAllActionItemList(legalEntityCode, search, orderColumn, orderDirection, page, size);
				System.out.println("Inside controller dataTableResponse .... "+dataTableResponse);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				logger.error(e.getStackTrace()); 
			}

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

		return dataTableResponse;
	}
	
	
@RequestMapping(value = "/getActionItemByAuditTypeCode", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	DataTableResponse getActionItemByAuditTypeCode(
			@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")Integer legalEntityCode,
			@RequestParam("auditTypeCode") String auditTypeCode, 
			@RequestParam("status") String status){

		DataTableResponse dataTableResponse = null;

		if (!BankAuditUtil.isEmptyString(drawStr) && !BankAuditUtil.isEmptyString(length)
				&& !BankAuditUtil.isEmptyString(start)) {
			try {
				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 = maintActionItemsService.getActionItemListByauditTypeCode(legalEntityCode, auditTypeCode, status, search, orderColumn, orderDirection, page, size);
				System.out.println("Inside controller dataTableResponse .... "+dataTableResponse);
				dataTableResponse.setDraw(draw);

			} catch (Exception e) {
				dataTableResponse = new DataTableResponse();
				dataTableResponse.setError(e.getMessage());
				logger.error(e.getStackTrace()); 
			}

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

		return dataTableResponse;
	}	
	
	
@RequestMapping(value="/getAllActionItemByauditTypeCode",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getAllActionItemByauditTypeCode(
			@RequestParam("legalEntityCode")Integer legalEntityCode, 
			@RequestParam("auditTypeCode") String auditTypeCode, 
			@RequestParam("status") String status){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info(" Inside getAllActionItemByauditTypeCode .."+legalEntityCode  +" :: "+auditTypeCode+" :: " +status );		 
			try {
				List<MaintActionItemWrk> maintActionItemWrkList = maintActionItemsService.getAllActionItemListByauditTypeCode(legalEntityCode, auditTypeCode, status);
				serviceStatus.setResult(maintActionItemWrkList);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully fetched the record");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				logger.error(e.getStackTrace());
			} 
		
		return serviceStatus; 
	} 
	
	/*
@RequestMapping(value="/addActionItem",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus addActionItem(@RequestBody MaintActionItemWrk maintActionItemWrk){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(maintActionItemWrk!=null){
			try {
				maintActionItemsService.saveActionItem(maintActionItemWrk);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successsfully saved");
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				logger.error(e.getStackTrace());
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("at least one observation is required ");
		}		
		return serviceStatus; 
	}*/
	
	
@RequestMapping(value="/updateActionItem",method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateActionItem(@RequestBody MaintActionItemDto maintActionItemDto){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(maintActionItemDto!=null){
			try {
				String message = maintActionItemsService.saveActionItem(maintActionItemDto);
				if(BankAuditConstant.SUCCESS.equalsIgnoreCase(message)){
					serviceStatus.setStatus("success");
					serviceStatus.setMessage("successsfully saved");
				}else{
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("failure");
				}
			} catch (Exception e) {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				logger.error(e);
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}		
		return serviceStatus; 
	}
	
	
@RequestMapping(value="/authorizeActionItem",method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus authorizeActionItem(@RequestBody MaintActionItemDto maintActionItemDto){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info(" Inside authorizeActionItem .."+maintActionItemDto.getLegalEntityCode()  +" :: "+maintActionItemDto.getAuditTypeCode() );		 
		 
		String message = maintActionItemsService.authorizeActionItem(maintActionItemDto);				 
		if(BankAuditConstant.SUCCESS.equalsIgnoreCase(message)){
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully authorised");
		}else{
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
		}
		
			 
		
		return serviceStatus; 
	} 
		 
	 
	
}
