package cz.moneta.test.regression.backoffice.ufooperations.jenkinstests;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.taskslist.TypesOfViewFilter;
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.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;

import static cz.moneta.test.dsl.ufo.banka.pages.fon.DMS001_S001_ScanDocumentsPage.FAKE_ZL;
import static cz.moneta.test.dsl.ufo.banka.pages.fon.UploadUtil.getResourcePath;
import static cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskList.SECOND_TASK_ID_KEY;
import static cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskList.TASK_ID_KEY;
import static cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskPage.PDF_IMG;
import static cz.moneta.test.dsl.util.task.ufo.cb.CommercialTasks.JIRA_COMMENT_STORE_KEY;

@TestScenarioWithOrder(name = "CheckTaskDetail")
public class CheckTaskDetail {

    private String rcOfExistingClient;

    @BeforeAll
    protected void setUp(Harness harness) {
        rcOfExistingClient = harness.getConfig("ufo.operations.rc-of-existing-client");
    }

    @Order(1)
    @TestCase(name = "Check Detail - part 1")
    @JiraTestCase(id = "BOC-T8462")
    public void checkTaskDetail1(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        String attachmentTitle = "Nazev prilohy pridane automatem";
        OperationsTasks operationsTasksTasks = harness.tasks().ufo().operations();
        String attachmentPath = getResourcePath(FAKE_ZL);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentials(credentials))
                .clickTasks()
                .then(operationsTasksTasks.tasks().createSimpleTask(rcOfExistingClient))
                .selectTypeOfView(TypesOfViewFilter.ASSIGNED_BY_ME)
                .fillTaskID(harness.get(TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .clickAddNote()
                .fillNote("úkol založený automatem")
                .clickOkButton()
                .then(operationsTasksTasks.tasks().checkAttachmentUpload("Bankovní informace", attachmentPath, attachmentTitle))
                .clickFirstAttachmentDetail()
                .waitForAttachmentDetail()
                .clickSolveTab()
                .clickFileDownload(PDF_IMG)
                .clickSolve()
                .clickSaveTask()
                .clickDelegateTask()
                .fillTaskID(harness.get(TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .clickSolve()
                .clickReturnTask()
                .fillNote("test")
                .clickOkButton();
    }

    @Order(2)
    @TestCase(name = "Check Detail - part 2")
    @JiraTestCase(id = "BOC-T8819")
    public void checkTaskDetail2(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        OperationsTasks operationsTasksTasks = harness.tasks().ufo().operations();
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentials(credentials))
                .clickTasks()
                .then(operationsTasksTasks.tasks().createSimpleTask(rcOfExistingClient))
                .selectTypeOfView(TypesOfViewFilter.ASSIGNED_BY_ME)
                .fillTaskID(harness.get(TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .clickHistory()
                .clickSolve()
                .clickTaskChangeCheckbox()
                .fillReason("Automat test")
                .clickOkButton()
                .clickCancellation()
                .fillNote("Storno úkolu")
                .clickOkCancellation();
    }

    @Order(3)
    @TestCase(name = "Check Detail - part 3")
    @JiraTestCase(id = "BOC-T8820")
    public void checkTaskDetail3(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        OperationsTasks operationsTasksTasks = harness.tasks().ufo().operations();
        String attachmentTitle = "Nazev prilohy pridane automatem";
        String attachmentPath = getResourcePath(FAKE_ZL);

        harness.withUfo()
                .openLoginPage()
                .then(operationsTasksTasks.login().withCredentials(credentials))
                .clickTasks()
                .then(operationsTasksTasks.tasks().createSimpleTask(rcOfExistingClient))
                .then(operationsTasksTasks.tasks().createSecondSimpleTask())
                .fillTaskID(harness.get(TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .then(operationsTasksTasks.tasks().checkAttachmentUpload("Bankovní informace", attachmentPath, attachmentTitle))
                .fillIdTaskConnect(harness.get(SECOND_TASK_ID_KEY))
                .clickFirstAttachment()
                .clickConnectTasks()
                .clickHistory()
                .clickTaskChangeCheckbox()
                .fillReason("Automat test")
                .clickOkButton()
                .clickTaskList()
                .selectTypeOfView(TypesOfViewFilter.ASSIGNED_BY_ME)
                .fillTaskID(harness.get(TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .clickTaskTree()
                .then(operationsTasksTasks.tasks().createSubtask())
                .clickFinishProcess()
                .fillCommentAndClickAgreeFinish("Ukončit úkol")
                .assertStatusLabel("Úkol byl v pořádku dokončen !");
    }

    @AfterEach
    protected void tearDown(Harness harness) {
        String secondTask = harness.get(SECOND_TASK_ID_KEY, true);
        harness.store(JIRA_COMMENT_STORE_KEY,
                "\nUFO Session ID: " + harness.get("reports.tmfj.execution.commentufosessionid", true) +
                        "\nID Ukolu: " + harness.get(TASK_ID_KEY, true) +
                        ((secondTask != null) ? "\nID Ukolu2: " + secondTask : ""));
    }
}