package cz.moneta.test.dsl.ib.mortgages;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.components.MainMenu;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.junit.Assert;

import static cz.moneta.test.dsl.ib.mortgages.MortgageCalculatorPage.*;

@Wait({MORTGAGE_CALCULATOR_TITLE_PAGE, MORTGAGES_CALCULATOR_INPUTS_PANEL, MORTGAGES_CALCULATOR_RESULT_PANEL})
public interface MortgageCalculatorPage extends IbPageFlow<MortgageCalculatorPage>, MainMenu {
    String MORTGAGE_CALCULATOR_TITLE_PAGE = "//p[text()='Hypoteční kalkulačka']";
    String MORTGAGES_CALCULATOR_INPUTS_PANEL = "//div[@class='flexi-mortgage-calculator-input']";
    String MORTGAGES_CALCULATOR_RESULT_PANEL = "//div[@class='flexi-mortgage-calculator-result']";

    //Calculator inputs panel
    String AMOUNT_OF_REALITY_INPUT = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//label[contains(text(), 'Cena nemovitosti')]/parent::div/following-sibling::div//input[@id='realtyAmount']";
    String MORTGAGE_AMOUNT_INPUT = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//label[contains(text(), 'Výše hypotéky')]/parent::div/following-sibling::div//input[@id='loanAmount']";
    String FREE_RESOURCES_INPUT = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//label[contains(text(), ' Volné úspory')]/parent::div/following-sibling::div//input[@id='freeResources']";
    String REPEYMENT_PERIOD = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//label[contains(text(), ' Doba splácení')]/parent::div/following-sibling::div//input[@id='maturity']";
    String FIXATION_LENGTH = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//label[contains(text(), 'Délka fixace')]/parent::div//div[@id='fixationWrapper']";
    String FIXATION_LENGTH_BUTTON = FIXATION_LENGTH + "/button[@fixation='3']";
    String INSURANCE_ABILITY_PAYMENT = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//div[contains(text(), 'Pojištění schopnosti splácet')]/following-sibling::div/span[@id='withInsuranceLabel' and text()='S pojištěním']/preceding-sibling::span[@id='withInsurance']";
    String WITHOUT_INSURANCE_ABILITY_PAYMENT = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//div[contains(text(), 'Pojištění schopnosti splácet')]/following-sibling::div/span[@id='withoutInsuranceLabel' and text()='Bez pojištění']/preceding-sibling::span[@id='withoutInsurance']";
    String LOAN_AMOUNT_SLIDER = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//div[@id='loanAmountSlider']";
    String MATURITY_SLIDER = MORTGAGES_CALCULATOR_INPUTS_PANEL + "//div[@id='maturitySlider']";

    String AVERAGE_MONTHLY_PAYMENT = MORTGAGES_CALCULATOR_RESULT_PANEL + "//div[contains(text(), 'Průměrná měsíční splátka')]/following-sibling::div/p[@id='monthlyInstalment']";
    String EFECTIVE_INTEREST_RATE = MORTGAGES_CALCULATOR_RESULT_PANEL + "//span[contains(text(), 'Efektivní úroková sazba')]";
    String RPSN_ELEMENT = MORTGAGES_CALCULATOR_RESULT_PANEL + "//span[contains(text(), 'RPSN')]";
    String I_AM_INTERESTED_BUTTON = MORTGAGES_CALCULATOR_RESULT_PANEL + "//div[contains(text(), 'MÁM ZÁJEM')]";
    String UNDEFINED = "undefined Kč";

    @CheckElementsPresent({
            @CheckElementPresent(AMOUNT_OF_REALITY_INPUT),
            @CheckElementPresent(MORTGAGE_AMOUNT_INPUT),
            @CheckElementPresent(FREE_RESOURCES_INPUT),
            @CheckElementPresent(REPEYMENT_PERIOD),
            @CheckElementPresent(FIXATION_LENGTH),
            @CheckElementPresent(LOAN_AMOUNT_SLIDER),
            @CheckElementPresent(INSURANCE_ABILITY_PAYMENT),
            @CheckElementPresent(WITHOUT_INSURANCE_ABILITY_PAYMENT),
            @CheckElementPresent(MATURITY_SLIDER),
    })
    MortgageCalculatorPage checkCalculatorInputsPanelElements();

