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

import java.sql.Timestamp;
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.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.MaintAddress;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.service.MaintAddressService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;


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

@RestController

@RequestMapping("/api/maintAddress")
public class MaintAddressController {
	
	/**
	 * The maint address service is autowired and make methods available from
	 * service layer .
	 */
	@Autowired
	MaintAddressService maintAddressService;
	
	/**
	 * The business date service is autowired and make methods available from
	 * service layer .
	 */
	@Autowired
	BusinessDateService businessDateService;
	
	/** The Constant logger is used to specify the . */
	static final Logger logger = Logger.getLogger(MaintAddressController.class);
	
	/**
	 * This method is use to Creates the maint address.
	 *
	 * @param maintAddress
	 *            specify the maint address
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus createMaintAddress(@RequestBody MaintAddress maintAddress){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		System.out.println("Inside createMaintAddress .... ");
		try{
			if(maintAddress!=null
					&& !BankAuditUtil.isEmptyString(maintAddress.getCountryAlpha3Code())
					&& !BankAuditUtil.isEmptyString(maintAddress.getStateCode()) 
					&& !BankAuditUtil.isEmptyString(maintAddress.getDistrictCode())
					&& !BankAuditUtil.isEmptyString(maintAddress.getCityCode())
					&& !BankAuditUtil.isEmptyString(maintAddress.getMaker())
					){ 
				    maintAddress.setStatus("A");
				   // maintAddress.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(maintAuditActivity.getLegalEntityCode()));
				    maintAddress.setBusinessdateTimestamp(new Timestamp(System.currentTimeMillis()));
				    maintAddress.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				    maintAddress.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
				    
				    maintAddressService.createMaintAddress(maintAddress);
				    
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully created");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("invalid payload ");
			}
		}catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure"+e.getMessage());
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}
		}
		
		return serviceStatus;
	}
	
	/**
	 * This method is use to Update maint address.
	 *
	 * @param maintAddress
	 *            specify the maint address
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(method=RequestMethod.PUT,consumes=MediaType.APPLICATION_JSON_VALUE,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus updateMaintAddress(@RequestBody MaintAddress maintAddress){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		try {
			if(maintAddress!=null
					&& !BankAuditUtil.isEmptyString(maintAddress.getCountryAlpha3Code())
					&& !BankAuditUtil.isEmptyString(maintAddress.getStateCode()) 
					&& !BankAuditUtil.isEmptyString(maintAddress.getDistrictCode())
					&& !BankAuditUtil.isEmptyString(maintAddress.getCityCode())
					&& !BankAuditUtil.isEmptyString(maintAddress.getMaker())
					){  
				maintAddress.setStatus("A");
				maintAddress.setBusinessdateTimestamp(new Timestamp(System.currentTimeMillis()));
				maintAddress.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				maintAddress.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
				    
				maintAddressService.updateMaintAddress(maintAddress);
				    
				 serviceStatus.setStatus("success");
				 serviceStatus.setMessage("successfully updated");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("invalid payload ");
			}
		}catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure"+e.getMessage());
			if(e instanceof org.springframework.dao.DataIntegrityViolationException) {
				serviceStatus.setMessage("DATAINTGRTY_VIOLATION");
			}
		}
		
		return serviceStatus;
	}
	
	/**
	 * This method is use to Gets the maint address.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/getAll/{legalEntityCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMaintAddress(@PathVariable("legalEntityCode")Integer legalEntityCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		try {
			    List<MaintAddress> maintAddress=maintAddressService.getAllAddress(legalEntityCode);
			    if(maintAddress!=null){
			    	serviceStatus.setResult(maintAddress);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAddress not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		return serviceStatus;
	}
	
	/**
	 * This method is use to Gets the maint address country.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/getAllCountryCode/{legalEntityCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMaintAddress_Country(@PathVariable("legalEntityCode")Integer legalEntityCode){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		try {
			    List<MaintAddress> maintAddress=maintAddressService.getAllCountryCode(legalEntityCode);
			    if(maintAddress!=null){
			    	serviceStatus.setResult(maintAddress);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAddress not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		return serviceStatus;
	}
	
	/**
	 * This method is use to Gets the maint address.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param countryAlpha3Code
	 *            specify the country alpha 3 code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/getStatesByCountryCode/{legalEntityCode}/{countryAlpha3Code}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMaintAddress(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("countryAlpha3Code") String countryAlpha3Code
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		System.out.println("Inside getStatesByCountryCode is.... "+ countryAlpha3Code);
		if(!BankAuditUtil.isEmptyString(countryAlpha3Code)){
			
			try {
				List<MaintAddress> maintAddress=maintAddressService.getStatesbyCountry(legalEntityCode,countryAlpha3Code);
			    if(maintAddress!=null){
			    	serviceStatus.setResult(maintAddress);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAddressService not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}
	
	/**
	 * This method is use to Gets the maint address.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param countryAlpha3Code
	 *            specify the country alpha 3 code
	 * @param stateCode
	 *            specify the state code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/getDistricts/{legalEntityCode}/{countryAlpha3Code}/{stateCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMaintAddress(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("countryAlpha3Code") String countryAlpha3Code,
			@PathVariable("stateCode") String stateCode
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		System.out.println("getStatesbyCountry in IMPL ...."+ countryAlpha3Code);
		
		if(!BankAuditUtil.isEmptyString(countryAlpha3Code) && !BankAuditUtil.isEmptyString(stateCode)){
			
			try {
				List<MaintAddress> maintAddress=maintAddressService.getDistrictsByCountryState(legalEntityCode,countryAlpha3Code,stateCode);
			    if(maintAddress!=null){
			    	serviceStatus.setResult(maintAddress);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAddressService not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure");
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}
	
	/**
	 * This method is use to Gets the maint address.
	 *
	 * @param legalEntityCode
	 *            specify the legal entity code
	 * @param countryAlpha3Code
	 *            specify the country alpha 3 code
	 * @param stateCode
	 *            specify the state code
	 * @param districtCode
	 *            specify the district code
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/getCities/{legalEntityCode}/{countryAlpha3Code}/{stateCode}/{districtCode}",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	ServiceStatus getMaintAddress(@PathVariable("legalEntityCode")Integer legalEntityCode,
			@PathVariable("countryAlpha3Code") String countryAlpha3Code,
			@PathVariable("stateCode") String stateCode,@PathVariable("districtCode") String districtCode
			){
		ServiceStatus serviceStatus=new ServiceStatus();
		
		if(!BankAuditUtil.isEmptyString(countryAlpha3Code) && !BankAuditUtil.isEmptyString(stateCode)){
			
			try {
				List<MaintAddress> maintAddress=maintAddressService.getCitiesByCountryStateDist(legalEntityCode,countryAlpha3Code,stateCode,districtCode);
			    if(maintAddress!=null){
			    	serviceStatus.setResult(maintAddress);
				    serviceStatus.setStatus("success");
				    serviceStatus.setMessage("successfully retrieved ");	
			    }else {
					serviceStatus.setStatus("failure");
					serviceStatus.setMessage("maintAuditActivity not found");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("failure"+e.getMessage());
			}
			
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("invalid payload ");
		}
		
		return serviceStatus;
	}

}
