from tempfile import mkdtemp
from unittest.mock import Mock

import pytest

from galaxy.model import tool_shed_install
from galaxy.model.tool_shed_install import mapping
from galaxy.tool_shed.cache import ToolShedRepositoryCache
from galaxy.tool_util.toolbox.base import ToolConfRepository
from galaxy_test.base.mock_http_server import (
    MockHTTPRequestHandler,
    MockHttpServer,
    start_mock_http_server,
)


@pytest.fixture(scope="session")
def mock_http_server():
    server, base_url = start_mock_http_server()
    try:
        yield MockHttpServer(base_url=base_url, handler_class=MockHTTPRequestHandler, is_remote=False)
    finally:
        server.shutdown()


@pytest.fixture
def mock_app():
    app = Mock()
    app.install_model = mapping.init("sqlite:///:memory:", create_tables=True)
    return app


@pytest.fixture
def tool_shed_repository_cache(mock_app):
    tool_shed_repository_cache = ToolShedRepositoryCache(session=mock_app.install_model.context)
    return tool_shed_repository_cache


@pytest.fixture
def repos(mock_app):
    session = mock_app.install_model.context
    repositories = [create_repo(session, changeset=i + 1, installed_changeset=i) for i in range(10)]
    session.commit()
    return repositories


@pytest.fixture
def tool_conf_repos(tool_shed_repository_cache):
    for i in range(10, 20):
        repo = ToolConfRepository(
            "github.com",
            "example",
            "galaxyproject",
            str(i),
            str(i + 1),
            None,
            repository_path=mkdtemp(prefix="repository_path"),
            tool_path="../shed_tools",
        )
        tool_shed_repository_cache.add_local_repository(repo)
    return tool_shed_repository_cache.local_repositories


def create_repo(session, changeset, installed_changeset, config_filename=None):
    metadata = {
        "tools": [
            {
                "add_to_tool_panel": False,  # to have repository.includes_tools_for_display_in_tool_panel=False in InstalledRepositoryManager.activate_repository()
                "guid": f"github.com/galaxyproject/example/test_tool/0.{changeset}",
                "tool_config": "tool.xml",
            }
        ],
    }
    if config_filename:
        metadata["shed_config_filename"] = config_filename
    repository = tool_shed_install.ToolShedRepository(metadata_=metadata)
    repository.tool_shed = "github.com"
    repository.owner = "galaxyproject"
    repository.name = "example"
    repository.changeset_revision = str(changeset)
    repository.installed_changeset_revision = str(installed_changeset)
    repository.deleted = False
    repository.uninstalled = False
    session.add(repository)
    session.commit()
    tool_dependency = tool_shed_install.ToolDependency(
        name="Name",
        version="100",
        type="package",
        status="ok",
        tool_shed_repository_id=repository.id,
    )
    session.add(tool_dependency)
    return repository
