package cz.moneta.test.dsl.ufo.banka.pages.search;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.UfoBankaPageFlow;
import cz.moneta.test.dsl.ufo.banka.pages.loans.VER013_S001_SecondSignatureSearchPage;
import cz.moneta.test.dsl.ufo.banka.pages.loans.VER018_S001_SecondBankerVerificationConfirmPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.PKU001_S002_CreatePaymentOrderPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.UHK002_S001_CreateBankBookPayment;
import cz.moneta.test.dsl.ufo.banka.pages.menu.vertical.VerticalMenu;
import cz.moneta.test.dsl.ufo.banka.pages.po.ZAS003_S001_StatutoryAuthorityEvidencePage;
import cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL002_S003_ListOfSmoothPaymentsForVerificationPage;
import cz.moneta.test.dsl.ufo.common.landing.ApplicationSelectPage;
import cz.moneta.test.dsl.util.BankaUtils;
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.banka.pages.search.HLO001_S001_ClientSearchPage.HLO001_S001_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage.SEARCH_BY_RC_BUTTON;
import static cz.moneta.test.dsl.ufo.banka.pages.search.HLO001_S001_ClientSearchPage.SEARCH_TERM_INPUT;

@Wait(value = HLO001_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 120)
@Wait(value = {SEARCH_TERM_INPUT, SEARCH_BY_RC_BUTTON}, waitSecondsForElement = 120)
@Wait(explicitWaitSeconds = 1)
public interface HLO001_S001_ClientSearchPage extends UfoBankaPageFlow<HLO001_S001_ClientSearchPage>, VerticalMenu, StoreAccessor {
    String HLO001_S001_PAGE_LABEL_XPATH = "//div[text()='HLO001_S001']";
    String SEARCH_TERM_INPUT = "HLO001_S001_E001";
    String SEARCH_BY_PRODUCT_BUTTON = "HLO001_S001_B002";
    String SEARCH_BY_CIF_BUTTON = "HLO001_S001_B005";
    String SEARCH_BY_RC_BUTTON = "HLO001_S001_B006";
    String SEARCH_BY_RC_RADIO_XPATH = "//td[text()='Rodné číslo']";
    String SEARCH_BY_ICO_RADIO = "HLO001_S001_G001-3";
    String SEARCH_BY_CARD_RADIO = "HLO001_S001_G001-6";
    String CLIENT_BUTTON = "HLO001_S001_N010";
    String PRODUCT_BUTTON = "HLO001_S001_N011";
    String SKIP_PAGE_BUTTON = "ZAS003_S001_B004";
    String CREATE_PAYMENT_ORDER = "HLO001_S001_S008";
    String CALCULATORS_BUTTON = "HLO001_S001_S016";
    String LOGIN_MENU_BUTTON = "HLO001_S001_N024";
    String SECOND_SIGNATURE_BUTTON = "XXX100_T001_B012";
    String BANKER_NAME_LABEL_XPATH = "//div[@id='XXX100_T001_L001']/b";
    String BANKER_NAME = "BANKER_NAME";
    String BANK_BOOK_BUTTON = "HLO001_S001_I043";
    String BILLING_ON_BANK_BOOK_BUTTON = "HLO001_S001_I041";
    String HISTORY_SEARCHED_CLIENTS_TABLE = "HLO001_S001_FB001";
    String CLOSE_HISTORY_BUTTON = "HLO001_S001_X043";
    String VERIFICATION_BUTTON = "HLO001_S001_N055";
    String VERIFICATION_OF_LOAN_BUTTON = "HLO001_S001_I059";
    String SMOOTH_PAYMENTS_BUTTON = "HLO001_S001_S018";

    @TypeInto(SEARCH_TERM_INPUT)
    HLO001_S001_ClientSearchPage fillSearchTerm(String term);

    @Click(value = SEARCH_BY_PRODUCT_BUTTON, jsClick = true)
    HLO001_S001_ClientSearchPageTransition clickSearchByProduct();

    @Click(SEARCH_BY_RC_BUTTON)
    HLO001_S001_ClientSearchPageTransition clickSearchByRc();

