package cz.moneta.test.dataprepare.ml.hypos;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.hypos.enums.HyposProfile;
import cz.moneta.test.dsl.hypos.pages.cadaster.CadasterRedocDetailPage;
import cz.moneta.test.dsl.hypos.tasks.HyposTasks;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.Builders;
import cz.moneta.test.harness.support.web.Lookup;

import static cz.moneta.test.dsl.hypos.pages.cadaster.CadasterRedocDetailPage.FLAT_UNIT_DETAIL;

@TestScenario(name = "Process valuation")
public class ValuationFlatAtomPrepare {

    @TestCase(name = "Process valuation with job param - flat", environments = {Environment.TST1, Environment.PPE})
    public void tcValuationFlatAtom(Harness harness) {
        HyposTasks tasks = harness.tasks().hypos();
        //yamlData format "contractId lvId lvCode lvFlatNumber"
        String contractId = harness.getSystemOrConfigValue("yamlData").split(" ")[0];
        String lvId = harness.getSystemOrConfigValue("yamlData").split(" ")[1];
        String lvCode = harness.getSystemOrConfigValue("yamlData").split(" ")[2];
        String lvFlatNumber = harness.getSystemOrConfigValue("yamlData").split(" ")[3];

        harness.withHypos()
                .openLoginPage()
                .then(tasks.loginToHyposWithProfile(HyposProfile.EMERGENCY))
                .switchToRealtyPage()
                .then(tasks.valuationTasks().searchCadasterRedocDetail(lvCode,lvId));

        HyposEndpoint endpoint = harness.getEndpoint(HyposEndpoint.class);
        if (endpoint.isElementVisible(5, String.format(FLAT_UNIT_DETAIL, lvFlatNumber), Lookup.XPATH)) {
            Builders.newWebFlowBuilder(CadasterRedocDetailPage.class, endpoint, harness)
                    .clickFlatUnitDetail(lvFlatNumber)
                    .removeUnit(lvFlatNumber);
        }

        harness.withHypos()
                .openLoginPage()
                .then(tasks.loginToHyposWithProfile(HyposProfile.EMERGENCY))
                .then(tasks.searchAndOpenContract(contractId))
                .then(tasks.valuationTasks().createNewFlatUnit(lvCode,lvFlatNumber));

        harness.withHypos()
                .openLoginPage()
                .then(tasks.loginToHyposWithProfile(HyposProfile.EMERGENCY))
                .then(tasks.searchAndOpenContract(contractId))
                .clickRealty()
                .clickRealtyDetail(lvFlatNumber)
                .clickOnOrderValuation()
                .then(tasks.valuationTasks().doFlatMarketValuation());

        harness.withHypos()
                .openLoginPage()
                .then(tasks.loginToHyposWithProfile(HyposProfile.SUPERVISOR))
                .then(tasks.searchAndOpenContract(contractId))
                .clickRealty()
                .clickRealtyDetail(lvFlatNumber)
                .openFirstOrder()
                .then(tasks.valuationTasks().finishFlatOrder())
                .clickRealty()
                .clickRealtyDetail(lvFlatNumber)
                .clickOnOrderSupervision()
                .clickSubmitButton();
    }
}