package cz.moneta.test.dsl.ib.loans.consolidationloans;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.components.MainMenu;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.openqa.selenium.WebElement;

import java.util.Arrays;
import java.util.List;

import static cz.moneta.test.dsl.ib.loans.consolidationloans.ConsolidationCalculatorPage.*;

@Wait({CALCULATOR_PANEL_PARENT, TITLE_PAGE, LOANS_CALCULATOR_FIELDS, CARD_CALCULATOR_FIELDS, OVERDRAFT_CALCULATOR_FIELDS, RESULT_FIELD})
public interface ConsolidationCalculatorPage extends IbPageFlow<ConsolidationCalculatorPage>, MainMenu, StoreAccessor {
    //Identification PAGE elements
    String CALCULATOR_PANEL_PARENT = "//div[@id = 'calculatorPanel']";
    String TITLE_PAGE = CALCULATOR_PANEL_PARENT + "//td[text()='Spočítejte si snížení Vaší splátky při konsolidaci půjček z jiných bank.']";
    String CONTINUE_BUTTON = CALCULATOR_PANEL_PARENT + "//div[@id='next-step-btn']";

    //Fields for requirment to loans consolidation
    String LOANS_CALCULATOR_FIELDS = CALCULATOR_PANEL_PARENT + "//div[@id='loan-calculator']";
    String ADD_ANOTHER_LOAN_BUTTON = CALCULATOR_PANEL_PARENT + "//span[text()='Úvěr']//ancestor::div[@id='loan-calculator']/descendant::div[@id='loan-add-btn']";
    String LOAN_REMAINING_INPUTS = CALCULATOR_PANEL_PARENT + "//label[contains(text(),'Zbývá mi doplatit')]//following-sibling::input[@class='loan-remaining']";
    String LOAN_PAYMENT_INPUTS = LOANS_CALCULATOR_FIELDS + "//label[contains(text(),'Měsíčně splácím')]//following-sibling::input[@class='loan-payment']";
    String LOAN_PROVIDER_SELECTORS_XPATH = LOANS_CALCULATOR_FIELDS + "//label[contains(text(),'Poskytovatel')]//following-sibling::select";
    String FIRST_LOAN_PROVIDER_SELECTORS = LOAN_PROVIDER_SELECTORS_XPATH + "[@name='loanProvider1']";
    String SECOND_LOAN_PROVIDER_SELECTORS = LOAN_PROVIDER_SELECTORS_XPATH + "[@name='loanProvider2']";
    String THIRD_LOAN_PROVIDER_SELECTORS = LOAN_PROVIDER_SELECTORS_XPATH + "[@name='loanProvider3']";
    String LOAN_REMOVE_BUTTON = CALCULATOR_PANEL_PARENT + "//div[@id='loan%s']//span[@class='remove-btn']/a[text()='Odebrat']";

    //Fields for requirment to loans consolidation
    String CARD_CALCULATOR_FIELDS = CALCULATOR_PANEL_PARENT + "//div[@id='card-calculator']";
    String ADD_CREDIT_CARD_BUTTON = CALCULATOR_PANEL_PARENT + "//span[text()='Kreditní karta']//ancestor::div[@id='card-calculator']/descendant::div[@id='card-add-btn']";
    String CARD_REMAINING_INPUTS = CALCULATOR_PANEL_PARENT + "//label[contains(text(),'Mohu čerpat až')]//following-sibling::input[@class='card-payment']";
    String CARD_PROVIDER_SELECTORS_XPATH = CARD_CALCULATOR_FIELDS + "//label[contains(text(),'Poskytovatel')]//following-sibling::select";
    String FIRST_CREDIT_CARD_PROVIDER_SELECTORS = CARD_PROVIDER_SELECTORS_XPATH + "[@name='cardProvider1']";
    String SECOND_CREDIT_CARD_PROVIDER_SELECTORS = CARD_PROVIDER_SELECTORS_XPATH + "[@name='cardProvider2']";
    String THIRD_CREDIT_CARD_PROVIDER_SELECTORS = CARD_PROVIDER_SELECTORS_XPATH + "[@name='cardProvider3']";
    String CARD_REMOVE_BUTTON = CARD_CALCULATOR_FIELDS + "//div[@id='card%s']//span[@class='remove-btn-noloan']/a[text()='Odebrat']";

