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.BUILDING_DETAIL;
import static cz.moneta.test.dsl.hypos.pages.cadaster.CadasterRedocDetailPage.LAND_DETAIL;

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

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

        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(BUILDING_DETAIL, lvBuildingNumber), Lookup.XPATH)) {
            Builders.newWebFlowBuilder(CadasterRedocDetailPage.class, endpoint, harness)
                    .clickBuildingDetail(lvBuildingNumber)
                    .removeUnit(lvBuildingNumber);
        }

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

        if (endpoint.isElementVisible(5, String.format(LAND_DETAIL, lvLandNumber), Lookup.XPATH)) {
            Builders.newWebFlowBuilder(CadasterRedocDetailPage.class, endpoint, harness)
                    .clickLandDetail(lvLandNumber)
                    .removeUnit(lvLandNumber);
        }

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

        harness.withHypos()
                .openLoginPage()
                .then(tasks.loginToHyposWithProfile(HyposProfile.EMERGENCY))
                .then(tasks.searchAndOpenContract(contractId))
                .then(tasks.valuationTasks().addLandToUnit(lvId, lvCode, lvBuildingNumber, lvLandNumber));

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

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