package cz.moneta.test.regression.backoffice.exevido.web.inheritance;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.util.credentials.Keys;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import static cz.moneta.test.dsl.exevido.pages.NewIncomingMessagePage.INCOMING_MESSAGE_ID_KEY;
import static cz.moneta.test.dsl.exevido.pages.NewOutgoingMessagePage.ATTACHMENT_PATH;
import static cz.moneta.test.dsl.exevido.pages.NewOutgoingMessagePage.OUTGOING_MESSAGE_ID_KEY;
import static cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks.JIRA_COMMENT_STORE_KEY;
import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenarioWithOrder(name = "Verify Four Eyes Control For Incoming Messages")
class VerifyFourEyesControlForIncomingMessages {

    private String controller;

    @BeforeAll
    protected void setUp(Harness harness) {
        controller = harness.getConfig("commercial.exevido.controller");
    }

    @Order(1)
    @TestCase(name = "Verify Four Eyes Control For Incoming Messages - Approved", environments = {TST1, PPE})
    @JiraTestCase(id = "BOC-T9043")
    public void approved(Harness harness, @Key(Keys.exevido.inheritance) Credentials inheritanceSender, @Key(Keys.exevido.inheritanceController) Credentials InheritanceController) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(inheritanceSender))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance("DĚD 5 - Kontrola 4 očí - ok", "uchhtu9", ATTACHMENT_PATH))
                .then(harness.tasks().exevidoTasks().incomingMessageTasks().selectFirstFolderFileAndSave())
                .clickAnswer()
                .clickPdfTemplateGenerating()
                .clickApplication()
                .clickInspection()
                .storeMessageId()
                .then(harness.tasks().exevidoTasks().outgoingMessageTasks().assignReviewerAndSubmit(controller))
                .logOutFromExevido()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(InheritanceController))
                .clickMyMessages()
                .clickForCheckMessages()
                .searchByMessageId(harness.get(OUTGOING_MESSAGE_ID_KEY))
                .clickOutgoingMessageRow(20)
                .clickSendReply();
    }

    @Order(2)
    @TestCase(name = "Verify Four Eyes Control For Incoming Messages - Returned", environments = {TST1, PPE})
    @JiraTestCase(id = "BOC-T8947")
    public void returned(Harness harness, @Key(Keys.exevido.inheritance) Credentials inheritanceSender, @Key(Keys.exevido.inheritanceController) Credentials InheritanceController) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(inheritanceSender))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance("DĚD 5 - Kontrola 4 očí - vráceno", "uchhtu9", ATTACHMENT_PATH))
                .then(harness.tasks().exevidoTasks().incomingMessageTasks().selectFirstFolderFileAndSave())
                .clickAnswer()
                .clickPdfTemplateGenerating()
                .clickApplication()
                .clickInspection()
                .storeMessageId()
                .then(harness.tasks().exevidoTasks().outgoingMessageTasks().assignReviewerAndSubmit(controller))
                .logOutFromExevido()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(InheritanceController))
                .clickMyMessages()
                .clickForCheckMessages()
                .searchByMessageId(harness.get(OUTGOING_MESSAGE_ID_KEY))
                .clickOutgoingMessageRow(20)
                .clickReturnToProcessing()
                .fillReason("test DĚD 5")
                .clickAddReason();
    }

    @AfterEach
    protected void tearDown(Harness harness) {
        harness.store(JIRA_COMMENT_STORE_KEY,
                "\nMessage ID příchozí: " + harness.get(INCOMING_MESSAGE_ID_KEY, true) +
                        "\nMessage ID odchozí: " + harness.get(OUTGOING_MESSAGE_ID_KEY, true));
    }
}