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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.client.NewProductsByNameUfoOps;
import cz.moneta.test.dsl.ufo.operations.client.NewProductsUfoOps;
import cz.moneta.test.dsl.ufo.operations.client.STO702_S002_ApproveAlreadyReadPage;
import cz.moneta.test.dsl.ufo.operations.client.clientDetail.STS701_S002_ClientDetailPage;
import cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.contactForwarding.XCM701_S001_ContactForwardingPage;
import org.junit.jupiter.api.Assertions;

import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Function;

public class ClientDetailsTasks {

    private final Harness harness;

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

    public Function<STS701_S002_ClientDetailPage, STO702_S002_ApproveAlreadyReadPage> createNewProduct(NewProductsUfoOps newProduct) {
        return start -> start.selectNewProductForActivation(newProduct)
                .confirmAddProduct();
    }

    public Function<STS701_S002_ClientDetailPage, STO702_S002_ApproveAlreadyReadPage> createNewProductByName(NewProductsByNameUfoOps newProduct) {
        return start -> start.selectNewProductByNameForActivation(newProduct)
                .confirmAddProduct();
    }

    public Function<OPS001_S001_ClientSearchPageUfoOperations, XCM701_S001_ContactForwardingPage> stepsOnContactForwarding(String rcOfExistingClient) {
        return start -> start.then(harness.tasks().ufo().operations().searchClient().byRc(rcOfExistingClient))
                .clickContactForwarding();
    }

    public Function<STS701_S002_ClientDetailPage, STS701_S002_ClientDetailPage> assertFirstRequirementSolveDate() {
        return start -> start.menuClickClientRequirements()
                .clickRefreshRequirements()
                .assertFirstRequirementSolveDate(start.getActualDate());
    }

    public Function<STS701_S002_ClientDetailPage, STS701_S002_ClientDetailPage> assertFirstRequirementSolveTime() {
        return start -> {
            start.menuClickClientRequirements()
                    .clickRefreshRequirements();

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm:ss");
            LocalTime statusBarTime = LocalTime.parse(start.getStatusBarContent().split(" ")[0], formatter);
            LocalTime requirementSolveTime = LocalTime.parse(start.getFirstRequirementSolveDate().split(" ")[1], formatter);
            Assertions.assertTrue(Duration.between(statusBarTime, requirementSolveTime).getSeconds() <= 1,
                    "Time difference between status bar time and requirement solve time is greater than 1 second");
            return start;
        };
    }
}
