package cz.moneta.test.regression.backoffice.callcenter.jenkinstests.ccretail.cczakladadc;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.search.ClientType;
import cz.moneta.test.dsl.util.credentials.Keys;
import cz.moneta.test.dsl.util.task.ufo.operations.OperationsTasks;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenarioWithOrder;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenarioWithOrder(name = "Creating and removing infoservice tests")
public class CreatingAndRemovingInfoService {
    private OperationsTasks operationsTasks;
    private String rcOfExistingClient;
    private String icOfExistingClientFOP;
    private String icOfExistingClientPO;

    @BeforeAll
    public void getSetup(Harness harness) {
        operationsTasks = harness.tasks().ufo().operations();
        rcOfExistingClient = harness.getSystemOrConfigValue("cc.createInfoserviceClientFO.rc");
        icOfExistingClientFOP = harness.getSystemOrConfigValue("cc.createInfoserviceClientFOP.ic");
        icOfExistingClientPO = harness.getSystemOrConfigValue("cc.createInfoserviceClientPO.ic");
    }

    @Order(1)
    @TestCase(name = "Creating infoservice - FO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2170")
    public void createInfoServiceFO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createInfoServiceTestFlow(harness, credentials, ClientType.FON, rcOfExistingClient);
    }

    @Order(2)
    @TestCase(name = "Editing infoservice - FO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2216")
    public void editInfoServiceFO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        editInfoServiceTestFlow(harness, credentials, ClientType.FON, rcOfExistingClient);
    }

    @Order(3)
    @TestCase(name = "Remove infoservice - FO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2178")
    public void removeInfoServiceFO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        deleteInfoServiceTestFlow(harness, credentials, ClientType.FON, rcOfExistingClient);
    }

    @Order(4)
    @TestCase(name = "Creating infoservice - FOP", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2173")
    public void createInfoServiceFOP(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createInfoServiceTestFlow(harness, credentials, ClientType.FOP, icOfExistingClientFOP);
    }

    @Order(5)
    @TestCase(name = "Editing infoservice - FOP", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2218")
    public void editInfoServiceFOP(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        editInfoServiceTestFlow(harness, credentials, ClientType.FOP, icOfExistingClientFOP);
    }

    @Order(6)
    @TestCase(name = "Remove infoservice - FOP", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2181")
    public void removeInfoServiceFOP(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        deleteInfoServiceTestFlow(harness, credentials, ClientType.FOP, icOfExistingClientFOP);
    }

    @Order(7)
    @TestCase(name = "Creating infoservice - PO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2176")
    public void createInfoServicePO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        createInfoServiceTestFlow(harness, credentials, ClientType.PO, icOfExistingClientPO);
    }

    @Order(8)
    @TestCase(name = "Editing infoservice - PO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2217")
    public void editInfoServicePO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        editInfoServiceTestFlow(harness, credentials, ClientType.PO, icOfExistingClientPO);
    }

    @Order(9)
    @TestCase(name = "Remove infoservice - PO", environments = {PPE, TST1})
    @JiraTestCase(id = "CC-T2184")
    public void removeInfoServicePO(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        deleteInfoServiceTestFlow(harness, credentials, ClientType.PO, icOfExistingClientPO);
    }

    private void createInfoServiceTestFlow(Harness harness, Credentials credentials, ClientType clientType, String client) {

        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .then(operationsTasks.searchClient().searchClientByType(clientType, client))
                .then(operationsTasks.infoServiceTasks().selectInfoServiceProductAndCheckDetails())
                .clickContinue()
                .onAdcProductsPanel()
                .clickCommonUser()
                .clickSetWithoutTelephoneOrEmailAndCheckAlert()
                .then(operationsTasks.infoServiceTasks().fillEmailAndPhoneNumberInExpressSettings("ef", "72086726727"))
                .clickSetWithoutTelephoneOrEmailAndCheckAlert()
                .then(operationsTasks.infoServiceTasks().fillEmailAndPhoneNumberInExpressSettings("212628443@moneta.cz", "734426356"))
                .then(operationsTasks.infoServiceTasks().selectStartTimeAndEndTimeInExpressSettings("07:00", "10:00"))
                .clickSet();

    }

    private void editInfoServiceTestFlow(Harness harness, Credentials credentials, ClientType clientType, String client) {

        String startTime = "07:00";
        String endTime = "10:00";
        String email = "212628443@moneta.cz";
        String phoneNumber = "720867267";

        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .then(operationsTasks.searchClient().searchClientByType(clientType, client))
                .clickInfoService()
                .clickOrderInAccountsDetail("1")
                .fillBalanceFrom("2000")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime(startTime, endTime))
                .then(operationsTasks.infoServiceTasks().selectStartTimeAndEndTimeInDetailOfOrder("07:00", "22:00"))
                .clickSave()
                .checkStatusMessage("Editace InfoServisu proběhla v pořádku")
                .clickOrderInCardsDetail("1")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime(startTime, endTime))
                .clickOrderInOthersDetail("1")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime(startTime, endTime))
                .clickOrderInAccountsDetail("1")
                .then(operationsTasks.infoServiceTasks().editSmsSectionInDetailOfOrder(phoneNumber, "2000", "11:00", "16:00"))
                .clickOrderInAccountsDetail("1")
                .checkTelephoneInDetailOfOrderInput(phoneNumber)
                .checkBalance("2000")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime("11:00", "16:00"))
                .then(operationsTasks.infoServiceTasks().editEmailSectionInDetailOfOrder(email, "3000", "12:00", "15:00"))
                .clickOrderInAccountsDetail("1")
                .checkEmailInDetailOfOrderInput(email)
                .checkBalance("3000")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime("12:00", "15:00"))
                .clickOrderInCardsDetail("1")
                .then(operationsTasks.infoServiceTasks().editSmsSectionInDetailOfOrder(phoneNumber, "2000", "11:00", "16:00"))
                .clickOrderInCardsDetail("1")
                .checkTelephoneInDetailOfOrderInput(phoneNumber)
                .checkBalance("2000")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime("11:00", "16:00"))
                .then(operationsTasks.infoServiceTasks().editEmailSectionInDetailOfOrder(email, "3000", "12:00", "15:00"))
                .clickOrderInCardsDetail("1")
                .checkEmailInDetailOfOrderInput(email)
                .checkBalance("3000")
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime("12:00", "15:00"))
                .clickOrderInOthersDetail("1")
                .clickSmsInDetailOfOrder()
                .fillTelephoneInputInDetailOfOrder(phoneNumber)
                .then(operationsTasks.infoServiceTasks().selectStartTimeAndEndTimeInDetailOfOrder("11:00", "16:00"))
                .clickSave()
                .clickOrderInOthersDetail("1")
                .checkTelephoneInDetailOfOrderInput(phoneNumber)
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime("11:00", "16:00"))
                .clickOrderInOthersDetail("1")
                .clickEmailInDetailOfOrder()
                .fillEmailInputDetailOfOrder(email)
                .then(operationsTasks.infoServiceTasks().selectStartTimeAndEndTimeInDetailOfOrder("12:00", "15:00"))
                .clickSave()
                .checkEmailInDetailOfOrderInput(email)
                .then(operationsTasks.infoServiceTasks().checkStartTimeAndEndTime("12:00", "15:00"))
                .deleteFirstAccountOrderAndCheckDeletion()
                .clickExpressSettings()
                .clickInternetPro()
                .then(operationsTasks.infoServiceTasks().fillEmailAndPhoneNumberInExpressSettings("internet@moneta.cz", phoneNumber))
                .clickSet()
                .checkTelephoneOrEmailDivInCardsFirstRow("internet@moneta.cz")
                .clickExpressSettings()
                .clickCautiousUser()
                .then(operationsTasks.infoServiceTasks().fillEmailAndPhoneNumberInExpressSettings("cautious@moneta.cz", phoneNumber))
                .clickSet()
                .checkTelephoneOrEmailDivInCardsFirstRow("cautious@moneta.cz");

    }

    private void deleteInfoServiceTestFlow(Harness harness, Credentials credentials, ClientType clientType, String client) {

        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .then(operationsTasks.searchClient().searchClientByType(clientType, client))
                .clickInfoService()
                .cancelInfoService()
                .clickHomePage()
                .then(operationsTasks.searchClient().searchClientByType(clientType, client))
                .checkInfoServiceProductIsNotPresent();

    }
}