package cz.moneta.test.regression.creditdistribution.loans;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.HousingLoanData;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.LoanData;
import cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP006_S001_ExpressScoringResultPage;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisAllLoanObject;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisBLRateFixation;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisPropertyOwnershipType;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisUcelUveruExpres;
import cz.moneta.test.dsl.ufo.banka.tasks.BankaTasks;
import cz.moneta.test.dsl.ufo.banka.tasks.expres.ExpresLoanDataPreparationTasks;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.NonConcurrentTestScenario;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;

import java.util.List;

import static cz.moneta.test.dsl.util.credentials.Keys.ufo.banka.banker;


@NonConcurrentTestScenario(name = "Housing loan (Expres Home) regression tests")
public class HousingLoanTests {
    private BankaTasks bankaTasks;
    private LoanData data;

    @BeforeEach
    public void setUp(Harness harness) {
        bankaTasks = harness.tasks().ufo().banka();
        data = bankaTasks.expresTasks().expressLoanDataPreparationTasks().preBuildLoanData();
        bankaTasks.expresTasks().expressLoanDataPreparationTasks().prepareClient(data.getClient());
    }

    @TestCase(name = "Cloan Expres Home RT1 - automat, papírem, střednědobý, vlastní, výstavba")
    @JiraTestCase(id = "CLOANS-T953", project = "Cloans")
    public void housingLoanTest_01_Construction(Harness harness, @Key(banker) Credentials credentials) {
        HousingLoanData hlData = data.getHousingLoanData();
        hlData.setRealityCustomAddress(false);
        hlData.setRealityType(HCisAllLoanObject.FAMILY_HOUSE);
        hlData.setOwnershipType(HCisPropertyOwnershipType.PERSONAL);
        hlData.setPurposeType(HCisUcelUveruExpres.CONSTRUCTION);
        hlData.setFixation(HCisBLRateFixation._5Y);
        data.setAmount(75000);
        data.setTerm(60);
        ExpresLoanDataPreparationTasks preparationTasks = bankaTasks.expresTasks().expressLoanDataPreparationTasks();
        data.setCoApplicants(List.of(preparationTasks.createCoApplicantData()));

        preparationTasks.prepareCoApplicants(data.getCoApplicants());

        NEP006_S001_ExpressScoringResultPage page = harness.withUfo().openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.expresTasks().housingLoanTasks().housingLoanE2ETask(data));

        Assertions.assertEquals("Úvěr načerpán", page.getAppStat());
    }

    @TestCase(name = "Cloan Expres Home RT2 - automat, papírem, krátkodobý, rekonstrukce rodinného domu")
    @JiraTestCase(id = "CLOANS-T954", project = "Cloans")
    public void housingLoanTest_02_Reconstruction(Harness harness, @Key(banker) Credentials credentials) {
        HousingLoanData hlData = data.getHousingLoanData();
        hlData.setRealityCustomAddress(false);
        hlData.setRealityType(HCisAllLoanObject.FAMILY_HOUSE);
        hlData.setOwnershipType(HCisPropertyOwnershipType.PERSONAL);
        hlData.setPurposeType(HCisUcelUveruExpres.RECONSTRUCTION);
        hlData.setFixation(HCisBLRateFixation._5Y);
        data.setAmount(55000);
        data.setTerm(12);
        ExpresLoanDataPreparationTasks preparationTasks = bankaTasks.expresTasks().expressLoanDataPreparationTasks();
        data.setCoApplicants(List.of(preparationTasks.createCoApplicantData(), preparationTasks.createSecondCoApplicantData()));

        preparationTasks.prepareCoApplicants(data.getCoApplicants());

        NEP006_S001_ExpressScoringResultPage page = harness.withUfo().openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.expresTasks().housingLoanTasks().housingLoanE2ETask(data));

        Assertions.assertEquals("Úvěr načerpán", page.getAppStat());
    }

    @TestCase(name = "Cloan Expres Home RT6 - automat, papírem, střednědobý, výstavba rodinného domu s energ. úsporami")
    @JiraTestCase(id = "CLOANS-T988", project = "Cloans")
    public void housingLoanTest_03_ConstructionWithEnergySavings(Harness harness, @Key(banker) Credentials credentials) {
        HousingLoanData hlData = data.getHousingLoanData();
        hlData.setRealityCustomAddress(false);
        hlData.setRealityType(HCisAllLoanObject.FAMILY_HOUSE);
        hlData.setOwnershipType(HCisPropertyOwnershipType.PERSONAL);
        hlData.setPurposeType(HCisUcelUveruExpres.CONSTRUCTION_WITH_ENERGY_SAVINGS);
        hlData.setFixation(HCisBLRateFixation._5Y);
        data.setAmount(700000);
        data.setTerm(300);

        NEP006_S001_ExpressScoringResultPage page = harness.withUfo().openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.expresTasks().housingLoanTasks().housingLoanE2ETask(data));

        Assertions.assertEquals("Úvěr načerpán", page.getAppStat());
    }

    @TestCase(name = "CCloan Expres Home RT7 - automat, papírem, střednědobý, rekonstrukce bytu s energ. úsporami")
    @JiraTestCase(id = "CLOANS-T989", project = "Cloans")
    public void housingLoanTest_04_ReconstructionWithEnergySavings(Harness harness, @Key(banker) Credentials credentials) {
        HousingLoanData hlData = data.getHousingLoanData();
        hlData.setRealityCustomAddress(false);
        hlData.setRealityType(HCisAllLoanObject.APARTMENT);
        hlData.setOwnershipType(HCisPropertyOwnershipType.PERSONAL);
        hlData.setPurposeType(HCisUcelUveruExpres.RECONSTRUCTION_WITH_ENERGY_SAVINGS);
        hlData.setFixation(HCisBLRateFixation._5Y);
        data.setInsurance(true);
        data.setInsuranceName("Poj. schopnosti splácet 2025 Home");
        data.setAmount(55000);
        data.setTerm(12);

        NEP006_S001_ExpressScoringResultPage page = harness.withUfo().openLoginPage()
                .then(bankaTasks.login().withCredentialsAndTurnOffSignpad(credentials))
                .then(bankaTasks.expresTasks().housingLoanTasks().housingLoanE2ETask(data));

        Assertions.assertEquals("Úvěr načerpán", page.getAppStat());
    }

    @AfterEach
    public void tearDown(Harness harness) {
        harness.store("reports.tmfj.execution.comment", bankaTasks.expresTasks().housingLoanLoggingTasks().getLogExpresHomeDataString(data));
    }
}
