/*
 * Licensed to Apache Software Foundation (ASF) under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Apache Software Foundation (ASF) licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.dolphinscheduler.e2e.cases;

import org.apache.dolphinscheduler.e2e.core.DolphinScheduler;
import org.apache.dolphinscheduler.e2e.pages.LoginPage;
import org.apache.dolphinscheduler.e2e.pages.common.NavBarPage;
import org.apache.dolphinscheduler.e2e.pages.project.ProjectDetailPage;
import org.apache.dolphinscheduler.e2e.pages.project.ProjectPage;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.TaskInstanceTab;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.WorkflowDefinitionTab;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.WorkflowForm;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.WorkflowForm.TaskType;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.WorkflowInstanceTab;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.WorkflowInstanceTab.Row;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.task.ShellTaskForm;
import org.apache.dolphinscheduler.e2e.pages.project.workflow.task.SwitchTaskForm;
import org.apache.dolphinscheduler.e2e.pages.security.SecurityPage;
import org.apache.dolphinscheduler.e2e.pages.security.TenantPage;

import org.testcontainers.shaded.org.awaitility.Awaitility;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@DolphinScheduler(composeFiles = "docker/basic/docker-compose.yaml")
class WorkflowSwitchE2ETest {
    private static final String project = "test-workflow-1";
    private static final String workflow = "test-workflow-1";
    private static final String ifBranchName = "key==1";
    private static final String elseBranchName = "key!=1";

    private static final String tenant = System.getProperty("user.name");

    private static RemoteWebDriver browser;

    @BeforeAll
    public static void setup() {
        new LoginPage(browser)
            .login("admin", "dolphinscheduler123")
            .goToNav(SecurityPage.class)
            .goToTab(TenantPage.class)
            .create(tenant)
            .goToNav(ProjectPage.class)
            .create(project)
        ;
    }

    @AfterAll
    public static void cleanup() {
        new NavBarPage(browser)
            .goToNav(ProjectPage.class)
            .goTo(project)
            .goToTab(WorkflowDefinitionTab.class)
            .cancelPublishAll()
            .deleteAll()
        ;
        new NavBarPage(browser)
            .goToNav(ProjectPage.class)
            .delete(project)
            .goToNav(SecurityPage.class)
            .goToTab(TenantPage.class)
            .delete(tenant)
        ;
    }

    @Test
    @Order(1)
    void testCreateSwitchWorkflow() {

        final WorkflowDefinitionTab workflowDefinitionPage =
            new ProjectPage(browser)
                .goTo(project)
                .goToTab(WorkflowDefinitionTab.class);

        WorkflowForm workflowForm = workflowDefinitionPage.createWorkflow();

        workflowForm.<ShellTaskForm> addTask(TaskType.SHELL)
            .script("echo ${today}\necho ${global_param}\n")
            .name("pre-task")
            .submit();

        SwitchTaskForm switchTaskForm = workflowForm.addTask(TaskType.SWITCH);
        switchTaskForm.preTask("pre-task")
            .name("switch")
            .submit();

        workflowForm.<ShellTaskForm>addTask(TaskType.SHELL)
            .script("echo ${key}")
            .preTask("switch")
            .name(ifBranchName)
            .submit();

        workflowForm.<ShellTaskForm>addTask(TaskType.SHELL)
            .script("echo ${key}")
            .preTask("switch")
            .name(elseBranchName)
            .submit();

        // format dag
        workflowForm.formatDAG().confirm();

        // add branch for switch task
        workflowForm.getTask("switch");
        switchTaskForm.addIfBranch("${key}==1", ifBranchName);
        switchTaskForm.elseBranch(elseBranchName);
        switchTaskForm.submit();

        workflowForm.submit()
            .name(workflow)
            .addGlobalParam("key", "1")
            .submit();

        Awaitility.await().untilAsserted(() -> assertThat(
            workflowDefinitionPage.workflowList()
        ).anyMatch(it -> it.getText().contains(workflow)));

        workflowDefinitionPage.publish(workflow);
    }

    @Test
    @Order(10)
    void testRunWorkflow() {
        final ProjectDetailPage projectPage =
                new ProjectPage(browser)
                        .goToNav(ProjectPage.class)
                        .goTo(project);

        projectPage
                .goToTab(WorkflowInstanceTab.class)
                .deleteAll();

        projectPage
                .goToTab(WorkflowDefinitionTab.class)
                .run(workflow)
                .submit();

        Awaitility.await().untilAsserted(() -> {
            browser.navigate().refresh();

            final Row row = projectPage
                    .goToTab(WorkflowInstanceTab.class)
                    .instances()
                    .iterator()
                    .next();

            assertThat(row.isSuccess()).isTrue();
            assertThat(row.executionTime()).isEqualTo(1);
        });

        // check task for switch
        List<TaskInstanceTab.Row> taskInstances = projectPage
                .goToTab(TaskInstanceTab.class)
                .instances();

        Awaitility.await().untilAsserted(() -> {
            assertThat(taskInstances.size()).isEqualTo(3);
            assertThat(taskInstances.stream().filter(row -> row.name().contains(ifBranchName)).count()).isEqualTo(1);
            assertThat(taskInstances.stream().noneMatch(row -> row.name().contains(elseBranchName))).isTrue();
        });
    }
}
