package cz.moneta.test.dsl.ufo.operations.client;

import cz.moneta.test.dsl.ufo.operations.shared.UfoOpsUtils;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.util.Level;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Wait;
import cz.moneta.test.harness.support.web.WebFlow;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ufo.operations.client.ClientEvaluationPage.PAGE_TITLE;

@Wait(value = PAGE_TITLE, by = Lookup.XPATH)
public interface ClientEvaluationPage extends WebFlow<ClientEvaluationPage>, StoreAccessor, ScoringDataVerticalMenu {
    String PAGE_TITLE = "//div[text()='Vyhodnocení Klient']";
    String DSTI_CELL_TEXT = "ČNB DSTI";
    String GRADE_STORE = "GRADE_STORE";

    int INCOME_SIBLING_INDEX = 1;
    int INTERNAL_PAYMENTS_SIBLING_INDEX = 3;
    int EXTERNAL_PAYMENTS_SIBLING_INDEX = 4;
    int MAX_INSTALMENT_SIBLING_INDEX = 6;
    int INDICATOR_SIBLING_INDEX = 7;

    int SCORE_SIBLING_INDEX = 1;
    int SCORECARD_SIBLING_INDEX = 2;
    int CUTOFF_SIBLING_INDEX = 3;
    int GRADE_SIBLING_INDEX = 6;

    @CustomAction
    default ClientEvaluationPage checkDstiValues(float dstiCoef) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);

        float income = UfoOpsUtils.getTableValueFloat(DSTI_CELL_TEXT, INCOME_SIBLING_INDEX, endpoint);
        float internalPayments = UfoOpsUtils.getTableValueFloat(DSTI_CELL_TEXT, INTERNAL_PAYMENTS_SIBLING_INDEX, endpoint);
        float externalPayments = UfoOpsUtils.getTableValueFloat(DSTI_CELL_TEXT, EXTERNAL_PAYMENTS_SIBLING_INDEX, endpoint);
        float maxInstalment = UfoOpsUtils.getTableValueFloat(DSTI_CELL_TEXT, MAX_INSTALMENT_SIBLING_INDEX, endpoint);

        float maxInstalmentCalc = income * dstiCoef - internalPayments - externalPayments;

        try {
            Assertions.assertEquals(maxInstalmentCalc, maxInstalment);
        } catch (AssertionError e) {
            log(Level.ERROR, "Max installment: " + e.getMessage());
        }
        return null;
    }

    @CustomAction
    default ClientEvaluationPage checkScorecardValues(boolean useApplicationScore, String expectedScorecard, int expectedCutoff) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String usedScorecard = UfoOpsUtils.getScoreTableValueString(useApplicationScore, SCORECARD_SIBLING_INDEX, endpoint);
        float actualCutoff = UfoOpsUtils.getScoreTableValueFloat(useApplicationScore, CUTOFF_SIBLING_INDEX, endpoint);

        try {
            Assertions.assertEquals(expectedScorecard, usedScorecard);
        } catch (AssertionError e) {
            log(Level.ERROR, "Scorecard: " + e.getMessage());
        }
        try {
            Assertions.assertEquals(expectedCutoff, actualCutoff);
        } catch (AssertionError e) {
            log(Level.ERROR, "Cutoff: " + e.getMessage());
        }
        return null;
    }

    @CustomAction
    default ClientEvaluationPage logClientGrade(boolean useApplicationScore) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String grade = UfoOpsUtils.getScoreTableValueString(useApplicationScore, GRADE_SIBLING_INDEX, endpoint);
        log("Client grade is {}", grade);
        store(GRADE_STORE, grade);
        return null;
    }
}