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

import java.sql.Timestamp;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
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.model.MaintLegalEntity;
import com.bankaudit.service.MaintLegalEntityService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;


/**
 * The Class {@link MaintLegalEntityController} provides the REST services to
 * manage the {@link MaintLegalEntity} class objects .
 *
 * @author amit.patel
 * @version 1.0
 */

@RestController

@RequestMapping("/api/maintLegalEntity")
public class MaintLegalEntityController {

	/**
	 * The maint legal entity service is autowired and make methods available
	 * from service layer .
	 */
	@Autowired
	MaintLegalEntityService maintLegalEntityService;
	
	/** The Constant logger is used to specify the . */
	static final Logger logger = Logger.getLogger(MaintLegalEntityController.class);
	
	
	/**
	 * This method is use to Creates the maint legal entity.
	 *
	 * @param maintLegalEntity
	 *            specify the maint legal entity
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE,consumes=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus createMaintLegalEntity(@RequestBody MaintLegalEntity maintLegalEntity){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(maintLegalEntity!=null
				&& !BankAuditUtil.isEmptyString(maintLegalEntity.getLegalEntityName())
				&& !BankAuditUtil.isEmptyString(maintLegalEntity.getEntityStatus())
				&& maintLegalEntity.getNumberOfLevels()!=null
				&& maintLegalEntity.getNumberOfLevels()>0
				&& !BankAuditUtil.isEmptyString(maintLegalEntity.getMaker())
				&& !BankAuditUtil.isEmptyString(maintLegalEntity.getChecker())
				 ){
			
			try {
				maintLegalEntity.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
				maintLegalEntity.setCheckerTimestamp(new Timestamp(System.currentTimeMillis()));
			    maintLegalEntity.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
			    maintLegalEntity.setBusinessdateTimestamp(new Timestamp(System.currentTimeMillis()));
			    maintLegalEntity.setStatus("A");
				maintLegalEntityService.createMaintLegalEntity(maintLegalEntity);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully created ");
			} catch (Exception e) {

				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
				if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
					serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
				}
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload");
		}
		
		return serviceStatus;
	}
	
	
	/**
	 * This method is use to Update entity status.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param entityStatus
	 *            specify the entity status
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/changeStatus/{legalEntityCode}/{entityStatus}" , method=RequestMethod.PUT,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateEntityStatus(
			@PathVariable("legalEntityCode") Integer legalEntityCode,
			@PathVariable("entityStatus") String entityStatus){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(legalEntityCode!=null
				&&!BankAuditUtil.isEmptyString(entityStatus)){
			try {
				
				maintLegalEntityService.updateEntityStatus(legalEntityCode,entityStatus);
				serviceStatus.setStatus("success");
				 serviceStatus.setMessage("successfully updated");
			} catch (Exception e) {

				e.printStackTrace();

				serviceStatus.setMessage("failure");
				serviceStatus.setStatus("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid status");
		}
		
		return serviceStatus;
		
	}
	
	/*MaintLegalEntity by legalEntityCode */
	/*
@RequestMapping(value = "/getMaintLegalEntity", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getUser(@RequestParam(required = true, value = "legalEntityCode") Integer legalEntityCode) {
		ServiceStatus serviceStatus = new ServiceStatus();
		try {
			MaintLegalEntity maintLegalEntity = maintLegalEntityService.getMaintLegalEntityById(legalEntityCode);
			if (maintLegalEntity != null) {
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfuly retrieved");
				serviceStatus.setResult(maintLegalEntity);
			} else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("no records found");
			}
		} catch (Exception e) {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			e.printStackTrace();
		}
		return serviceStatus;
	}*/
	
}