    //Fields for requirment to loans consolidation
    String OVERDRAFT_CALCULATOR_FIELDS = CALCULATOR_PANEL_PARENT + "//div[@id='overdraft-calculator']";
    String ADD_OVERDRAFT_BUTTON = CALCULATOR_PANEL_PARENT + "//span[text()='Kontokorent']//ancestor::div[@id='overdraft-calculator']/descendant::div[@id='overdraft-add-btn']";
    String OVERDRAFT_PAYMENT_INPUTS = CALCULATOR_PANEL_PARENT + "//label[contains(text(),'Mohu čerpat až')]//following-sibling::input[@class='overdraft-payment']";
    String OVERDRAFT_PROVIDER_SELECTORS_XPATH = OVERDRAFT_CALCULATOR_FIELDS + "//label[contains(text(),'Poskytovatel')]//following-sibling::select";
    String FIRST_OVERDRAFT_PROVIDER_SELECTORS = OVERDRAFT_PROVIDER_SELECTORS_XPATH + "[@name='overdraftProvider1']";
    String SECOND_OVERDRAFT_PROVIDER_SELECTORS = OVERDRAFT_PROVIDER_SELECTORS_XPATH + "[@name='overdraftProvider2']";
    String THIRD_OVERDRAFT_PROVIDER_SELECTORS = OVERDRAFT_PROVIDER_SELECTORS_XPATH + "[@name='overdraftProvider3']";
    String OVERDRAFT_REMOVE_BUTTON = OVERDRAFT_CALCULATOR_FIELDS + "//div[@id='overdraft%s']//span[@class='remove-btn-noloan']/a[text()='Odebrat']";

    //Result field
    String RESULT_FIELD = CALCULATOR_PANEL_PARENT + "//div[@id='result-fields']";
    String TOTAL_MONTHLY_PAYMENT_SUM = RESULT_FIELD + "//label[contains(text(), 'Celkem měsíčně splácíte:')]/following-sibling::span[@id='total-monthly-payment-sum']";
    String TOTAL_CONSOLIDATION_PAYMENT_SUM = RESULT_FIELD + "//label[contains(text(), 'Celkem konsolidujete:')]/following-sibling::span[@id='total-consolidation-sum']";
    String TOTAL_MONTHLY_PAYMENT_KEY = "TotalMonthlyPayment";
    String TOTAL_CONSOLIDATION_PAYMENT_KEY = "TotalConsolidationPayment";

    @Clicks({
            @Click(ADD_ANOTHER_LOAN_BUTTON),
            @Click(ADD_ANOTHER_LOAN_BUTTON)})
    ConsolidationCalculatorPage twoTimesClickToAddAnotherLoanButton();

    @CustomAction
    default ConsolidationCalculatorPage fillInInputsLoanFields(String loanRemainingFirst, String loanRemainingSecond, String loanRemainingThird, String loanPaymentFirst, String loanPaymentSecond, String loanPaymentThird) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        List<String> loanRemainingValues = Arrays.asList(loanRemainingFirst, loanRemainingSecond, loanRemainingThird);
        List<String> loanPaymentValues = Arrays.asList(loanPaymentFirst, loanPaymentSecond, loanPaymentThird);
        List<WebElement> remainingInputs = ibEndpoint.findAndGetListOfElements(LOAN_REMAINING_INPUTS, Lookup.XPATH);
        List<WebElement> allPaymentInputs = ibEndpoint.findAndGetListOfElements(LOAN_PAYMENT_INPUTS, Lookup.XPATH);

