package com.enactor.retail.pos.stepDefinitions;

import com.enactor.core.automation.AUT.AutomationException;
import com.enactor.core.automation.api.CommonApiSteps;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
/**
 * API related common cucumber steps definitions
 * 
 * @author harsha
 */
public class ApiCucumberCommonSteps extends CommonApiSteps {

	/**
	 * set the baseUrl to a variable called baseUrl. can be later refer it as
	 * {baseUrl}
	 *
	 * @param baseUrl
	 * @throws AutomationException
	 */
	@Given("^base URL is set to \"(.*?)\"$")
	public void baseUrlIsSetTo(String baseUrl) throws AutomationException {
		setupBaseUrl(baseUrl);
	}

	/**
	 * set the file path to a variable called filePath. can be later refer it as
	 * {filePath}
	 *
	 * @param filePath
	 * @throws AutomationException
	 */
	@Given("^base file path is set to \"(.*?)\"$")
	public void baseFilePathIsSetTo(String filePath) throws AutomationException {
		setFilePath(filePath);
	}

	/**
	 * set the service end point.
	 *
	 * @param endpoint
	 * @throws AutomationException
	 */
	@Given("^service path is set to \"(.*?)\"$")
	public void apiUserSetApiEndpoint(String endpoint) throws AutomationException {
		userSetApiEndpoint(endpoint);
	}

	/**
	 * set the api endpoint
	 *
	 * @param table
	 * @throws Exception
	 */
	@And("^user set query parameters$")
	public void apiUserSetQueryParamsToEndpoint(DataTable table) throws Exception {
		userSetQueryParamsToEndpoint(table);
	}

	/**
	 * set the headers to endpoint
	 *
	 * @param table
	 * @throws AutomationException
	 */
	@And("^headers set to$")
	public void apiUserSetHeadersToEndpoint(DataTable table) throws AutomationException {
		userSetHeadersToEndpoint(table);
	}

	/**
	 * send request to the end point in given format
	 *
	 * @param type
	 * @param body
	 * @throws AutomationException
	 */
	@When("^HTTP \"([^\"]*)\" request with Request Body \"(.*)\" is sent$")
	public void apiSendHttpRequestWithRequestBody(String type, String body) throws Exception {
		sendHttpRequestWithRequestBody(type, body);
	}

	/**
	 * send request to the end point in given format
	 *
	 * @param type
	 * @param filename
	 * @throws Exception
	 */
	@When("^HTTP \"([^\"]*)\" request with Request Body from file \"([^\"]*)\" is sent$")
	public void apiSendHttpRequestWithRequestBodyFromFile(String type, String filename) throws Exception {
		sendHttpRequestWithRequestBodyFromFile(type, filename);
	}

	/**
	 * save property to a variable using XPath if it s josn then it will convert
	 * into XML and use XPath
	 *
	 * @param property
	 * @param variable
	 * @throws Exception
	 */
	@When("^property \"([^\"]*)\" extracted to the variable \"([^\"]*)\"$")
	public void apiPropertyExtractedToVariable(String property, String variable) throws Exception {
		propertyExtractedToVariable(property, variable);
	}

	/**
	 * Extract the Node to the variable If extracting object type is string do not
	 * use this.
	 *
	 * @param property
	 * @param variable
	 * @param type
	 * @throws Exception
	 */
	@When("^property \"([^\"]*)\" extracted to the variable \"([^\"]*)\" of type \"([^\"]*)\"$")
	public void apiPropertyExtractedToVariableOfType(String property, String variable, String type) throws Exception {
		propertyExtractedToVariableOfType(property, variable, type);
	}

	/**
	 * Extract the value from header into a variable.
	 *
	 * @param headerKey
	 * @param variable
	 * @throws Exception
	 */
	@When("^property \"([^\"]*)\" extracted to the variable \"([^\"]*)\" from header$")
	public void apiPropertyExtractedToVariableFromHeader(String headerKey, String variable) throws Exception {
		propertyExtractedToVariableFromHeader(headerKey, variable);
	}

	/**
	 * Check the response contains given values specified in given XPaths
	 *
	 * @param table
	 * @throws AutomationException
	 */
	@And("^response contains$")
	public void apiResponseContains(DataTable table) throws AutomationException {
		responseContains(table);
	}

	/**
	 * Check the response contains given values specified in given JSONPath
	 *
	 * @param table
	 * @throws AutomationException
	 */
	@And("^response contains with JSONpath$")
	public void apiResponseContainsWithJsonPath(DataTable table) throws AutomationException {
		responseContainsWithJsonPath(table);
	}

	/**
	 * Check the response status code is matched with given status code
	 *
	 * @param statusCode
	 * @throws AutomationException
	 */
	@Then("^response status code is \"([^\"]*)\"$")
	public void apiResponseStatusCodeIs(String statusCode) throws AutomationException {
		responseStatusCodeIs(statusCode);
	}

