package com.bankaudit.rest.process.controller;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bankaudit.rest.process.service.EmailService;
import com.bankaudit.util.ServiceStatus;



@RestController

@RequestMapping("/api/email")
public class EmailController {
	
	@Autowired
	private EmailService emailService;
	
	static final Logger logger = Logger.getLogger(EmailController.class);
	
	/************************************ Email implementation for DOS Supersoft ***************************************/
	
@RequestMapping(value="/sendInspectionInitiate/{legalEntityCode}/{auditId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus sendInspectionInitiate(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("auditId") String auditId){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null){			
			try {
			    String sendStatus=emailService.sendInspectionInitiate(legalEntityCode, auditId);
			    if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
	
	
@RequestMapping(value="/sendEmailForTest/{legalEntityCode}/{auditId}/{status}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus sendEmailForTest(@PathVariable("legalEntityCode")Integer legalEntityCode, @PathVariable("auditId") String auditId,
			@PathVariable("status") String status){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info("sendEmailForTest :: "+ legalEntityCode +" :: "+auditId+ " :: "+status);
		if(legalEntityCode!=null){			
			try {
				if("AI".equalsIgnoreCase(status)) {
					emailService.sendInspectionInitiate(legalEntityCode, auditId);
				}else if("AE".equalsIgnoreCase(status)) {
					emailService.sendInspectionExit(legalEntityCode, auditId);
				}
				if("RAR".equalsIgnoreCase( status)) {
					emailService.sendReadyForReview(legalEntityCode, auditId);
				}else if("RD".equalsIgnoreCase( status)) {
					emailService.sendInspectionDispatch(legalEntityCode, auditId);
				}
				else if("AR".equalsIgnoreCase(status) || "RS".equalsIgnoreCase(status)) {
					emailService.sendComplianceSubmission(legalEntityCode, auditId);
				}else if("AW".equalsIgnoreCase(status) || "WS".equalsIgnoreCase(status)) {
					emailService.sendFurtherCompliance(legalEntityCode, auditId);
				}
				// String sendStatus=emailService.sendInspectionInitiate(legalEntityCode, auditId);
			    /*if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}*/
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
	
	
@RequestMapping(value="/sendEmailForInspectionNotification/{legalEntityCode}/{auditTypeCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus sendEmailForInspectionNotification(@PathVariable("legalEntityCode")Integer legalEntityCode, @PathVariable("auditTypeCode") String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info("sendEmailForTest :: "+ legalEntityCode +" :: "+auditTypeCode);
		if(legalEntityCode!=null){			
			try {
			    String sendStatus=""; //emailService.sendEmailForInspectionNotification(legalEntityCode);
			    if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
	
@RequestMapping(value="/sendEmailForInspectionAboutToStart/{legalEntityCode}/{auditTypeCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus sendEmailForInspectionAboutToStart(@PathVariable("legalEntityCode")Integer legalEntityCode, @PathVariable("auditTypeCode") String auditTypeCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		logger.info("sendEmailForTest :: "+ legalEntityCode +" :: "+auditTypeCode);
		if(legalEntityCode!=null){			
			try {
			    String sendStatus=""; //emailService.sendEmailForInspectionNotification(legalEntityCode);
			    if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
	
	
	/******* Dos supersoft, End here ************/
	
	
@RequestMapping(value="/sendEmailFirstNotification/{legalEntityCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getComplianceEventById(@PathVariable("legalEntityCode")Integer legalEntityCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null){			
			try {
			    String sendStatus=emailService.sendEmailFirstNotification(legalEntityCode);
			    if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
	
@RequestMapping(value="/sendEmailBeforeDueNotification/{legalEntityCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus sendEmailBeforeDueNotification(@PathVariable("legalEntityCode")Integer legalEntityCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null){			
			try {
			    String sendStatus=emailService.sendEmailBeforeDueNotification(legalEntityCode);
			    if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
	
@RequestMapping(value="/sendEmailOnCompliance/{legalEntityCode}/{complianceEventId}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus sendEmailOnCompliance(@PathVariable("legalEntityCode")Integer legalEntityCode, 
			@PathVariable("complianceEventId")Integer complianceEventId){
		ServiceStatus serviceStatus=new ServiceStatus();
		if(legalEntityCode!=null){			
			try {
			    String sendStatus=emailService.sendEmailOnCompliance(legalEntityCode, complianceEventId);
			    if(sendStatus!=null){
			    	serviceStatus.setResult(sendStatus);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("MaintAuditGroup not found");
				}
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}return serviceStatus;
	}
	
}