        for (int i = 0; i < remainingInputs.size(); i++) {
            remainingInputs.get(i).sendKeys(loanRemainingValues.get(i));
            allPaymentInputs.get(i).sendKeys(loanPaymentValues.get(i));
        }
        return null;
    }

    @Selects({
            @Select(FIRST_LOAN_PROVIDER_SELECTORS),
            @Select(SECOND_LOAN_PROVIDER_SELECTORS),
            @Select(THIRD_LOAN_PROVIDER_SELECTORS)})
    ConsolidationCalculatorPage selectProvidersInLoanField(String providerInFirstLoans, String providerInSecondLoans, String providerInThirdLoans);

    @Click(value = LOAN_REMOVE_BUTTON, isStringDynamicXpath = true)
    ConsolidationCalculatorPage clickToRemoveButtonInLoanField(String numberFieldOfLoanToRemove);

    @CustomAction
    default ConsolidationCalculatorPage fillInInputsCardFields(String cardPaymentFirst, String cardPaymentSecond, String cardPaymentThird) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        List<String> cardPaymentValues = Arrays.asList(cardPaymentFirst, cardPaymentSecond, cardPaymentThird);
        List<WebElement> cardInputs = ibEndpoint.findAndGetListOfElements(CARD_REMAINING_INPUTS, Lookup.XPATH);

        for (int i = 0; i < cardInputs.size(); i++) {
            cardInputs.get(i).sendKeys(cardPaymentValues.get(i));
        }
        return null;
    }

    @Clicks({
            @Click(ADD_CREDIT_CARD_BUTTON),
            @Click(ADD_CREDIT_CARD_BUTTON),
            @Click(ADD_CREDIT_CARD_BUTTON)})
    ConsolidationCalculatorPage threeTimesClickToAddCreditCardButton();

    @Selects({
            @Select(FIRST_CREDIT_CARD_PROVIDER_SELECTORS),
            @Select(SECOND_CREDIT_CARD_PROVIDER_SELECTORS),
            @Select(THIRD_CREDIT_CARD_PROVIDER_SELECTORS)})
    ConsolidationCalculatorPage selectProvidersInCardField(String providerInFirstCard, String providerInSecondCard, String providerInThirdCard);

    @Click(value = CARD_REMOVE_BUTTON, isStringDynamicXpath = true)
    ConsolidationCalculatorPage clickToRemoveButtonInCardField(String numberFieldOfCardToRemove);

    @CustomAction
    default ConsolidationCalculatorPage fillInInputsOverdraftFields(String overdraftPaymentFirst, String overdraftPaymentSecond, String overdraftPaymentThird) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        List<String> paymentOverdraftValues = Arrays.asList(overdraftPaymentFirst, overdraftPaymentSecond, overdraftPaymentThird);
        List<WebElement> overdraftInputs = ibEndpoint.findAndGetListOfElements(OVERDRAFT_PAYMENT_INPUTS, Lookup.XPATH);

        for (int i = 0; i < overdraftInputs.size(); i++) {
            overdraftInputs.get(i).sendKeys(paymentOverdraftValues.get(i));
        }
        return null;
    }

    @Clicks({
            @Click(ADD_OVERDRAFT_BUTTON),
            @Click(ADD_OVERDRAFT_BUTTON),
            @Click(ADD_OVERDRAFT_BUTTON)})
    ConsolidationCalculatorPage threeTimesClickToAddOverdraftButton();

    @Selects({
            @Select(FIRST_OVERDRAFT_PROVIDER_SELECTORS),
            @Select(SECOND_OVERDRAFT_PROVIDER_SELECTORS),
            @Select(THIRD_OVERDRAFT_PROVIDER_SELECTORS)})
    ConsolidationCalculatorPage selectProvidersInOverdraftField(String providerInFirstOverdraft, String providerInSecondOverdraft, String providerInThirdOverdraft);

    @Click(value = OVERDRAFT_REMOVE_BUTTON, isStringDynamicXpath = true)
    ConsolidationCalculatorPage clickToRemoveButtonInOverdraftField(String numberFieldOfOverdraftToRemove);

    @StoreElementContents({
            @StoreElementContent(storeKey = "TotalMonthlyPayment", value = TOTAL_MONTHLY_PAYMENT_SUM),
            @StoreElementContent(storeKey = "TotalConsolidationPayment", value = TOTAL_CONSOLIDATION_PAYMENT_SUM)})
    ConsolidationCalculatorPage getTotalMonthlyPaymentAndConsolidationPaymentValues();

    @Click(CONTINUE_BUTTON)
    YouRequestToMergeLoansPage clickToContinueButton();
}
