from typing import TYPE_CHECKING

from galaxy_test.base.workflow_fixtures import WORKFLOW_SIMPLE_CAT_TWICE
from galaxy_test.selenium.framework import (
    managed_history,
    RunsWorkflows,
    UsesHistoryItemAssertions,
)
from .framework import (
    selenium_test,
    SeleniumIntegrationTestCase,
)

if TYPE_CHECKING:
    from galaxy_test.selenium.framework import SeleniumSessionDatasetPopulator


class BaseWorkflowRunTargetTestCase(SeleniumIntegrationTestCase, RunsWorkflows, UsesHistoryItemAssertions):
    dataset_populator: "SeleniumSessionDatasetPopulator"
    ensure_registered = True


class TestWorkflowRunTargetNewSeleniumIntegration(BaseWorkflowRunTargetTestCase):
    @classmethod
    def handle_galaxy_config_kwds(cls, config):
        super().handle_galaxy_config_kwds(config)
        config["simplified_workflow_run_ui_target_history"] = "new"
        config["simplified_workflow_run_ui"] = "prefer"

    @selenium_test
    @managed_history
    def test_execution_to_new_history(self):
        filename = self.test_data_resolver.get_filename("1.fasta")
        self.perform_upload(filename)
        self.wait_for_history()
        self.workflow_run_open_workflow(WORKFLOW_SIMPLE_CAT_TWICE)
        workflow_run = self.components.workflow_run
        workflow_run.expanded_form.wait_for_absent_or_hidden()
        self.workflow_run_submit()
        self.sleep_for(self.wait_types.UX_TRANSITION)
        workflow_run.not_current_history_indicator.wait_for_visible()
        workflow_run.history_target_link.wait_for_and_click()
        self.sleep_for(self.wait_types.UX_TRANSITION)
        workflow_run.current_history_indicator.wait_for_visible()
        self.workflow_run_wait_for_ok(hid=2, expand=True)
        self.assert_item_summary_includes(2, "2 sequences")


class TestWorkflowRunTargetCurrentSeleniumIntegration(BaseWorkflowRunTargetTestCase):
    @classmethod
    def handle_galaxy_config_kwds(cls, config):
        super().handle_galaxy_config_kwds(config)
        config["simplified_workflow_run_ui_target_history"] = "current"
        config["simplified_workflow_run_ui"] = "prefer"

    @selenium_test
    @managed_history
    def test_execution_in_current_history(self):
        filename = self.test_data_resolver.get_filename("1.fasta")
        self.perform_upload(filename)
        self.wait_for_history()
        self.workflow_run_open_workflow(WORKFLOW_SIMPLE_CAT_TWICE)
        workflow_run = self.components.workflow_run
        workflow_run.expanded_form.wait_for_absent_or_hidden()
        self.workflow_run_submit()
        self.sleep_for(self.wait_types.UX_TRANSITION)
        workflow_run.current_history_indicator.wait_for_visible()
        self.sleep_for(self.wait_types.UX_TRANSITION)
        self.workflow_run_wait_for_ok(hid=2, expand=True)
        self.assert_item_summary_includes(2, "2 sequences")


class TestWorkflowRunTargetSelectNewSeleniumIntegration(BaseWorkflowRunTargetTestCase):
    @classmethod
    def handle_galaxy_config_kwds(cls, config):
        super().handle_galaxy_config_kwds(config)
        config["simplified_workflow_run_ui_target_history"] = "prefer_current"
        config["simplified_workflow_run_ui"] = "prefer"

    @selenium_test
    @managed_history
    def test_execution_in_current_history(self):
        filename = self.test_data_resolver.get_filename("1.fasta")
        self.perform_upload(filename)
        self.wait_for_history()
        self.workflow_run_open_workflow(WORKFLOW_SIMPLE_CAT_TWICE)
        workflow_run = self.components.workflow_run
        workflow_run.expanded_form.wait_for_absent_or_hidden()
        workflow_run.runtime_setting_button.wait_for_and_click()
        workflow_run.runtime_setting_target.wait_for_and_click()
        self.send_escape()
        workflow_run.runtime_setting_button.wait_for_and_click()
        workflow_run.runtime_setting_target.wait_for_absent_or_hidden()
        self.workflow_run_submit()
        self.sleep_for(self.wait_types.UX_TRANSITION)
        workflow_run.not_current_history_indicator.wait_for_visible()
        workflow_run.history_target_link.wait_for_and_click()
        self.sleep_for(self.wait_types.UX_TRANSITION)
        workflow_run.current_history_indicator.wait_for_visible()
        self.workflow_run_wait_for_ok(hid=2, expand=True)
        self.assert_item_summary_includes(2, "2 sequences")
