package cz.moneta.test.dsl.ib.account.current;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.components.AccountsAndTransactionsSubmenu;
import cz.moneta.test.dsl.ib.components.MainMenu;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.CheckElementPresent;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Select;
import cz.moneta.test.harness.support.web.TypeInto;
import cz.moneta.test.harness.support.web.Until;
import cz.moneta.test.harness.support.web.Wait;

import static cz.moneta.test.dsl.ib.account.current.TransactionListPage.TRANSACTION_LIST_HEADER;

@Wait(TRANSACTION_LIST_HEADER)
public interface TransactionListPage extends IbPageFlow<TransactionListPage>, MainMenu, AccountsAndTransactionsSubmenu {

    String TRANSACTION_LIST_HEADER = "//div[@id='document']//img[contains(@src, 'prehled_trans.gif')]";
    String FIRST_TRANSACTION_CELL = "//div[@id='document']//table[@id='movementList_1_2']//tr[@class='listBodyOdd']";
    String FILTER_TRANSACTIONS_BUTTON = "//div[@id='document']//form[@id='mainFrm']//img[contains(@src, 'zobrazit_b.gif')]";
    String DATE_FROM = "//input[@id='valueDateFrom']";
    String TRANSACTIONS_TYPES = "//select[@id='transactionType']";
    String DISPLAY_BUTTON = "//table[@id='helpNav']//img[contains(@src, 'images_new/zobrazit_b.gif')]";
    String LOADER_DIV = "//div[@class='busyLoader']";
    String PAYMENT_VALUE_XPATH = "//td/font[text()='%s']";
    String TRANSACTIONS_DATES = "//table[@id='movementList_1_2']//td[@class='listItem' and contains(text(), '%s')]";

    @CustomAction
    default TransactionListPage checkDateOfTransactionIfPresent(String transactionDate) {
        IbEndpoint endpoint = getEndpoint(IbEndpoint.class);
        endpoint.isAtLeastOneElementPresent(String.format(TRANSACTIONS_DATES, transactionDate));
        return null;
    }

    @CheckElementPresent(FIRST_TRANSACTION_CELL)
    TransactionListPage checkUserHasSomeTransactions();

    @Click(FILTER_TRANSACTIONS_BUTTON)
    TransactionListPage clickFilterTransactions();

    @TypeInto(value = DATE_FROM, clear = true)
    TransactionListPage fillDateFrom(String actualDate);

    @Select(TRANSACTIONS_TYPES)
    TransactionListPage selectTransactionsTypes(String transactionTypes);

    @Click(value = DISPLAY_BUTTON, andWait = @Wait(value = LOADER_DIV, until = Until.GONE))
    TransactionListPage clickDisplayTransactions();

    @CheckElementPresent(value = PAYMENT_VALUE_XPATH, by = Lookup.XPATH, isStringDynamicXpath = true)
    TransactionListPage checkPaymentValue(String paymentValue);
}
