package org.openbravo.test.mobile.retail.extmodules.selenium.tests.supportforattributes;

import org.junit.Test;
import org.openbravo.test.mobile.core.selenium.SeleniumHelper;
import org.openbravo.test.mobile.retail.example.selenium.TestIdExample;
import org.openbravo.test.mobile.retail.extmodules.selenium.TestIdExtModules;
import org.openbravo.test.mobile.retail.extmodules.selenium.terminals.WebPOSExtModulesTerminalHelper;
import org.openbravo.test.mobile.retail.mobilecore.database.DatabaseHelperInsertUpdateOrDelete;
import org.openbravo.test.mobile.retail.mobilecore.selenium.TestIdCore;

/**
 * @author yogas
 *
 */
public class SFA008VerifiedReturns extends WebPOSExtModulesTerminalHelper {
  private static String vbsTerminalTypeId = "BD39916225594B32A88983899CF05F72";

  @Test
  public void test() {
    SFA000EnvironmentSetup envSetup = new SFA000EnvironmentSetup();
    changeUseExternalInput(true);
    reload();
    envSetup.testEnablePreference(true);

    tap(TestIdCore.BUTTON_MENU);
    tap(TestIdExtModules.BUTTON_MENU_VERIFIEDRETURN);
    tap(TestIdExtModules.BUTTON_VERIFIEDRETURNS_SEARCH);
    tap(TestIdExtModules.BUTTON_VERIFIEDRETURNS_ROW1);
    tap(TestIdExtModules.BUTTON_VERIFIEDRETURNS_LINES_CHECKROW1);
    tap(TestIdExtModules.BUTTON_POPUP_APPLY);
    tap(TestIdExtModules.MODAL_VERIFIED_RETURN_TEXT_FIELD);
    SeleniumHelper.pressKeysFromString("L10001_20-03-2018");
    tap(TestIdExtModules.BUTTON_VERIFIED_RETURN_OK);
    verify(TestIdExtModules.LABEL_TOTALTOPAY, "-338.00");
    tap(TestIdExample.BUTTON_PAY);
    tap(TestIdExample.BUTTON_PAY_EXACT);
    tap(TestIdExample.BUTTON_PAY_DONE);
    verify(TestIdExtModules.LABEL_TOTALTOPAY, "0.00");
    changeUseExternalInput(false);
    envSetup.testEnablePreference(false);
  }

  private void changeUseExternalInput(final boolean isForTest) {
    final String sql = String.format("UPDATE obpos_terminaltype SET usebarcodescanner = '%s' "
        + "WHERE obpos_terminaltype_id = '%s'", isForTest ? "Y" : "N", vbsTerminalTypeId);
    new DatabaseHelperInsertUpdateOrDelete().execute(sql, 1);
  }

}
