package cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.enums.TransactionReasonPAE;
import cz.moneta.test.dsl.payment_engine.home.PaymentEngineHomePage;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchPage.ID;
import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchPage.SEARCH_BUTTON;
import static cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_search.PaymentEngineSearchPageLifeCycle.REFRESH_BUTTON_LIFE_CYCLE;

@Wait({ID, SEARCH_BUTTON})
public interface PaymentEngineSearchPage extends WebFlow<PaymentEngineSearchPage>, StoreAccessor {

    String ADVANCED_FILTER = "//div[label[text()='Rozšířený filtr'] and @class='  xcheckbox-wrap']/a";
    String ID = "//div[@id='Vyhledávání']//input[@name='field1#id']";
    String DEBTOR_ACC = "//div[@id='Vyhledávání']//input[@name='field1#orAccIdent']";
    String SETTLEMENT_FROM = "//input[@name='field1#settDate']//following-sibling::input";
    String SETTLEMENT_TO = "//input[@name='field2#settDate']//following-sibling::input";
    String PAY_SCHEME = "//input[@name='field1#productCode']//following-sibling::input[contains(@class, 'x-field-editable')]";
    String PAY_SCHEME_IMG = PAY_SCHEME + "/following-sibling::img";
    String PAY_SCHEME_FILLED = "//input[@name='field1#productCode' and not(@value='')]";
    String FRONT_INPUT = "//input[@name='field1#queueId']//following-sibling::input[contains(@class, 'x-field-editable')]";
    String FRONT_IMG = FRONT_INPUT + "/following-sibling::img";
    String FRONT_LABEL = "//div[text()='%s']/../..//div[@class='x-grid3-cell-inner x-grid3-col-6']";
    String FRONT_FILLED = "//input[@name='field1#queueId' and not(@value='')]";
    String REASON = "//input[@name='field1#queueReason']//following-sibling::input[contains(@class, 'x-field-editable')]";
    String REASON_IMG = REASON + "/following-sibling::img";
    String REASON_FILLED = "//input[@name='field1#queueReason' and not(@value='')]";
    String SELECTION_DIV = "//div[text()='%s']";
    String UETR_INPUT = "//input[@name='field1#uetr']";
    String SEARCH_BUTTON = "//div[@id = 'Vyhledávání']//button[text() = 'Hledat']/../..";
    String SEARCH_FORM_BUTTON = "//button[@class='x-btn-text  grid-filter-collapse']";
    String CHECK_SELECTED_TRANSACTION_DATA_PATH = "//*[contains(@class,'x-grid3-row-selected')]//*[contains(text(),'%s')]";
    String REFRESH_BUTTON = "//div[@id='Vyhledávání']//div[@class='x-toolbar x-small-editor']//button[@class='x-btn-text x-tbar-loading']";
    String TRANSACTION_DETAIL = "//a[text()='Detail']";
    String TRANSACTION_FILES = "//a[text()='Soubory']";
    String TRANSACTION_LIFE_CYCLE = "//a[text()='Životní cyklus']";
    String CLOSE_TAB_BUTTON = "//a[@class='x-tab-strip-close']";
    String TRANSACTION_ID_KEY = "TRANSACTION_ID";
    String TRANSACTION_ID_XPATH = "//div[@class='x-panel-body x-panel-body-noheader x-border-layout-ct']//div[contains(@class,'x-grid3-row-selected')]//div[@class='x-grid3-cell-inner x-grid3-col-2']";
    String EQUIVALENT_AMOUNT_KEY = "EQUIVALENT_AMOUNT_KEY";
    String EQUIVALENT_AMOUNT_EUR_XPATH = "//div[@class='x-grid3-scroller']//div[text()='EUR']/../following-sibling::td[contains(@class, 'x-grid3-td-11')]/div";
    String EQUIVALENT_AMOUNT_CZK_XPATH = "//div[@class='x-grid3-scroller']//div[text()='CZK']/../following-sibling::td[contains(@class, 'x-grid3-td-11')]/div";
    String EQUIVALENT_AMOUNT_USD_XPATH = "//div[@class='x-grid3-scroller']//div[text()='USD']/../following-sibling::td[contains(@class, 'x-grid3-td-11')]/div";
    String AJAX_LOADING = "//div[text()='Načítám...']";

    @Wait(value = ID, until = Until.VISIBLE)
    @TypeInto(value = ID, clear = true)
    PaymentEngineSearchPage typeId(String id);

    @Click(ADVANCED_FILTER)
    PaymentEngineSearchPage clickAdvancedFilter();

    @Wait(value = DEBTOR_ACC, until = Until.VISIBLE)
    @TypeInto(DEBTOR_ACC)
    PaymentEngineSearchPage typeDebtorAcc(String debtorAcc);

    @Wait(value = SETTLEMENT_FROM, until = Until.VISIBLE)
    PaymentEngineSearchPage waitForSettlementDateFrom();

