package cz.moneta.test.dataprepare.ufo.cb;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisAllObrazovkySME;
import cz.moneta.test.dsl.ufo.cb.landing.KOM401_S001_ProposalParameters;
import cz.moneta.test.dsl.ufo.cb.landing.proposals.Proposal;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks;
import cz.moneta.test.dsl.util.task.ufo.cb.ProposalTasks;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

@TestScenario(name = "Creates investment proposal.")
public class CreateInvestmentProposal {
    private Proposal proposal;
    private String text = StringUtils.repeat("TEST. ", 10);
    private HCisAllObrazovkySME[] pages = HCisAllObrazovkySME.values();

    private KOM401_S001_ProposalParameters accessProposal(Harness harness) {
        CommercialTasks tasks = harness.tasks().ufo().cb();
        return harness.withUfo()
                .openLoginPage()
                .then(tasks.loginAsCbBanker())
                .then(tasks.proposalTasks().proposalSignpost(proposal.getProposalId()));
    }

    private void editPages(Harness harness) {
        CommercialTasks tasks = harness.tasks().ufo().cb();
        Long proposalId = Long.parseLong(proposal.getProposalId());
        tasks.editPages(proposalId, pages, "1");
    }

    @BeforeAll
    public void setUp(Harness harness) {
        CommercialTasks tasks = harness.tasks().ufo().cb();
        ProposalTasks proposalTasks = tasks.proposalTasks();
        String ico = harness.getSystemOrConfigValue("ico");
        proposal = new Proposal();
        proposal.setAmount("20000000");
        proposal.setCharacterType("Splátkový");
        proposal.setProposalType("Plný");
        proposal.setLoanType("Investiční (Splátkový)");
        proposal.setProductType("34990 - INVESTIČNÍ ÚVĚR");
        proposal.setLoanPurpose("Dopravní prostředky - koupě");
        proposal.setSignDate(TestUtils.getTodayDate());
        proposal.setLastDrawingDate(proposalTasks.getValidDrawingDate());
        proposal.setSupport("Bez podpory");
        proposal.setInsurance("Ne");
        harness.withUfo()
                .openLoginPage()
                .then(tasks.loginAsCbBanker())
                .then(tasks.searchClient(ico))
                .then(proposalTasks.createProposal())
                .then(proposalTasks.fillAll(proposal))
                .clickSave()
                .clickContinueButton()
                .saveProposalId(proposal)
                .clickOkButton();
        Assertions.assertNotEquals(null, proposal.getProposalId(), "Proposal was not created, that means we cannot work with it.");
        harness.log("Proposal ID:" + proposal.getProposalId());
        editPages(harness);
    }

    @TestCase(name = "Check description screen in proposal.", environments = {Environment.TST1, Environment.PPE})
    public void checkDescriptionScreen(Harness harness) {
        String purpose = "Nová činnost";
        accessProposal(harness).clickDescription()
                .fillDescription(text)
                .fillFinancialResources(text)
                .selectLoanPurpose(purpose)
                .clickDoneButton()
                .clickSaveButton();
    }

    @TestCase(name = "Check basic information screen in proposal.", environments = {Environment.TST1, Environment.PPE})
    public void checkBasicInformationScreen(Harness harness) {
        accessProposal(harness).clickBasicInformation()
                .fillCompanyHistory(text)
                .fillCurrentState(text)
                .clickSaveButton()
                .clickDoneButton();
    }

}
