package cz.moneta.test.regression.commercial.cb;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks;
import cz.moneta.test.harness.annotations.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Order;

import java.util.Optional;

import static cz.moneta.test.dsl.ufo.banka.pages.main.HKO101_S001_MainClientPage.FULL_NAME_KEY;
import static cz.moneta.test.dsl.ufo.cb.landing.SME401_S001_SearchPage.SEARCH_HISTORY_CLIENT_ICO_KEY;
import static cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks.JIRA_COMMENT_STORE_KEY;

@TestScenarioWithOrder(name = "Search for specific clients")
public class SearchClient {

    private String clientIco;

    @TestCase(name = "Search for client FOP", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SC-T2112")
    @Order(1)
    public void searchClientFop(Harness harness) {
        searchClient(harness, harness.getConfig("commercial.cb.ico.fop"));
    }

    @TestCase(name = "Search for client PO", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SC-T2135")
    @Order(2)
    public void searchClientPo(Harness harness) {

        searchClient(harness, harness.getConfig("commercial.cb.ico.po"));
    }

    @TestCase(name = "Search for first client from history FOP", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SC-T2200")
    @Order(3)
    public void searchHistoryClientFop(Harness harness) {
        searchClientFromSearchHistory(harness, "FOP");
    }

    @TestCase(name = "Search for first client from history PO", environments = {Environment.TST1, Environment.PPE})
    @JiraTestCase(id = "SC-T2201")
    @Order(4)
    public void searchHistoryClientPo(Harness harness) {

        searchClientFromSearchHistory(harness, "PO");
    }

    private void searchClient(Harness harness, String ico) {
        clientIco = ico;
        CommercialTasks commercialTasks = harness.tasks().ufo().cb();
        harness.withUfo()
                .openLoginPage()
                .then(commercialTasks.loginAsCbBanker())
                .then(commercialTasks.searchClient(clientIco))
                .storeClientFullName()
                .checkIco(clientIco);

    }

    private void searchClientFromSearchHistory(Harness harness, String legalPersonType) {
        clientIco = null;
        CommercialTasks commercialTasks = harness.tasks().ufo().cb();
        harness.withUfo()
                .openLoginPage()
                .then(commercialTasks.loginAsCbBanker())
                .clickSearchLink()
                .storeIcoOfFirstClientInSearchHistoryByLegalType(legalPersonType)
                .clickFirstClientInSearchHistoryByLegalType(legalPersonType)
                .storeClientFullName()
                .checkIco(harness.get(SEARCH_HISTORY_CLIENT_ICO_KEY));

    }

    @AfterEach
    protected void teardown(Harness harness) {

        String historyClientICO = harness.get(SEARCH_HISTORY_CLIENT_ICO_KEY, true);
        clientIco = Optional.ofNullable(historyClientICO).orElseGet(() -> clientIco);
        harness.store(JIRA_COMMENT_STORE_KEY,
                "\nIČO klienta: " + clientIco +
                        "\nJméno klienta: " + harness.get(FULL_NAME_KEY, true) +
                        "\nUFO Session ID: " + harness.get("reports.tmfj.execution.commentufosessionid", true));
    }

}
