package cz.moneta.test.utils.cards;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@TestScenario(name = "Broadcom monitoring")
public class BroadcomMonitor {

    @TestCase(name = "Broadcom monitoring", environments = Environment.LIVE)
    public void broadcomMonitoring(Harness harness, @Key("cards.broadcom") Credentials credentials) {

        LocalDateTime from = harness.tasks().elasticApiTasks().getBroadcomLastLogTime().minusMinutes(2);
        LocalDateTime to = LocalDateTime.now(ZoneOffset.UTC);

        String minutes = harness.getConfig("broadcom.intervalFromLastRecord");
        if (minutes != null) {
            to = from.plusMinutes(Long.parseLong(minutes));
        }

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMdduuHHmm");
        String dates = from.format(formatter) + "_" + to.format(formatter);
        harness.log("Calculated dates for download are: " + dates);

        harness.withBroadcom()
                .openBroadcomLoginPage()
                .loginWithUsernameAndPassword(credentials.getUsername(), credentials.getPassword())
                .switchFrameAndGoToAllTransactions()
                .fillFromDateTime(String.valueOf(from.getYear()), String.valueOf(from.getMonthValue()), String.valueOf(from.getDayOfMonth()), String.valueOf(from.getHour()), String.valueOf(from.getMinute()))
                .fillToDateTime(String.valueOf(to.getYear()), String.valueOf(to.getMonthValue()), String.valueOf(to.getDayOfMonth()), String.valueOf(to.getHour()), String.valueOf(to.getMinute()))
                .clickToExportAndDownloadFile(dates);
    }
}