package cz.moneta.test.dsl.util.task.brokerportal;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.task.brokerportal.BrokerPortalTasks;
import cz.moneta.test.harness.support.auth.Credentials;

public class BrokerPortalPreconditions {

    private final Harness harness;
    private final Credentials broker;
    public String contractId;

    public BrokerPortalPreconditions(Harness harness, Credentials broker) {
        this.harness = harness;
        this.broker = broker;
    }

    /**
     * Creating new contract used as precondition for broker portal tests.
     * @return String of contract id used for following tests.
     */
    public String createNewContract() {
        BrokerPortalTasks tasks = harness.tasks().brokerPortal();
        harness.withBrokerPortal()
                .openLoginPage()
                .then(tasks.loginTasks().loginWithCredentials(broker))
                .then(tasks.menuTasks().selectProductMortgage())
                .then(tasks.calculatorTasks().fillCalculator("1000000"))
                .then(tasks.calculatorTasks().calculateAndSaveCalculator())
                .clickToShortApplicationButton()
                .then(tasks.shortApplicationTasks().fillAndConfirmShortApplication())
                .then(tasks.shortApplicationTasks().checkShortApplicationSummary())
                .clickCreateApplicationButton()
                .then(tasks.shortApplicationTasks().signShortApplication())
                .clickToFullApplication()
                .then(tasks.fullApplicationTasks().fillAndSaveFullApplication())
                .then(tasks.fullApplicationTasks().signFullApplication());

        return harness.get("CONTRACT_ID");
    }

    /**
     * Close browser after precondition is finished.
     * Reason is, that precondition test should be independent from other tests -> every test has it's own browser session.
     */
    public void closeBrowser(){
        harness.withBrokerPortal().closeLoginPage();
    }
}
