package cz.moneta.test.dsl.util.task.ufo.operations;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskDetail;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskList;
import cz.moneta.test.dsl.util.data.client.NonClient;

import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;

import static cz.moneta.test.dsl.util.DateFormatPatterns.CZ_DATE_FORMAT;
import static cz.moneta.test.dsl.util.DateUtils.getDateWithFormat;
import static cz.moneta.test.dsl.util.DateUtils.getTodayRawDate;

public class DocumentProcessTasks {

    public static final String CZ_COUNTRY = "Česká republika";
    public static final String DEF_OWNER = "Klient";
    public static final String DEFAULT_TEXT = "HARNESS SAID ITS OK";
    private final Harness harness;

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

    public Function<OPS001_S001_ClientSearchPageUfoOperations, UKO720_S001_TaskList> findTask(String birthday) {
        return page -> page.clickTasks()
                .fillBirthNumber(birthday)
                .clickDepartments()
                .clickFind();
    }

    public Function<UKO720_S001_TaskList, UKO720_S001_TaskDetail> selectTaskAndProcessAccountStatement(NonClient nonClient) {
        return page -> page
                .clickToFirstTask()
                .clickProcessFirstAttachment()
                .clickAccountStatement()
                .fillLastName(nonClient.lastName())
                .fillSurname(nonClient.firstName())
                .fillAccountNumber(String.valueOf(ThreadLocalRandom.current().nextInt(100000000, 999999999)))
                .selectOwnerBU(DEF_OWNER)
                .fillSenderIntake(nonClient.employer().getName())
                .fillFirstIncome(String.valueOf(nonClient.netMonthlyIncome()))
                .fillSecondIncome(String.valueOf(nonClient.netMonthlyIncome()))
                .fillThirdIncome(String.valueOf(nonClient.netMonthlyIncome()))
                .fillConfirmationDate(getDateWithFormat(getTodayRawDate(), CZ_DATE_FORMAT))
                .selectCountry(CZ_COUNTRY)
                .clickStamp()
                .clickSend()
                .clickNextSend()
                .clickLastSend()
                .closeWindow();
    }

    public Function<UKO720_S001_TaskList, UKO720_S001_TaskDetail> selectTaskAndConfirmationOfStatement(NonClient nonClient) {
        return page -> page
                .clickToFirstTask()
                .clickProcessFirstAttachment()
                .clickConfirmationOfIncome()
                .fillLastName(nonClient.lastName())
                .fillSurname(nonClient.firstName())
                .fillEmployedSince(getDateWithFormat(nonClient.occupationEmploymentFromRaw(), CZ_DATE_FORMAT))
                .selectEmployemntType(nonClient.occupationStatus().getUfoConfirmationOfReceiptValue())
                .fillEmploymentTillIfPresent(nonClient)
                .fillNetEarnings(String.valueOf(nonClient.netMonthlyIncome()))
                .fillInstalment(String.valueOf(nonClient.otherExpenses()))
                .fillWageDeductions(String.valueOf(nonClient.wageDeductions()))
                .fillDependingPeople(String.valueOf(nonClient.dependants()))
                .fillEmployerIco(nonClient.employer().getIco())
                .fillEmployerName(nonClient.employer().getName())
                .fillEmployerStreet(nonClient.employer().getAddress().getStreet())
                .fillDescriptionNumber(nonClient.employer().getAddress().getDescriptiveNumber())
                .fillOrientationNumber(nonClient.employer().getAddress().getOrientationNumber())
                .fillEmployerCity(nonClient.employer().getAddress().getCity())
                .fillEmployerPostCode(nonClient.residencyAddress().getZip())
                .fillIssueDate(getDateWithFormat(getTodayRawDate(), CZ_DATE_FORMAT))
                .clickStamp()
                .clickSignature()
                .clickSend()
                .clickSendAgree()
                .clickFinalAgree()
                .closeWindow();
    }

    public Function<UKO720_S001_TaskDetail, UKO720_S001_TaskList> finishDeputizeTask() {
        return page -> page
                .clickFinishProcess()
                .selectSolver(2)
                .fillCommentAndClickAgreeFinish(DEFAULT_TEXT);
    }
}
