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

import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.cb.landing.proposal.pages.JAN459_S001_CreateFinancialSheetPage.PAGE_DIV;

@Wait(value = PAGE_DIV, until = Until.VISIBLE)
public interface JAN459_S001_CreateFinancialSheetPage extends WebFlow<JAN459_S001_CreateFinancialSheetPage>, StoreAccessor {

    String PAGE_DIV = "JAN459_S001_L025";
    String CLIENT_INPUT = "JAN459_S001_E001";
    String CLIENT_XPATH = "//input[@id='JAN459_S001_E001']/following-sibling::input";
    String CLIENT_DIV = "XXX408_T001_L052";
    String ICO_INPUT = "JAN459_S001_E002";
    String ICO_XPATH = "//input[@id='JAN459_S001_E002']/following-sibling::input";
    String ICO_DIV = "XXX408_T001_L091";
    String ESTABLISHMENT_DATE_INPUT = "JAN459_S001_E005";
    String ESTABLISHMENT_DATE_XPATH = "//input[@id='JAN459_S001_E005']/following-sibling::input";
    String CZ_NACE_1_SELECT = "JAN459_S001_C006";
    String MARKET_SHARE_SELECT = "JAN459_S001_C002";
    String CZ_NACE_PERCENTAGE_INPUT = "JAN459_S001_E023";
    String ACCOUNTING_PU_DOUBLY_RADIO = "JAN459_S001_R003_2";
    String UNITS_SELECT = "JAN459_S001_C001";
    String CURRENCY_SELECT = "JAN459_S001_C003";
    String NOT_SEASONAL_RADIO = "JAN459_S001_R002_2";
    String SHEETS_TO_DATE_INPUT = "JAN459_S001_E014";
    String MONTHS_IN_WATCHED_PERIOD_COUNT_INPUT = "JAN459_S001_E015";
    String SHEET_TYPE_SELECT = "JAN459_S001_C005";
    String AUDITOR_INPUT = "JAN459_S001_E016";
    String CERTIFICATE_NUMBER_INPUT = "JAN459_S001_E011";
    String AUDITORS_STATEMENT_SELECT = "JAN459_S001_C004";
    String SHEETS_IMAGE_SELECT = "JAN459_S001_C007";
    String SHEET_FORM_SELECT = "JAN459_S001_C008";
    String FILL_SHEET_BUTTON = "JAN459_S001_B001";
    String EXISTING_FINANCIAL_SHEET_CONTINUE_BUTTON = "JAN459_S001_B005";

