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.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()
                .clickCreateNewIncomingMessage()
                .fillSubject("Sára Blínová")
                .fillSender("uchhtu9")
                .fillTypeMessage("D - Žádost [M] - MMB")
                .uploadAttachment(ATTACHMENT_PATH)
                .clickSave()
                .storeMessageId()
                .clickInheritanceMmbWithoutFolder()
                .searchByMessageId(harness.get(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()
                .clickCreateNewIncomingMessage()
                .fillSubject(subjectForProcessing)
                .fillSender("Axis Ostrava")
                .fillTypeMessage("D - Žádost [M] - MMB")
                .uploadAttachment(attachmentForProcessing)
                .clickSave()
                .storeMessageId()
                .clickInheritanceMmbForProcessing()
                .clickRequestMmb()
                .searchByMessageId(harness.get(MESSAGE_ID_KEY))
                .clickIncomingMessageRow(30)
                .checkInternalReferenceNumber(internalReferenceNumberForProcessing);
    }

    @Order(3)
    @TestCase(name = "Create New Incoming Message Copy")
    @JiraTestCase(id = "BOC-T8946")
    public void copy(Harness harness, @Key(Keys.exevido.inheritance) Credentials credentials) {
        harness.withExevido()
                .openLoginPage()
                .then(harness.tasks().exevidoTasks().login().withCredentialsToMmb(credentials))
                .clickIncomingMessages()
                .clickCreateNewIncomingMessage()
                .fillSubject("Test kopie")
                .fillSender("Axis Ostrava")
                .fillTypeMessage("D - Žádost [M] - MMB")
                .uploadAttachment(ATTACHMENT_PATH)
                .clickSave()
                .storeMessageId()
                .sendToAnotherDataBox()
                .clickUniversalSingleSelect()
                .fillSingleSelect(DataBoxType.MSS.getValue())
                .clickSubmitSingleSelect()
                .clickConfirm()
                .checkMessageId(harness.get(MESSAGE_ID_KEY).toString().replaceFirst("$", "-1"));
    }

    @Order(4)
    @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()
                .clickCreateNewIncomingMessage()
                .fillSubject("Změna typu dokumentu")
                .fillSender("Axis Ostrava")
                .fillTypeMessage("D - Žádost [M] - MMB")
                .uploadAttachment(ATTACHMENT_PATH)
                .clickSave()
                .clickChangeMessageType()
                .clickUniversalSingleSelect()
                .fillSingleSelect("D - Obecná - MMB")
                .clickSubmitSingleSelect()
                .storeMessageId();
    }

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