import pytest
from unittest import mock

from anaconda_navigator.widgets.dialogs.login import (
    MainLoginDialog, AnacondaLoginPage, EnterpriseRepoLoginPage, EnterpriseRepoSetDomainPage,
    CommercialEditionLoginPage, TeamEditionLoginPage, TeamEditionAddChannelsPage, TeamEditionSetDomainPage, QWidget
)


def _get_main_dialog_mock():
    main_mock = mock.Mock()
    main_mock.parent = QWidget()
    return main_mock


def dialog_bot(request, qtbot, widget):
    request.cls.qtbot = qtbot
    request.cls.widget = widget

    qtbot.addWidget(widget)
    widget.show()


@pytest.fixture
def main_dialog_bot(request, qtbot):
    dialog_bot(request, qtbot, MainLoginDialog())


@pytest.fixture
def anaconda_org_dialog_bot(request, qtbot):
    dialog_bot(request, qtbot, AnacondaLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def commercial_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, CommercialEditionLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def enterprise_domain_set_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, EnterpriseRepoSetDomainPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def enterprise_login_page_dialog_bot(request, qtbot):
    request.cls.config_url_option = "anaconda_api_url"
    return dialog_bot(request, qtbot, EnterpriseRepoLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def team_domain_set_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, TeamEditionSetDomainPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def anaconda_org_dialog_bot(request, qtbot):
    dialog_bot(request, qtbot, AnacondaLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def commercial_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, CommercialEditionLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def enterprise_domain_set_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, EnterpriseRepoSetDomainPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def enterprise_login_page_dialog_bot(request, qtbot):
    request.cls.config_url_option = "anaconda_api_url"
    return dialog_bot(request, qtbot, EnterpriseRepoLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def team_domain_set_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, TeamEditionSetDomainPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def team_login_page_dialog_bot(request, qtbot):
    return dialog_bot(request, qtbot, TeamEditionLoginPage(mock.Mock(), _get_main_dialog_mock()))


@pytest.fixture
def team_add_channels_page_dialog_bot(request, qtbot):
    anaconda_api_mock = mock.Mock()
    anaconda_api_mock.get_channels.return_value = tuple()
    return dialog_bot(request, qtbot, TeamEditionAddChannelsPage(anaconda_api_mock, _get_main_dialog_mock()))