    @CheckElementsPresent({
            @CheckElementPresent(AVERAGE_MONTHLY_PAYMENT),
            @CheckElementPresent(EFECTIVE_INTEREST_RATE),
            @CheckElementPresent(RPSN_ELEMENT),
            @CheckElementPresent(I_AM_INTERESTED_BUTTON)
    })
    MortgageCalculatorPage checkCalculatorRsultPanelElements();

    @CustomAction
    default MortgageCalculatorPage getMonthlyPaymentAndCheckNotUndefinedValue() {
        String monthlyPayment = getMonthlyPayment();
        Assert.assertNotEquals(UNDEFINED, monthlyPayment);
        return null;
    }

    @GetElementContent(AVERAGE_MONTHLY_PAYMENT)
    String getMonthlyPayment();

    @CustomAction
    default MortgageCalculatorPage changeMortgageAmountAndCheckChangeValueMonthlyPayment(String amountOfReality) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        String monthlyPaymentBeforeChange = getMonthlyPayment();
        ibEndpoint.type(() -> MORTGAGE_AMOUNT_INPUT, amountOfReality, true);
        ibEndpoint.sleepSeconds(5);
        String monthlyPaymentAfterChange = getMonthlyPayment();
        Assert.assertFalse("Monthly payment before change mortgages amount is " + monthlyPaymentBeforeChange + " and monthly payment after change is " + monthlyPaymentAfterChange, (monthlyPaymentBeforeChange.contains(monthlyPaymentAfterChange)) || (monthlyPaymentAfterChange.contains(UNDEFINED)));
        return null;
    }

    @CustomAction
    default MortgageCalculatorPage changeFreeResourcesAndCheckChangeValueMonthlyPayment(String freeResources) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        String monthlyPaymentBeforeChange = getMonthlyPayment();
        ibEndpoint.type(() -> FREE_RESOURCES_INPUT, freeResources, true);
        ibEndpoint.sleepSeconds(5);
        String monthlyPaymentAfterChange = getMonthlyPayment();
        Assert.assertFalse("Monthly payment before change free resources is " + monthlyPaymentBeforeChange + " and monthly payment after change is " + monthlyPaymentAfterChange, (monthlyPaymentBeforeChange.contains(monthlyPaymentAfterChange)) || (monthlyPaymentAfterChange.contains(UNDEFINED)));
        return null;
    }


    @CustomAction
    default MortgageCalculatorPage changeFixationLenghtAndCheckChangeValueMonthlyPayment() {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        String monthlyPaymentBeforeChange = getMonthlyPayment();
        ibEndpoint.click(() -> FIXATION_LENGTH_BUTTON);
        ibEndpoint.sleepSeconds(5);
        String monthlyPaymentAfterChange = getMonthlyPayment();
        Assert.assertFalse("Monthly payment before change period of fixation is " + monthlyPaymentBeforeChange + " and monthly payment after change is " + monthlyPaymentAfterChange, (monthlyPaymentBeforeChange.contains(monthlyPaymentAfterChange)) || (monthlyPaymentAfterChange.contains(UNDEFINED)));
        return null;
    }

    @CustomAction
    default MortgageCalculatorPage insuranceAbilityPaymentAndCheckChangeValueMonthlyPayment() {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        String monthlyPaymentBeforeChange = getMonthlyPayment();
        ibEndpoint.click(() -> INSURANCE_ABILITY_PAYMENT);
        ibEndpoint.sleepSeconds(7);
        String monthlyPaymentAfterChange = getMonthlyPayment();
        Assert.assertFalse("Monthly payment without insurance is " + monthlyPaymentBeforeChange + " and monthly payment after change is " + monthlyPaymentAfterChange, (monthlyPaymentBeforeChange.contains(monthlyPaymentAfterChange)) || (monthlyPaymentAfterChange.contains(UNDEFINED)));
        return null;
    }

    @Click(I_AM_INTERESTED_BUTTON)
    MortgagesCalculatorThankPage clickIAmInterestedButton();
}