	/**
	 * Check the response headers have given values in given header name
	 *
	 * @param table
	 * @throws AutomationException
	 */
	@And("^response headers have$")
	public void apiResponseHeadersHave(DataTable table) throws AutomationException {
		responseHeadersHave(table);
	}

	/**
	 * Set Authentication for basic Authentication enactor.auth.Username and
	 * enactor.auth.Password are needed to pass as key values pairs
	 *
	 * @param table
	 * @throws AutomationException
	 */
	@And("^authorisation tokens set$")
	public void authorisationTokensSetToBasicAuthentication(DataTable table) throws AutomationException {
		authorisationTokensSet(table);
	}

	/**
	 * Compare the response body with given content
	 *
	 * @param comparingContent
	 * @throws Exception
	 */
	@And("^response body/content exactly matches \"(.*)\"$")
	public void apiResponseBodyExactlyMatches(String comparingContent) throws Exception {
		responseBodyExactlyMatches(comparingContent);
	}

	/**
	 * Compare the response body with given content from file
	 *
	 * @param filename
	 * @throws Exception
	 */
	@And("^response body/content exactly matches file \"([^\"]*)\"$")
	public void apiResponseBodyExactlyMatchesFromFile(String filename) throws Exception {
		responseBodyExactlyMatchesFromFile(filename);
	}

	/**
	 * Compare the response body is a superset of given content
	 *
	 * @param comparingContent
	 * @throws Exception
	 */
	@And("^response is a superset of \"(.*)\"$")
	public void apiResponseIsSupersetOf(String comparingContent) throws Exception {
		responseIsSupersetOf(comparingContent);
	}

	/**
	 * Compare the response body is a superset of given content from file
	 *
	 * @param filename
	 * @throws Exception
	 */
	@And("^response is a superset of file \"(.*)\"$")
	public void apiResponseIsSupersetOfFile(String filename) throws Exception {
		responseIsSupersetOfFile(filename);
	}

	/**
	 * Compare the response body with given schema file
	 *
	 * @param filename
	 * @throws Exception
	 */
	@And("^response is valid with schema \"([^\"]*)\"$")
	public void apiResponseIsValidWithSchema(String filename) throws Exception {
		responseIsValidWithSchema(filename);
	}

	/**
	 * Compare the variable contains given values in given Xpath
	 *
	 * @param variable
	 * @param table
	 * @throws Exception
	 */
	@And("^variable \"([^\"]*)\" contains$")
	public void apiVariableContains(String variable, DataTable table) throws AutomationException {
		variableContains(variable, table);
	}

	/**
	 * Compare the variable contains given values in given Xpath
	 *
	 * @param variable
	 * @param filename
	 * @throws Exception
	 */
	@And("^variable \"([^\"]*)\" is a superset of file \"([^\"]*)\"$")
	public void apiVariableIsSupersetOfFile(String variable, String filename) throws Exception {
		variableIsSupersetOfFile(variable, filename);
	}

	/**
	 * Compare the variable is a superset of content Content type is also should
	 * provide
	 *
	 * @param variable
	 * @param comparingContent
	 * @param type
	 * @throws Exception
	 */
	@And("^variable \"([^\"]*)\" is a superset of \"(.*)\" in format \"([^\"]*)\"$")
	public void apiVariableIsSupersetOfWithContent(String variable, String comparingContent, String type)
			throws Exception {
		variableIsSupersetOfWithContent(variable, comparingContent, type);
	}

	/**
	 * Check idempotency of JSON -> XML -> JSON
	 *
	 * @param comparingContent
	 * @throws Exception
	 */
	@And("^check idempotency of the JSON transaction \"(.*)\"$")
	public void apiCheckIdempotencyOfTheJSONTransaction(String comparingContent) throws Exception {
		checkIdempotencyOfTheJSONTransaction(comparingContent);
	}

	/**
	 * Check idempotency of JSON -> XML -> JSON input is given as file content
	 *
	 * @param filename
	 * @throws Exception
	 */
	@And("^check idempotency of the JSON transaction from file \"([^\"]*)\"$")
	public void apiCheckIdempotencyOfTheJSONTransactionFromFile(String filename) throws Exception {
		checkIdempotencyOfTheJSONTransactionFromFile(filename);
	}

	/**
	 * Check idempotency of XML ->JSON -> XML
	 *
	 * @param comparingContent
	 * @throws Exception
	 */
	@And("^check idempotency of the XML transaction \"(.*)\"$")
	public void apiCheckIdempotencyOfTheXMLTransaction(String comparingContent) throws Exception {
		checkIdempotencyOfTheXMLTransaction(comparingContent);
	}

	/**
	 * Check idempotency of XML ->JSON -> XML input is given as file content
	 *
	 * @param filename
	 * @throws Exception
	 */
	@And("^check idempotency of the XML transaction from file \"([^\"]*)\"$")
	public void apiCheckIdempotencyOfTheXMLTransactionFromFile(String filename) throws Throwable {
		checkIdempotencyOfTheXMLTransactionFromFile(filename);
	}

}
