package cz.moneta.test.dsl.newib.mainpage;

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.newib.mainpage.TermDepositPage.TERM_DEPOSIT_TITLE_XPATH;

@Wait(value = TERM_DEPOSIT_TITLE_XPATH, waitSecondsForElement = 30)
public interface TermDepositPage extends NewIbPageFlow<TermDepositPage> {

    String TERM_DEPOSIT_TITLE_XPATH = "//h1/span[text()='Termínovaný vklad']";
    String TERM_DEPOSIT_DOCUMENT_COMFIRM = "//div/strong[text()='Prohlašuji, že jsem se seznámil s těmito dokumenty']";
    String TERM_DEPOSIT_ACCOUNT_COMBO_BOX = "//button[@name='sourceAccountId']";
    String TERM_DEPOSIT_SELECT_ACCOUNT = "//div[contains(text(), 'Běžný účet %s')][1]";
    String TERM_DEPOSIT_AMOUNT = "//input[@data-testid='deposit']";
    String MINIMUM_AMOUNT_TEXT = "//div[starts-with(text(), 'Zadaná hodnota je menší než')]";
    String MAXIMUM_AMOUNT_TEXT = "//small[text()='Na vybraném účtu nemáte dostatek finančních prostředků']";
    String CHECK_TV = "//label[@for='termDepositType-%s']";
    String PERIOD_COMBO_BOX = "//button[@name='selectedPeriod']";
    String PERIOD_XPATH = "//div[contains(text(), '%s')]";
    String TAX_RESIDENCE_COMBO_BOX = "//button[@id='countryId.0_button']";
    String TAX_RESIDENCE_XPATH_TEMPLATE = "//div[text()='%s']";
    String TAX_ID_INPUT_XPATH1 = "//input[@data-testid='customer-dic-0']";
    String TAX_ID_INPUT_XPATH2 = "//input[@data-testid='customer-dic-1']";
    String NEXT_TAX_RESIDENCE_XPATH = "//button[@data-testid='add-tax-residence']";
    String TAX_RESIDENCE_COMBO_BOX_1 = "//button[@id='countryId.1_button']";
    String TAX_RESIDENCE_COMBO_BOX_2 = "//button[@id='countryId.2_button']";
    String TAX_RESIDENCE_COMBO_BOX_3 = "//button[@id='countryId.3_button']";
    String TAX_RESIDENCE_COMBO_BOX_4 = "//button[@id='countryId.4_button']";
    String TERM_DEPOSIT_CONFIRM_BUTTON = "//button[@data-testid='create-savings-term-deposit']";
    String CHECK_BOX_PRE_CONTRACT = "//input[@id='agreement']";
    String AGREE_PRE_CONTRACT = "//button[@data-testid='goNext']";
    String MOBILE_KEY_INPUT = "//label[text()='Mobilní klíč']/following::input[@id='mobileKey']";
    String SUBMIT_TERM_DEPOSIT = "//button[@data-testid='secondFactorSend']";
    String SUCCESSFULLY_NEW_TER_DEPOSIT_H2_XPATH = "//h2[text()='Hotovo! Vaši žádost jsme odeslali ke zpracování']";
    String BACK_TO_DASHBOARD = "//button[@data-testid='back-to-dashboard']";
    String INVALID_DIC_BUTTON = "//button[@data-testid='continueWithInvalidDIC']";
    String DECLINE_BUTTON = "//button[@data-testid='decline']";
    String CONFIRMATION_DISAGREEMENT_BUTTON = "//button[@data-testid='confirmDisagreement']";

    @Click(TERM_DEPOSIT_ACCOUNT_COMBO_BOX)
    TermDepositPage clickOnAccount();

    @Click(value = TERM_DEPOSIT_SELECT_ACCOUNT, by = Lookup.XPATH, isStringDynamicXpath = true)
    TermDepositPage selectAccount(String account);

    @TypeInto(TERM_DEPOSIT_AMOUNT)
    TermDepositPage setLimitAmountMin(String limitAmount);

    @CheckElementPresent(MINIMUM_AMOUNT_TEXT)
    TermDepositPage checkMinimumLimit();

    @TypeInto(value = TERM_DEPOSIT_AMOUNT, clear = true)
    TermDepositPage setLimitAmountMax(String limitAmount);

    @CheckElementPresent(MAXIMUM_AMOUNT_TEXT)
    TermDepositPage checkMaximumLimit();

    @TypeInto(value = TERM_DEPOSIT_AMOUNT, clear = true, andWait = @Wait(value = PERIOD_COMBO_BOX, until = Until.CLICKABLE))
    TermDepositPage setAmount(String amount);

    @Click(value = CHECK_TV, by = Lookup.XPATH, isStringDynamicXpath = true)
    TermDepositPage clickOnTV(String termDepositType);

    @Click(PERIOD_COMBO_BOX)
    TermDepositPage clickOnPeriod();

