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

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.GeneralParameter;
import com.bankaudit.service.GeneralParameterService;
import com.bankaudit.util.BankAuditUtil;
import com.bankaudit.util.ServiceStatus;


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

@RestController

@RequestMapping("/api/generalParameter")
public class GeneralParameterController {

	
	/**
	 * The general parameter service is autowired and make methods available
	 * from service layer .
	 */
	@Autowired
	GeneralParameterService generalParameterService;
	
	
	
	/**
	 * This method is use to Gets the general parameter.
	 *
	 * @param legalEntityCodeStr
	 *            specify the legal entity code str
	 * @param modCode
	 *            specify the mod code
	 * @param language
	 *            specify the language
	 * @param key1
	 *            specify the key 1
	 * @param key2
	 *            specify the key 2
	 * @param value
	 *            specify the value
	 * @param maker
	 *            specify the maker
	 * @return the service status class object with response status and payload
	 *         .
	 */
	
@RequestMapping(value="/get",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	ServiceStatus getGeneralParameter(
			@RequestParam(value="legalEntityCode",required=false) String legalEntityCodeStr,
			@RequestParam(value="modCode",required=false) String modCode,
			@RequestParam(value="language",required=false) String language,
			@RequestParam(value="key1",required=false) String key1,
			@RequestParam(value="key2",required=false) String key2,
			@RequestParam(value="value",required=false)String value,
			@RequestParam(value="maker",required=false)String maker){

		ServiceStatus serviceStatus=new ServiceStatus();
		try {
			Integer legalEntityCode=null;
			if(!BankAuditUtil.isEmptyString(legalEntityCodeStr)){
				 legalEntityCode=Integer.parseInt(legalEntityCodeStr);	
			}else {
				legalEntityCode=0;
			}
			
		List<GeneralParameter> generalParameters=null;
		
		generalParameters = generalParameterService.getGeneralParameter(legalEntityCode , modCode , language, key1,key2, value,maker);
			
		if(generalParameters!=null && !generalParameters.isEmpty()){
			serviceStatus.setResult(generalParameters);
			serviceStatus.setStatus("success");
			serviceStatus.setMessage("successfully retrieved");
		}else {
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("no records found");
			
		}
				
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			
		}
	
		return serviceStatus;
	}
	
	
@RequestMapping(value="/getParamKeyValue",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) 
	ServiceStatus getGeneralParameterKeyValue(
			@RequestParam(value="legalEntityCode",required=false) String legalEntityCodeStr,
			@RequestParam(value="modCode",required=false) String modCode,
			@RequestParam(value="language",required=false) String language,
			@RequestParam(value="key1",required=false) String key1,
			@RequestParam(value="key2",required=false) String key2,
			@RequestParam(value="value",required=false)String value,
			@RequestParam(value="maker",required=false)String maker){

		ServiceStatus serviceStatus=new ServiceStatus();
		try {
			Integer legalEntityCode=null;
			if(!BankAuditUtil.isEmptyString(legalEntityCodeStr)){
				 legalEntityCode=Integer.parseInt(legalEntityCodeStr);	
			}else {
				legalEntityCode=0;
			}
			
			Map<String, String> paramKeyValues=null;
			paramKeyValues = generalParameterService.getGeneralParameterKeyValue(legalEntityCode , modCode , language, key1,key2, value,maker);
			
			if(paramKeyValues!=null && !paramKeyValues.isEmpty()){
				serviceStatus.setResult(paramKeyValues);
				serviceStatus.setStatus("success");
				serviceStatus.setMessage("successfully retrieved");
			}else {
				serviceStatus.setStatus("failure");
				serviceStatus.setMessage("no records found");
				
			}
				
		} catch (Exception e) {
			e.printStackTrace();
			serviceStatus.setStatus("failure");
			serviceStatus.setMessage("failure");
			
		}
	
		return serviceStatus;
	}
	
}