    @Click(SEARCH_BY_CIF_BUTTON)
    HLO001_S001_ClientSearchPageTransition clickSearchByCif();

    @CustomAction
    default HKO101_S001_MainClientPage clickIcoRadioAndContinueToMainClientPageAndCloseAlertIfVisible() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> SEARCH_BY_ICO_RADIO);
        endpoint.click(() -> CLIENT_BUTTON);
        if (endpoint.isAlertPresent(3)) {
            endpoint.dismissAlert(1);
        }
        return null;
    }

    @Click(SEARCH_BY_ICO_RADIO)
    @Click(CLIENT_BUTTON)
    HLO001_S001_ClientSearchPageTransition searchByIco();

    @Click(SEARCH_BY_CARD_RADIO)
    @Click(PRODUCT_BUTTON)
    HLO001_S001_ClientSearchPageTransition searchByCard();

    @Click(SEARCH_BY_ICO_RADIO)
    @Click(CLIENT_BUTTON)
    @Click(SKIP_PAGE_BUTTON)
    HLO001_S001_ClientSearchPageTransition searchByLegalEntityIco();

    @Click(CALCULATORS_BUTTON)
    KAL001_S001_CalculatorsPage clickCalculators();

    @Click(LOGIN_MENU_BUTTON)
    ApplicationSelectPage clickLoginMenu();

    @Click(CLIENT_BUTTON)
    HLO001_S001_ClientSearchPageTransition clickSearchClient();

    @CustomAction
    default VER013_S001_SecondSignatureSearchPage clickSecondSignature() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class, Harness.getSecondBankerToken());
        endpoint.click(() -> SECOND_SIGNATURE_BUTTON);
        endpoint.focusToNewPopup(20);
        return null;
    }

    @Click(value = VERIFICATION_BUTTON, andWait = @Wait(value = VERIFICATION_OF_LOAN_BUTTON, until = Until.VISIBLE))
    @Click(VERIFICATION_OF_LOAN_BUTTON)
    VER018_S001_SecondBankerVerificationConfirmPage clickVerificationLoan();

    @Click(value = VERIFICATION_BUTTON, andWait = @Wait(value = SMOOTH_PAYMENTS_BUTTON, until = Until.VISIBLE))
    @Click(SMOOTH_PAYMENTS_BUTTON)
    ZPL002_S003_ListOfSmoothPaymentsForVerificationPage clickVerificationSmoothPayments();

    @StoreElementContent(storeKey = BANKER_NAME, value = BANKER_NAME_LABEL_XPATH, by = Lookup.XPATH)
    HLO001_S001_ClientSearchPage storeBankerName();

    @CustomAction
    default HLO001_S001_ClientSearchPage globalStoreBankerName(Harness harness) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        harness.storeGlobal(BANKER_NAME, BankaUtils.formatSecondBankerName(endpoint.getText(BANKER_NAME_LABEL_XPATH, Lookup.XPATH)));
        return null;
    }

    @CheckElementPresent(HLO001_S001_PAGE_LABEL_XPATH)
    HLO001_S001_ClientSearchPage assertPageId();

    @Click(SEARCH_BY_ICO_RADIO)
    @Click(CLIENT_BUTTON)
    ZAS003_S001_StatutoryAuthorityEvidencePage clickIcoRadioAndContinueToStatutoryAuthorityEvidencePage();

    @Click(CREATE_PAYMENT_ORDER)
    PKU001_S002_CreatePaymentOrderPage clickOnCreatePaymentOrder();

    @Click(value = BANK_BOOK_BUTTON, andWait = @Wait(value = BILLING_ON_BANK_BOOK_BUTTON, until = Until.VISIBLE))
    @Click(BILLING_ON_BANK_BOOK_BUTTON)
    UHK002_S001_CreateBankBookPayment clickOnBillingOnBankBook();

    @CustomAction
    default HLO001_S001_ClientSearchPage closeSearchedHistoryIfVisible() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(3, HISTORY_SEARCHED_CLIENTS_TABLE, Lookup.ID)) {
            endpoint.click(() -> CLOSE_HISTORY_BUTTON);
        }
        return null;
    }
}
