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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.main.codelists.HCisAllObrazovkySME;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.harness.support.rest.RawRestRequest;

import java.util.Arrays;

import static cz.moneta.test.dsl.util.data.ihub.IHubConstants.GEBTHA6_SSO;

public class ComodaTasks {

    private final Harness harness;

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

    public Long setComodaData(String ico) {
        Long clientId = createAndGetClientId(ico);
        if (clientId > 0) {
            setEmployeeCount(clientId, Long.valueOf(1));
        }
        return clientId;
    }

    public Long createAndGetClientId(String ico) {
        RawRestRequest.Response unifiedClientResponse = harness.tasks().wso2().client().findUnifiedClient(ico);
        if (unifiedClientResponse.extract("PocetNalezenychZaznamu").intValue() == 1) {
            Long rsIdunifikovanehoklienta = unifiedClientResponse.extract("Data[0].IDUnifikovanehoKlienta").longValue();
            RawRestRequest.Response comodaResponse = createClientComoda(rsIdunifikovanehoklienta);
            return comodaResponse.extract("IDOsoba").longValue();
        }
        return Long.valueOf(-1);
    }

    public RawRestRequest.Response createClientComoda(Long uniPtKey) {
        return harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/sme.cluster/CRT_CLIENT_CMD/001")
                .addField("Business","GEM")
                .addField("DuvodDotazuDoODS", "1")
                .addField("UNIPT_KEY", uniPtKey)
                .post()
                .andAssertStatus(200);
    }

    public void setEmployeeCount(Long personId, Long empolyeeCount) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/sme.cluster/CMD/SET_PRSN_EMPLOYEE/001")
                .addField("ID_PRSN", personId)
                .addField("EMPLOYEE_COUNT", empolyeeCount)
                .addField("VALID_BY_USER_ID", GEBTHA6_SSO)
                .addField("VALID_DATE", DateUtils.convertRawDateForWso2(DateUtils.getTodayRawDate()))
                .post()
                .andAssertStatus(200);
    }

    public void editPage(Long idNavrh, HCisAllObrazovkySME page, String state) {
        harness.tasks().wso2().restRequest()
                .prepareEmptyRequestFromUfo("/t/sme.cluster/CMD/SET_SCREEN_STATUS/001")
                .addField("ID_Navrh", idNavrh)
                .addField("RS_DATA", Arrays.asList(new Object()))
                .addField("RS_DATA[0]", "Obrazovka", page.getValue())
                .addField("RS_DATA[0]", "Stav", state)
                .post()
                .andAssertStatus(200);
    }
}