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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.operations.ufoOperations.UKO720_S001_TaskPage;
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.TestScenario;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.auth.Key;
import cz.moneta.test.harness.support.web.Lookup;
import org.junit.jupiter.api.BeforeAll;

import java.util.List;

import static cz.moneta.test.dsl.ufo.banka.pages.fon.UploadUtil.getResourcePath;
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.*;
import static cz.moneta.test.dsl.ufo.operations.ufoOperations.taskslist.StateOfTaskFilter.*;
import static cz.moneta.test.dsl.ufo.operations.ufoOperations.taskslist.TypesOfViewFilter.*;

@TestScenario(name = "Tasklist tests")
public class TasklistTests {

    private OperationsTasks operationsTasks;
    private String rcOfExistingClient;
    private String cifOfClient;

    @BeforeAll
    public void setupInfo(Harness harness) {
        operationsTasks = harness.tasks().ufo().operations();
        rcOfExistingClient = harness.getSystemOrConfigValue("cc.somTaskListClient.rc");
        cifOfClient = harness.getSystemOrConfigValue("cc.somTaskListClient.cif");
    }

    @TestCase(name = "Loading of tasklist")
    @JiraTestCase(id = "CC-T2137")
    public void loadingOfTasklist(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .clickTasks()
                .checkPresentOfFirstTask();
    }

