package cz.moneta.test.dsl.hypos.contractPrepare.data;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.hypos.enums.HyposLogType;
import cz.moneta.test.dsl.hypos.tasks.HyposCommonTasks;
import cz.moneta.test.dsl.util.data.YamlLoader;
import cz.moneta.test.dsl.util.data.client.*;
import cz.moneta.test.dsl.util.data.hypos.HyposContract;
import cz.moneta.test.dsl.util.data.hypos.Person;
import cz.moneta.test.dsl.util.data.hypos.Realty;

import java.util.List;

public class ContractDataPrepare {

    private final HyposCommonTasks tasks;

    public ContractDataPrepare(Harness harness) {
        this.tasks = harness.tasks().hypos().hyposCommonTasks();
    }

    /**
     * Creating new contract for Hypos tests.
     *
     * @param hyposYamlData contains yaml file and optionally client data
     *      For example: "yaml birthNumber firstName lastName"
     *      For example: "yaml"
     * @return HyposContract with static data.
     */
    public HyposContract prepareContractData(String hyposYamlData, Harness harness) {
        String[] yamlDataParts = hyposYamlData.split(" ");
        Person person = null;
        if (yamlDataParts.length == 4) {
            person = Person.builder()
                    .birthNumber(yamlDataParts[1])
                    .firstName(yamlDataParts[2])
                    .lastName(yamlDataParts[3])
                    .build();
        }

        YamlLoader yamlLoader = new YamlLoader("yamls/hypos/hyposContract/" + yamlDataParts[0], HyposContractYamlData.class);
        HyposContractYamlData hyposContractYamlData = yamlLoader.parseObject();
        List<HyposClient> hyposClients = HyposClient.builder().build().createHyposClients(hyposContractYamlData, person, harness);
        Realty realty = Realty.builder().build().createRealty(hyposContractYamlData);
        hyposClients.forEach(hyposClient -> tasks.hyposLog(HyposLogType.CLIENT,
                String.format("%s %s", hyposClient.getNonClient().birthNumber(), hyposClient.getNonClient().fullName())));

        return HyposContract.builder().build().createHyposContract(hyposContractYamlData, hyposClients, realty);
    }
}