package cz.moneta.test.dsl.ufo.cb.landing.proposal.pages;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ufo.cb.landing.proposal.pages.JAN452_S001_IndividualFinancialSheetsPage.FINANCIAL_SHEETS_TABLE;

@Wait(value = FINANCIAL_SHEETS_TABLE, until = Until.VISIBLE, waitSecondsForElement = 120)
public interface JAN452_S001_IndividualFinancialSheetsPage extends WebFlow<JAN452_S001_IndividualFinancialSheetsPage>, StoreAccessor {

    String FINANCIAL_SHEETS_COUNT_KEY = "FINANCIAL_SHEETS_COUNT";
    String FINANCIAL_SHEETS_TABLE = "JAN452_S001_G001_UfoTable";
    String FINANCIAL_SHEETS_TABLE_ROWS_XPATH = "//table[@id='JAN452_S001_G001_UfoTable']//tbody//tr";
    String OLDER_VERSIONS_RADIO = "JAN452_S001_H001_1";
    String FILTER_BUTTON = "JAN452_S001_B005";
    String LOADER_NOT_VISIBLE_IMG_XPATH = "//img[@id='XXX408_T001_X006' and not(contains(@style, 'display: none;'))]";
    String CREATE_FINANCIAL_SHEET_BUTTON = "JAN452_S001_B001";

    /**
     * Storing Financial Sheets count INCLUDING older versions, because when not included, new version overrides the old ones,
     * so the count does not change.
     */
    @CustomAction
    default JAN452_S001_IndividualFinancialSheetsPage storeFinancialSheetsCountOlderVersionsIncluded() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        int financialSheetsCount = endpoint.findAndGetListOfElements(FINANCIAL_SHEETS_TABLE_ROWS_XPATH, Lookup.XPATH).size();
        store(FINANCIAL_SHEETS_COUNT_KEY, String.valueOf(financialSheetsCount));
        return null;
    }

    @CustomAction
    default JAN452_S001_IndividualFinancialSheetsPage checkFinancialSheetCreatedSuccessfully() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        int financialSheetsCount = endpoint.findAndGetListOfElements(FINANCIAL_SHEETS_TABLE_ROWS_XPATH, Lookup.XPATH).size();
        Assertions.assertEquals(String.valueOf(financialSheetsCount - 1), get(FINANCIAL_SHEETS_COUNT_KEY));
        return null;
    }

    @Click(OLDER_VERSIONS_RADIO)
    JAN452_S001_IndividualFinancialSheetsPage clickOlderVersions();

    @Click(value = FILTER_BUTTON, andWait = @Wait(value = LOADER_NOT_VISIBLE_IMG_XPATH, by = Lookup.XPATH, until = Until.GONE))
    JAN452_S001_IndividualFinancialSheetsPage clickFilter();

    @Click(CREATE_FINANCIAL_SHEET_BUTTON)
    JAN459_S001_CreateFinancialSheetPage clickCreateFinancialSheet();

}