    @Wait(value = SETTLEMENT_TO, until = Until.VISIBLE)
    @TypeIntos({@TypeInto(value = SETTLEMENT_FROM, clear = true), @TypeInto(value = SETTLEMENT_TO, clear = true)})
    PaymentEngineSearchPage typeSettlementDates(String settlementFrom, String settlementTo);

    @Wait(value = PAY_SCHEME, until = Until.VISIBLE)
    @Click(value = PAY_SCHEME_IMG, andWait = @Wait(value = SELECTION_DIV, until = Until.CLICKABLE, isStringDynamicXpath = true))
    @Click(value = SELECTION_DIV, isStringDynamicXpath = true, andWait = @Wait(value = PAY_SCHEME_FILLED, until = Until.PRESENT_IN_DOM, explicitWaitSeconds = 1))
    PaymentEngineSearchPage selectPayScheme(String payScheme);

    @Wait(value = FRONT_INPUT, until = Until.VISIBLE)
    @Click(value = FRONT_IMG, andWait = @Wait(value = SELECTION_DIV, until = Until.CLICKABLE, isStringDynamicXpath = true))
    @Click(value = SELECTION_DIV, isStringDynamicXpath = true, andWait = @Wait(value = FRONT_FILLED, until = Until.PRESENT_IN_DOM, explicitWaitSeconds = 1))
    PaymentEngineSearchPage selectFront(String front);

    @Wait(value = REASON, until = Until.VISIBLE)
    @Click(value = REASON_IMG, andWait = @Wait(value = SELECTION_DIV, until = Until.CLICKABLE, isStringDynamicXpath = true))
    @Click(value = SELECTION_DIV, isStringDynamicXpath = true, andWait = @Wait(value = REASON_FILLED, until = Until.PRESENT_IN_DOM, explicitWaitSeconds = 1))
    PaymentEngineSearchPage selectReason(String reason);

    @TypeInto(UETR_INPUT)
    PaymentEngineSearchPage fillUetr(String uetr);

    @Wait(value = SEARCH_BUTTON, until = Until.VISIBLE)
    @Click(value = SEARCH_BUTTON, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineSearchPage clickSearch();

    @Click(SEARCH_FORM_BUTTON)
    PaymentEngineSearchPage clickSearchForm();

    @CheckElementPresent(value = CHECK_SELECTED_TRANSACTION_DATA_PATH, isStringDynamicXpath = true)
    PaymentEngineSearchPage checkSelectedTransactionData(String data);

    @Click(TRANSACTION_DETAIL)
    PaymentEngineSearchTransactionDetail clickOnTransactionDetail();

    @Click(TRANSACTION_FILES)
    PaymentEngineSearchTransactionFiles clickOnTransactionFiles();

    @Click(CLOSE_TAB_BUTTON)
    PaymentEngineHomePage closeTab();

    @CustomAction
    default PaymentEngineSearchPage waitForStatus(Harness harness, TransactionReasonPAE status) {
        PaymentEngineUtils.waitForElementPresence(harness, String.format("//*[contains(@class,'x-grid3-row-selected')]//*[contains(text(),'%s')]", status.getStatus()), REFRESH_BUTTON);
        return null;
    }

    @StoreElementContent(storeKey = TRANSACTION_ID_KEY, value = TRANSACTION_ID_XPATH, by = Lookup.XPATH)
    PaymentEngineSearchPage storeSelectedTransactionId();

    @StoreElementContent(storeKey = EQUIVALENT_AMOUNT_KEY, value = EQUIVALENT_AMOUNT_EUR_XPATH, by = Lookup.XPATH)
    PaymentEngineSearchPage storeEquivalentAmountEur();

    @StoreElementContent(storeKey = EQUIVALENT_AMOUNT_KEY, value = EQUIVALENT_AMOUNT_CZK_XPATH, by = Lookup.XPATH)
    PaymentEngineSearchPage storeEquivalentAmountCzk();

    @StoreElementContent(storeKey = EQUIVALENT_AMOUNT_KEY, value = EQUIVALENT_AMOUNT_USD_XPATH, by = Lookup.XPATH)
    PaymentEngineSearchPage storeEquivalentAmountUsd();

    @Wait(explicitWaitSeconds = 3)
    PaymentEngineSearchPage wait3sec();

    @Wait(explicitWaitSeconds = 5)
    PaymentEngineSearchPage wait5sec();

    @Click(value = TRANSACTION_LIFE_CYCLE, andWait = @Wait(value = REFRESH_BUTTON_LIFE_CYCLE, until = Until.VISIBLE))
    PaymentEngineSearchPageLifeCycle clickLifeCycle();

    @CheckElementContent(value = FRONT_LABEL, isStringDynamicXpath = true)
    PaymentEngineSearchPage checkExpectedFrontValueByTransactionId(String transactionId, String expectedFrontValue);

}