package com.bankaudit.rest.process.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.RestController;

import com.bankaudit.model.RiskScorePercentage;
import com.bankaudit.process.model.AuditPlanWrk;
import com.bankaudit.rest.process.service.AuditPlanService;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;





@RestController

@RequestMapping("/api/auditPlan")
public class AuditPlanController {
	 
	@Autowired
	AuditPlanService auditPlanWrkService;
	
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(AuditPlanController.class);
	
	
@RequestMapping(value="/wrk",method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus createAuditPlanWrk(@RequestBody AuditPlanWrk auditPlanWrk){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(auditPlanWrk!=null
				&& auditPlanWrk.getAuditScheduleWrks()!=null
				&& !auditPlanWrk.getAuditScheduleWrks().isEmpty()){
			try {
				auditPlanWrk.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditPlanWrk.getLegalEntityCode()));
				auditPlanWrk.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				auditPlanWrk.setMakerTimestamp(auditPlanWrk.getBusinessdateTimestamp());
				if(auditPlanWrk.getStatus().equalsIgnoreCase("U")){
					
					if(!auditPlanWrkService.isAuditPlanWrk(auditPlanWrk.getLegalEntityCode(), auditPlanWrk.getAuditTypeCode(), auditPlanWrk.getPlanStartDate(),auditPlanWrk.getPlanType())){
						auditPlanWrkService.createAuditPlanWrk(auditPlanWrk);	
						serviceStatus.setStatus("success");
						serviceStatus.setMessage("successfully created");
					}else {
						serviceStatus.setStatus("failure");
						//serviceStatus.setMessage("already there with given start date ");
						serviceStatus.setMessage("There is an inspection planned for the selected unit whose dates are conflicting with the proposed dates.");
					}
					
				}else {
					auditPlanWrkService.createAuditPlanWrk(auditPlanWrk);
					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");
		}
		serviceStatus.setResult(auditPlanWrk);
		return serviceStatus;
		
	}
	
	
@RequestMapping(value="/wrk",method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateAuditPlanWrk(@RequestBody AuditPlanWrk auditPlanWrk){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(auditPlanWrk!=null
				&& auditPlanWrk.getAuditScheduleWrks()!=null
				&& !auditPlanWrk.getAuditScheduleWrks().isEmpty()){
			try {
				auditPlanWrk.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditPlanWrk.getLegalEntityCode()));
				auditPlanWrk.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				
				//change based on status : unauth and modify				
				if(auditPlanWrk.getStatus().equalsIgnoreCase("U")||auditPlanWrk.getStatus().equalsIgnoreCase("M")
						|| auditPlanWrk.getStatus().equalsIgnoreCase("DF") || auditPlanWrk.getStatus().equalsIgnoreCase("R")){
					auditPlanWrk.setMakerTimestamp(auditPlanWrk.getBusinessdateTimestamp());	
				}
				
				auditPlanWrkService.updateAuditPlanWrk(auditPlanWrk);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully updated");
			} 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;
		
	}
	
	
	
	
	
@RequestMapping(value="/get/{id}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getRiskScorePercentage(@PathVariable("id")String id
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		serviceStatus.setResult(auditPlanWrkService.getByPlanId(id));
		
		return serviceStatus;
	}
	
	
	
	
}
