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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.exevido.DataBoxType;
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.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.util.task.ufo.cb.CommercialTasks.JIRA_COMMENT_STORE_KEY;

@TestScenarioWithOrder(name = "Create New Incoming Message")
public class CreateNewIncomingMessage {

    @Order(1)
    @TestCase(name = "Create New Incoming Message Without Case")
    @JiraTestCase(id = "BOC-T8944")
    public void withoutCase(Harness harness, @Key(Keys.exevido.inheritance) Credentials credentials) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(credentials))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance("Sára Blínová", "uchhtu9", ATTACHMENT_PATH))
                .clickInheritanceMmbWithoutFolder()
                .searchByMessageId(harness.get(INCOMING_MESSAGE_ID_KEY))
                .checkFirstIncomingMessagePresence()
                .checkMessageTitlePresence("Sára Blínová");
    }

    @Order(2)
    @TestCase(name = "Create New Incoming Message For Processing")
    @JiraTestCase(id = "BOC-T8945")
    public void forProcessing(Harness harness, @Key(Keys.exevido.inheritance) Credentials credentials) {
        String subjectForProcessing = harness.getConfig("commercial.exevido.subject-for-processing");
        String attachmentForProcessing = harness.getConfig("commercial.exevido.attachment-for-processing");
        String internalReferenceNumberForProcessing = harness.getConfig("commercial.exevido.internal-reference-number-for-processing");

        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(credentials))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance(subjectForProcessing, "Axis Ostrava", attachmentForProcessing))
                .clickInheritanceMmbForProcessing()
                .clickRequestMmb()
                .searchByMessageId(harness.get(INCOMING_MESSAGE_ID_KEY))
                .clickIncomingMessageRow(30)
                .checkInternalReferenceNumber(internalReferenceNumberForProcessing);
    }

    @Order(3)
    @TestCase(name = "Create New Incoming Message Copy To Another DataBox")
    @JiraTestCase(id = "BOC-T8946")
    public void copyToAnotherDataMailbox(Harness harness, @Key(Keys.exevido.inheritance) Credentials credentials) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(credentials))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance("Test kopie", "Axis Ostrava", ATTACHMENT_PATH))
                .sendToAnotherDataBox()
                .clickUniversalSingleSelect()
                .fillSingleSelect(DataBoxType.MSS.getValue())
                .clickSubmitSingleSelect()
                .clickConfirm()
                .checkMessageId(harness.get(INCOMING_MESSAGE_ID_KEY).toString().replaceFirst("$", "-1"));
    }

    @Order(4)
    @TestCase(name = "Create New Incoming Message Copy")
    @JiraTestCase(id = "BOC-T8954")
    public void copy(Harness harness, @Key(Keys.exevido.inheritance) Credentials credentials) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(credentials))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance("DĚD 11 - kopie", "Axis Ostrava", ATTACHMENT_PATH))
                .clickCreateCopy()
                .clickConfirmAttachment()
                .clickSave()
                .clickIncomingMessages()
                .clickAllIncomingMessages()
                .searchByMessageId(harness.get(INCOMING_MESSAGE_ID_KEY))
                .clickIncomingMessageRow(20)
                .fillGroup("04 - Policie")
                .clickDelegate();
    }

    @Order(5)
    @TestCase(name = "Create Incoming Message Change Message Type")
    @JiraTestCase(id = "BOC-T8952")
    public void changeMessageType(Harness harness, @Key(Keys.exevido.inheritance) Credentials credentials) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(credentials))
                .clickIncomingMessages()
                .then(harness.tasks().exevidoTasks().createNewIncomingMessageTasks().forInheritance("Změna typu dokumentu", "Axis Ostrava", ATTACHMENT_PATH))
                .clickChangeMessageType()
                .clickUniversalSingleSelect()
                .fillSingleSelect("D - Obecná - MMB")
                .clickSubmitSingleSelect();
    }

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