    /**
     * Data iconsistency can cause problem during loading client, so then it is neccessary to fill it.
     *
     * @return
     */
    @CustomAction
    default JAN459_S001_CreateFinancialSheetPage fillClientIfEmpty() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        if (endpoint.getAttribute(CLIENT_XPATH, Lookup.XPATH, "value").isEmpty()) {
            String client = endpoint.getText(CLIENT_DIV).split(",")[0];
            endpoint.type(() -> CLIENT_INPUT, client, false);
        }
        return null;
    }

    /**
     * Data iconsistency can cause problem during loading ico, so then it is neccessary to fill it.
     *
     * @return
     */
    @CustomAction
    default JAN459_S001_CreateFinancialSheetPage fillICOIfEmpty() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        if (endpoint.getAttribute(ICO_XPATH, Lookup.XPATH, "value").isEmpty()) {
            String ico = endpoint.getText(ICO_DIV);
            endpoint.type(() -> ICO_INPUT, ico, false);
        }
        return null;
    }

    /**
     * Data iconsistency can cause problem during loading establishment date, so then it is neccessary to fill it.
     *
     * @return
     */
    @CustomAction
    default JAN459_S001_CreateFinancialSheetPage fillEstablishmentDateIfEmpty(String establishmentDate) {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        if (endpoint.getAttribute(ESTABLISHMENT_DATE_XPATH, Lookup.XPATH, "value").isEmpty()) {
            endpoint.type(() -> ESTABLISHMENT_DATE_INPUT, establishmentDate, false);
        }
        return null;
    }

    /**
     * Data iconsistency can cause problem during loading Cz Nace, so then it is neccessary to fill it.
     *
     * @return
     */
    @CustomAction
    default JAN459_S001_CreateFinancialSheetPage selectCzNace1IfEmpty(String czNace1) {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        if (endpoint.getSelectedFirstItemText(CZ_NACE_1_SELECT).equals("(není)")) {
            endpoint.selectByValue(CZ_NACE_1_SELECT, czNace1);
            endpoint.waitForElementsToLoad(2, Lookup.CLASSNAME, Until.GONE, SharedElements.LOAD_IMG_DIV_CLASS);
        }
        return null;
    }

    @TypeInto(value = CZ_NACE_PERCENTAGE_INPUT, clear = true)
    JAN459_S001_CreateFinancialSheetPage fillCzNacePercentage(String czNacePercentage);

    /**
     * Data iconsistency can cause problem during loading market share, so then it is neccessary to fill it.
     *
     * @return
     */
    @CustomAction
    default JAN459_S001_CreateFinancialSheetPage selectMarketShareIfEmpty(String marketShare) {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        if (endpoint.getSelectedFirstItemText(MARKET_SHARE_SELECT).equals("(není)")) {
            endpoint.selectByVisibleText(MARKET_SHARE_SELECT, marketShare);
        }
        return null;
    }

    @Click(ACCOUNTING_PU_DOUBLY_RADIO)
    JAN459_S001_CreateFinancialSheetPage clickAccountingPUDoubly();

    @Select(UNITS_SELECT)
    JAN459_S001_CreateFinancialSheetPage selectUnits(String units);

    @Select(CURRENCY_SELECT)
    JAN459_S001_CreateFinancialSheetPage selectCurrency(String currency);

    @Click(NOT_SEASONAL_RADIO)
    JAN459_S001_CreateFinancialSheetPage clickNotSeasonal();

    @TypeInto(SHEETS_TO_DATE_INPUT)
    JAN459_S001_CreateFinancialSheetPage fillSheetsToDate(String sheetsToDate);

    @TypeInto(MONTHS_IN_WATCHED_PERIOD_COUNT_INPUT)
    JAN459_S001_CreateFinancialSheetPage fillMonthsInWatchedPeriodCount(String monthsInWatchedPeriodCount);

    @Select(SHEET_TYPE_SELECT)
    JAN459_S001_CreateFinancialSheetPage selectSheetType(String sheetType);

    @TypeInto(AUDITOR_INPUT)
    JAN459_S001_CreateFinancialSheetPage fillAuditor(String auditor);

    @TypeInto(CERTIFICATE_NUMBER_INPUT)
    JAN459_S001_CreateFinancialSheetPage fillCertificateNuber(String certificateNumber);

    @Select(AUDITORS_STATEMENT_SELECT)
    JAN459_S001_CreateFinancialSheetPage selectAuditorsStatement(String auditorsStatement);

    @Select(value = SHEETS_IMAGE_SELECT, andWait = @Wait(SHEET_FORM_SELECT))
    JAN459_S001_CreateFinancialSheetPage selectSheetImage(String sheetImage);

    @Select(SHEET_FORM_SELECT)
    JAN459_S001_CreateFinancialSheetPage selectSheetForm(String sheetForm);

    @Click(FILL_SHEET_BUTTON)
    JAN459_S001_CreateFinancialSheetPage clickFillSheet();

    @CustomAction
    default JAN459_S001_CreateFinancialSheetPageTransitions clickExisitingFinancialSheetContinueIfVisible() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(3, EXISTING_FINANCIAL_SHEET_CONTINUE_BUTTON, Lookup.ID)) {
            endpoint.click(() -> EXISTING_FINANCIAL_SHEET_CONTINUE_BUTTON);
        }
        return null;
    }

}
