package com.enactor.retail.pos.stepDefinitions;

import com.enactor.pos.IPosStepsProvider;
import com.enactor.pos.PosTestManager;
import com.enactor.core.automation.api.CommonApiSteps;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

/**
 * Additional Automation steps.
 * Imply both common pos steps and common api steps combination
 * Reusable steps - combination of more than one common pos steps
 * eg - complete transaction with cash tender
 *
 * @author Charith Gunathilaka
 */
public class AdditionalPosSteps extends CommonApiSteps {

	private static final String INPUT_TEXT_FIELD = "Input Box";
	private static final String ENTER_BUTTON_LABEL = "ENT";
	private static final String TOTAL_BUTTON_LABEL = "TOTAL";
	private static final String CASH_BUTTON_LABEL = "Cash";
	private static final String OK_BUTTON_LABEL = "OK";
	private static final String CLOSE_BUTTON_LABEL = "Close";

	private static final String TRANSACTION_BASKET_WINDOW = "Transaction Basket";
	private static final String SELECT_TENDER_WINDOW = "Select Tender";
	private static final String ENTER_CASH_TENDER_AMOUNT_WINDOW = "Enter Cash Tender Amount";
	private static final String WAIT_DRAWER_CLOSED_WINDOW = "Await Drawer Closed";
	private static final String CASH_DRAWER_WINDOW = "Cash Drawer";

	private final IPosStepsProvider stepsProvider = PosTestManager.getInstance().getStepsProvider();

	/**
	 * Adding an item to the basket.
	 * eg: product, gift card, etc.
	 *
	 * @param id
	 * @param description
	 * @throws Exception
	 */
	@When("user submits \"(.+)\" on the basket")
	public void addItemToBasket(String item) throws Throwable {
		stepsProvider.enterText(item, INPUT_TEXT_FIELD, CommonPosSteps.currentPage);
		stepsProvider.pressButton(ENTER_BUTTON_LABEL, CommonPosSteps.currentPage);
	}

	/**
	 * User completes the transaction by cash tender.
	 * Note: This step can't be used if customer signature verification
	 * required during the tender.
	 *
	 * @throws Throwable
	 */
	@Then("user completes the transaction by cash tender")
	public void completeByCashTender() throws Throwable {
		pressButtonAndCheckPageShown(TOTAL_BUTTON_LABEL, SELECT_TENDER_WINDOW);
		pressButtonAndCheckPageShown(CASH_BUTTON_LABEL, ENTER_CASH_TENDER_AMOUNT_WINDOW);
		pressButtonAndCheckPageShown(OK_BUTTON_LABEL, WAIT_DRAWER_CLOSED_WINDOW);
		stepsProvider.pressButtonOnTestPeripheral(CLOSE_BUTTON_LABEL, CASH_DRAWER_WINDOW);
		stepsProvider.checkWindow(TRANSACTION_BASKET_WINDOW);
		CommonPosSteps.currentPage = TRANSACTION_BASKET_WINDOW;
	}
	/**
	 * Read and Validate latest transaction xml file ,after transaction 
	 *
	 * @throws Throwable
	 * @param dataTable
	 *  
	 * ex: DataTable
	 * | Transaction xml Property |  value | 
	 */
	@And("^latest transaction xml contains$")
    public void validateLatestTransactionXML(DataTable table) throws Throwable {
    	String filePath = "RetailTransaction/POS/";
        stepsProvider.readTransactionXmlFromPath(filePath);
        responseContains(table);
    }

	private void pressButtonAndCheckPageShown(String label, String page) throws Throwable {
		stepsProvider.pressButton(label, CommonPosSteps.currentPage);
		stepsProvider.checkWindow(page);
		CommonPosSteps.currentPage = page;

	}
}