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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.brokerportal.components.Menu;
import cz.moneta.test.dsl.brokerportal.contract.ValuationPage;
import cz.moneta.test.dsl.brokerportal.contractdetail.ContractDetailPage;
import cz.moneta.test.dsl.brokerportal.valuationsdataprepare.FlatValuationData;
import cz.moneta.test.dsl.brokerportal.valuationsdataprepare.HouseValuationData;
import cz.moneta.test.dsl.util.data.YamlLoader;

import java.util.function.Function;

public class ValuationTasks {

    private final Harness harness;

    public ValuationTasks(Harness harness) {
        this.harness = harness;
    }

    /**
     * Find contract in contract menu by it's ID, created in BPPreparation class.
     *
     * @param contractId - String used for find specific contract created in precondition of test.
     */
    public Function<Menu, ContractDetailPage> findContract(String contractId) {
        return start -> start
                .disableNewVersion()
                .clickContracts()
                .selectBy("contractId")
                .insertContractId(contractId)
                .searchContracts()
                .openContract();
    }

    /**
     * Used for evaluate flat property. Using data from yaml file.
     * Method compare victory screen Id, if it is simillar, test is evaluated as pass.
     *
     * @param valuationType - Int used for parameterized test - Input should be only 0,1,2 or 3.
     * @return execution of method operations
     */
    public Function<ContractDetailPage, ValuationPage> evaluateFlat(int valuationType, String contractId) {
        YamlLoader yamlLoader = new YamlLoader("yamls/brokerPortal/flatValuations", FlatValuationData.class);
        FlatValuationData data = yamlLoader.parseObject();

        return start -> start
                .clickValuateProperty()
                .insertArea(data.propertyArea)
                .insertFlatUnit(data.flatUnit)
                .clickContinue()
                .clickConfirm()
                .insertAddress(data.address)
                .selectFlatType(data.valuationDetails.get(valuationType).type)
                .selectFlatPlacing(data.valuationDetails.get(valuationType).placing)
                .selectFlatDisposition(data.valuationDetails.get(valuationType).disposition)
                .selectFlatState(data.valuationDetails.get(valuationType).state)
                .insertFlatArea(data.valuationDetails.get(valuationType).area)
                .insertFlatAreaOther(data.valuationDetails.get(valuationType).areaOther)
                .insertFlatGarage(data.valuationDetails.get(valuationType).garage)
                .insertPrice(data.valuationDetails.get(valuationType).price)
                .insertFlatHouseType(data.valuationDetails.get(valuationType).houseType)
                .insertFlatHouseYear(data.valuationDetails.get(valuationType).houseYear)
                .insertFlatHouseFloors(data.valuationDetails.get(valuationType).houseFloors)
                .insertFlatHouseStatus(data.valuationDetails.get(valuationType).houseStatus)
                .insertUserRole(data.userRole)
                .insertUserFirstName(data.userFirstName)
                .insertUserLastName(data.userLastName)
                .insertUserPhone(data.userPhone)
                .clickContinue()
                .checkVictoryId(data.valuationDetails.get(valuationType).victoryScreen)
                .reportResults(data.valuationDetails.get(valuationType).valuation, contractId)
                ;
    }

    /**
     * Used for evaluate house property. Using data from yaml file.
     * Method compare victory screen Id, if it is simillar, test is evaluated as pass.
     *
     * @param valuationType - Int used for parameterized test - Input should be only 0 or 1.
     * @return execution of method operations
     */
    public Function<ContractDetailPage, ValuationPage> evaluateHouse(int valuationType, String contractId) {
        YamlLoader yamlLoader = new YamlLoader("yamls/brokerPortal/houseValuations", HouseValuationData.class);
        HouseValuationData data = yamlLoader.parseObject();

        return start -> start
                .clickValuateProperty()
                .clickHouse()
                .insertArea(data.propertyArea)
                .insertHouse(data.houseUnit)
                .clickContinue()
                .clickConfirm()
                .insertAddress(data.address)
                .selectHouseMaterial(data.valuationDetails.get(valuationType).material)
                .selectHouseYear(data.valuationDetails.get(valuationType).year)
                .selectHouseStatus(data.valuationDetails.get(valuationType).state)
                .selectHouseLandArea(data.valuationDetails.get(valuationType).landArea)
                .selectHouseGarageBasement(data.valuationDetails.get(valuationType).garageBasement)
                .clickNeedCalculateFalse()
                .insertHouseFloorArea(data.valuationDetails.get(valuationType).landArea)
                .insertPrice(data.valuationDetails.get(valuationType).price)
                .insertUserRole(data.userRole)
                .insertUserFirstName(data.userFirstName)
                .insertUserLastName(data.userLastName)
                .insertUserPhone(data.userPhone)
                .clickContinue()
                .checkVictoryId(data.valuationDetails.get(valuationType).victoryScreen)
                .reportResults(data.valuationDetails.get(valuationType).valuation, contractId)
                ;
    }
}