    @Click(value = PERIOD_XPATH, by = Lookup.XPATH, isStringDynamicXpath = true)
    TermDepositPage selectPeriod(String termDepositPeriod);

    @Click(TAX_RESIDENCE_COMBO_BOX)
    TermDepositPage clickOnTaxResidence();

    @Click(value = TAX_RESIDENCE_XPATH_TEMPLATE, by = Lookup.XPATH, isStringDynamicXpath = true)
    TermDepositPage selectTaxResidence(String taxResidence);

    @TypeInto(value = TAX_ID_INPUT_XPATH1, clear = true)
    TermDepositPage fillTaxId1(String taxId);

    @CustomAction
    default TermDepositPage handleTaxIdIfPresent1(String taxId) {
        if (getEndpoint(NewIbEndpoint.class).isElementPresent(TAX_ID_INPUT_XPATH1, Lookup.XPATH)) {
            return fillTaxId1(taxId);
        }
        return this;
    }

    @TypeInto(value = TAX_ID_INPUT_XPATH2, clear = true)
    TermDepositPage fillTaxId2(String taxId);

    @CustomAction
    default TermDepositPage handleTaxIdIfPresent2(String taxId) {
        if (getEndpoint(NewIbEndpoint.class).isElementPresent(TAX_ID_INPUT_XPATH2, Lookup.XPATH)) {
            return fillTaxId2(taxId);
        }
        return this;
    }

    @Click(NEXT_TAX_RESIDENCE_XPATH)
    TermDepositPage clickOnAddNextTaxResidence();

    @Click(TAX_RESIDENCE_COMBO_BOX_1)
    TermDepositPage clickOnNextTaxResidence1();

    @Click(TAX_RESIDENCE_COMBO_BOX_2)
    TermDepositPage clickOnNextTaxResidence2();

    @Click(TAX_RESIDENCE_COMBO_BOX_3)
    TermDepositPage clickOnNextTaxResidence3();

    @Click(TAX_RESIDENCE_COMBO_BOX_4)
    TermDepositPage clickOnNextTaxResidence4();

    @CheckElementPresent(NEXT_TAX_RESIDENCE_XPATH)
    TermDepositPage checkNextTaxResidenceButtonExist();

    @Click(value = TERM_DEPOSIT_CONFIRM_BUTTON, andWait = @Wait(value = TERM_DEPOSIT_DOCUMENT_COMFIRM, waitSecondsForElement = 60))
    TermDepositPage clickOnConfirmTV();

    @Click(TERM_DEPOSIT_CONFIRM_BUTTON)
    TermDepositPage clickOnConfirmTV1();

    @CustomAction
    default TermDepositPage scrollToBottom() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        ep.sendKeysOneAtATime(Key.DOWN);
        ep.sendKeysOneAtATime(Key.END);
        return null;
    }

    @Click(value = CHECK_BOX_PRE_CONTRACT, jsClick = true, andWait = @Wait(value = AGREE_PRE_CONTRACT, until = Until.CLICKABLE))
    TermDepositPage clickOnCheckBox();

    @Click(value = AGREE_PRE_CONTRACT, andWait = @Wait(value = MOBILE_KEY_INPUT, waitSecondsForElement = 10))
    TermDepositPage clickOnAgreePrecontract();

    @TypeInto(value = MOBILE_KEY_INPUT, andWait = @Wait(value = SUBMIT_TERM_DEPOSIT, waitSecondsForElement = 10))
    TermDepositPage fillMobKey(String defaultMobileKey);

    @Click(value = SUBMIT_TERM_DEPOSIT, andWait = @Wait(value = SUCCESSFULLY_NEW_TER_DEPOSIT_H2_XPATH, waitSecondsForElement = 60))
    TermDepositPage clickOnSubmit();

    @CheckElementPresent(SUCCESSFULLY_NEW_TER_DEPOSIT_H2_XPATH)
    TermDepositPage checkSuccessApplicationSent();

    @Click(BACK_TO_DASHBOARD)
    DashboardPage clickOnBackToDashboard();

    @Click(value = INVALID_DIC_BUTTON, andWait = @Wait(value = TERM_DEPOSIT_DOCUMENT_COMFIRM, waitSecondsForElement = 60))
    TermDepositPage confirmInvalidDic();

    @Click(DECLINE_BUTTON)
    TermDepositPage clickOnDecline();

    @Click(CONFIRMATION_DISAGREEMENT_BUTTON)
    DashboardPage clickOnConfirmDisagreement();

    @CustomAction
    default TermDepositPage checkNextTaxResidenceButtonDoesntExist() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        boolean isElementPresent = ep.isElementPresent(NEXT_TAX_RESIDENCE_XPATH, Lookup.XPATH);
        if (isElementPresent) {
            throw new AssertionError("Element NEXT_TAX_RESIDENCE_XPATH should not be present, but it is.");
        }
        return this;
    }
}