    @TestCase(name = "List of tasks")
    @JiraTestCase(id = "CC-T2138")
    public void listOfTasks(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .clickTasks()
                .checkStatesOfTaskFilterValues(true, List.of(ERROR_CORRECTING_DATA, ASSIGNED, RETURNED))
                .checkStatesOfTaskFilterValues(false, List.of(NEW, ASSIGNED, IN_SOLVING))
                .checkIncludingLockedCheckbox()
                .clickOpenTaskDetail()
                .checkPresenceOfTaskId()
                .checkDelegateTaskBtn()
                .clickListOfTasks()
                .selectTypeOfView(MY_FOR_PROCESSING)
                .checkStatesOfTaskFilterValues(true, List.of(NEW, ASSIGNED, ASSIGNING, RETURNED, IN_SOLVING, TASK_IN_ACCEPTATION_WORKFLOW))
                .clickOpenTaskDetail()
                .checkPresenceOfTaskId()
                .clickListOfTasks()
                .selectTypeOfView(MY_FOR_VERIFICATION)
                .checkStatesOfTaskFilterValues(true, List.of(PROCESSED_UNAUTHORIZED))
                .selectTypeOfView(PROCESSED_BY_ME)
                .checkStatesOfTaskFilterValues(true, List.of(PROCESSED, PROCESSED_AUTHORIZED))
                .clickExpandFilterArrow()
                .clickCollapseFilterArrow()
                .checkDataFiltering("0")
                .clickFilter()
                .clickDepartments()
                .clickDepartmentFilterNameDynamic("4")
                .clickFind()
                .storeFirstRecordTaskId()
                .clickRevitalization()
                .selectTypeOfView(TASK_SEARCH)
                .fillTaskID(harness.get(TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .clickSolve()
                .clickFinishAndAcceptAnswerAlert()
                .fillNote("Autotest")
                .clickFinishOK()
                .selectTypeOfView(ASSIGNED_BY_ME)
                .checkStatesOfTaskFilterValues(true, List.of(NEW, ASSIGNED, CANCELED, RETURNED, IN_SOLVING, PROCESSED, PROCESSED_UNAUTHORIZED, PROCESSED_AUTHORIZED))
                .clickExpandFilterArrow()
                .clickCollapseFilterArrow()
                .checkDataFiltering("0")
                .clickOpenTaskDetail()
                .checkUpdateBtn()
                .clickListOfTasks()
                .selectTypeOfView(TASK_SEARCH)
                .checkStatesOfTaskFilterValues(true, List.of(ERROR_CORRECTING_DATA, NEW, ASSIGNED, ASSIGNING, CANCELED, RETURNED, IN_SOLVING, PROCESSED, PROCESSED_UNAUTHORIZED, PROCESSED_AUTHORIZED, TASK_IN_ACCEPTATION_WORKFLOW, WAITING))
                .typeIntoSsoSolver("212628185")
                .clickFind()
                .checkSearchOptionsEnabled()
                .clickOpenTaskDetail()
                .checkDelegateTaskBtnNotEnabled()
                .clickTaskList()
                .clickReset()
                .typeIntoSsoSubmitter("212628185")
                .clickFind()
                .clickOpenTaskDetail()
                .checkUpdateBtn()
                .clickTaskList()
                .clickReset()
                .fillTaskID("T_0001573310101")
                .clickFind()
                .clickOpenTaskDetail()
                .checkUpdateBtn()
                .clickTaskList()
                .clickReset()
                .fillBirthNumber("9206176276")
                .clickFind()
                .clickOpenTaskDetail()
                .checkUpdateBtn()
                .clickTaskList()
                .clickExpandFilterArrow()
                .clickCollapseFilterArrow()
                .checkFirstStateOfTasksByViewType(TEAM_TASKS, ERROR_CORRECTING_DATA)
                .checkFirstStateOfTasksByViewType(ASSIGNED_BY_TEAM, NEW)
                .checkFirstStateOfTasksByViewType(TEAM_FOR_REVIEW, PROCESSED_UNAUTHORIZED)
                .checkFirstStateOfTasksByViewType(MY_FOR_PROCESSING, NEW)
                .checkFirstStateOfTasksByViewType(MY_FOR_VERIFICATION, PROCESSED_UNAUTHORIZED)
                .checkFirstStateOfTasksByViewType(PROCESSED_BY_ME, PROCESSED)
                .checkFirstStateOfTasksByViewType(ASSIGNED_BY_ME, NEW)
                .checkFirstStateOfTasksByViewType(MY_FOR_APPROVAL, TASK_IN_ACCEPTATION_WORKFLOW)
                .checkFirstStateOfTasksByViewType(THIRD_PARTY_TASKS, ERROR_CORRECTING_DATA)
                .checkFirstStateOfTasksByViewType(TASK_SEARCH, ERROR_CORRECTING_DATA);
    }

    @TestCase(name = "Detail of tasklist")
    @JiraTestCase(id = "CC-T2139")
    public void detailOfTasklist(Harness harness, @Key(Keys.ufo.banka.banker) Credentials credentials) {

        String attachmentPath = getResourcePath(TASK_PDF);
        harness.withUfo()
                .openLoginPage()
                .then(operationsTasks.login().withCredentials(credentials))
                .clickTasks()
                .then(operationsTasks.tasks().createTask())
                .fillBirthNumberInTask(rcOfExistingClient)
                .clickClientDetailInfo()
                .assertClientRc(rcOfExistingClient)
                .closeSecondWindow()
                .clickListOfTasks()
                .selectTypeOfView(TASK_SEARCH)
                .fillTaskID(harness.get(UKO720_S001_TaskPage.TASK_ID_KEY))
                .clickFind()
                .clickOpenTaskDetail()
                .clickSolve()
                .clickAddNote()
                .fillNote("Automat test")
                .clickOkButton()
                .checkDateInLabel(FIRST_DATE_IN_LIST_OF_NOTES_XPATH, Lookup.XPATH)
                .clickAddAttachment()
                .fillFileUpload(attachmentPath)
                .fillFileTitle("autotest")
                .clickSaveAttachment()
                .clickFileDownload(PDF_IMG)
                .checkDateInLabel(FIRST_DATE_IN_LIST_OF_ATTACHMENTS, Lookup.ID)
                .clickFirstAttachmentDetail()
                .clickSolution()
                .clickFileDownload(FIRST_ATTACHMENT_DOWNLOAD_IMG)
                .clickTaskTree()
                .then(operationsTasks.tasks().createSubtask())
                .clickFinishProcessAndAcceptFinishAlert()
                .fillCommentAndClickAgreeFinish("autotest")
                .clickOpenTaskDetail()
                .clickHistory()
                .checkHistoryOfTask()
                .clickListOfTasks()
                .fillTaskID(harness.get(UKO720_S001_TaskPage.TASK_ID_KEY))
                .clickFind()
                .clickRevitalization()
                .clickOpenTaskDetail()
                .clickSolve()
                .clickTaskChangeCheckboxAndAcceptAlert()
                .fillReason("Automat test")
                .clickOkButton()
                .selectTaskCategory("Produkt - Běžný účet")
                .selectSubcategories("Reklamace a Stížnosti")
                .selectTaskType("Ostatní")
                .findUserAndSelectSolver()
                .fillIdOfClient(cifOfClient)
                .fillNumberOfCard("1111111111")
                .clickDelegateTask()
                .selectTypeOfView(TASK_SEARCH)
                .fillTaskID(harness.get(UKO720_S001_TaskPage.TASK_ID_KEY))
                .clickOpenTaskDetail()
                .clickSolve()
                .fillNameOfTask("Automat test")
                .clickSaveTask()
                .checkNameOfTask("Automat test")
                .clickListOfTasks()
                .selectTypeOfView(TASK_SEARCH)
                .fillTaskID(harness.get(UKO720_S001_TaskPage.TASK_ID_KEY))
                .clickOpenTaskDetail()
                .clickSolve()
                .typeIntoClientsEmail("matej.liska@moneta.cz")
                .checkStatusLabelContent(" - Update úkolu proběhl v pořádku !")
                .clickCancellation()
                .fillNote("autotest")
                .clickFinishOK()
                .selectTypeOfView(TASK_SEARCH)
                .fillTaskID(harness.get(UKO720_S001_TaskPage.TASK_ID_KEY))
                .clickFind()
                .assertTaskStatus("Stornován")
                .clickRevitalization()
                .clickOpenTaskDetail()
                .clickSolve()
                .clickFinishAndAcceptAnswerAlert()
                .fillNote("Autotest")
                .clickFinishOK()
                .clickOpenTaskDetail()
                .checkStatusLabelContent(" - Úkol byl v pořádku dokončen !");
    }
}
