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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.exception.HarnessException;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import org.openqa.selenium.WebElement;

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

public interface InvestmentsQuestionnaire extends NewIbPageFlow<InvestmentsQuestionnaire> {
    String GO_FILL_QUESTIONNAIRE = "//button[@data-testid='start-questionnaire']";
    String ANSWERS = "//*[starts-with(@id, 'question-') and contains(@id, '_value-')]";
    String CONTINUE = "//button[@data-testid='questionnaire-v2-continue' and not(@disabled)]";
    String ERROR_MESSAGE = "//div[contains(@id, '_errorMessage') and string-length(text()) > 0]";
    List<String> ANSWERS_LIST = Arrays.asList(
            "question-1_value-1",
            "question-2_value-2",
            "question-3_value-3",
            "question-4_value-2",
            "question-5_value-1",
            "question-6_value-1_label",
            "question-6_value-2_label",
            "question-6_value-3_label",
            "question-7_value-1_label",
            "question-7_value-2_label",
            "question-7_value-3_label",
            "question-8_value-2_label",
            "question-8_value-3_label",
            "question-9_value-2",
            "question-10_value-1",
            "question-11_value-3",
            "question-12_value-4",
            "question-13_value-5",
            "question-14_value-2",
            "question-15_value-1",
            "question-16_value-1",
            "question-17_value-1_label");

    @Click(GO_FILL_QUESTIONNAIRE)
    InvestmentsQuestionnaire clickGoFill();

    @Click(CONTINUE)
    InvestmentsQuestionnaire clickContinue();

    @CustomAction
    default InvestmentsQuestionnaireResult fillQuestionnaire() {
        return fillQuestionnaire(new ArrayList<>());
    }

    private InvestmentsQuestionnaireResult fillQuestionnaire(List<String> clickedAnswers) {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        List<WebElement> elements = ep.findAndGetListOfElements(ANSWERS, Lookup.XPATH);
        if (elements.isEmpty()) {
            throw new HarnessException("There are no questions on the page");
        }
        for (WebElement element : elements) {
            String elementId = element.getAttribute("id");
            if (ANSWERS_LIST.contains(elementId)) {
                if (clickedAnswers.contains(elementId)) {
                    throw new HarnessException("Answer (id: " + elementId + ") was already clicked");
                }
                ep.moveToElement("//*[@id='" + elementId + "']");
                element.click();
                clickedAnswers.add(elementId);
            }
        }
        ep.click(() -> CONTINUE);
        if (ep.isElementVisible(2,ERROR_MESSAGE,Lookup.XPATH)) {
            throw new HarnessException("Error message displayed. There are probably more questions then expected");
        }
        else if (clickedAnswers.size() < ANSWERS_LIST.size()) {
            fillQuestionnaire(clickedAnswers);
        }

        return null;
    }
}