# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2016-2017 Anaconda, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Tests for quit-like dialogs."""

# yapf: disable

# Third party imports
from qtpy.QtCore import Qt, QTimer
import pytest

# Local imports
from anaconda_navigator.config import (
    ENTERPRISE_EDITION_BRAND, TEAM_EDITION_BRAND, ANACONDA_ORG_BRAND, COMMERCIAL_EDITION_BRAND)
from anaconda_navigator.utils.fixtures import tmpconfig
from anaconda_navigator.widgets.main_window import MainWindow

# yapf: enable

xfail = pytest.mark.xfail
tmpconfig
TEST_USER = 'testuser'


# --- Fixtures
# -----------------------------------------------------------------------------
@pytest.fixture
def hide_dialogs_config(qtbot, tmpconfig):
    tmpconfig.set('main', 'hide_quit_dialog', True)
    tmpconfig.set('main', 'hide_quit_dialog', True)
    tmpconfig.set('main', 'hide_update_dialog', True)
    return tmpconfig


@pytest.fixture
def botmain(qtbot, hide_dialogs_config):
    widget = MainWindow(config=hide_dialogs_config)
    qtbot.addWidget(widget)
    widget.showMaximized()
    return qtbot, widget


# --- Tests
# -----------------------------------------------------------------------------
def test_all(qtbot, hide_dialogs_config):
    widget = MainWindow(config=hide_dialogs_config)
    qtbot.addWidget(widget)
    with qtbot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
        widget.showMaximized()


def test_home_env(qtbot, hide_dialogs_config):
    widget = MainWindow(
        tab_home=True,
        tab_environments=True,
        tab_community=False,
        tab_learning=False,
        config=hide_dialogs_config,
    )

    qtbot.addWidget(widget)
    with qtbot.waitSignal(widget.sig_ready, timeout=30000, raising=True):
        widget.showMaximized()


def notest_projects(qtbot, hide_dialogs_config):
    widget = MainWindow(
        tab_home=False,
        tab_environments=False,
        tab_project=True,
        tab_community=False,
        tab_learning=False,
        config=hide_dialogs_config,
    )
    qtbot.addWidget(widget)

    def _interact():
        with qtbot.waitSignal(widget._dialog_project_action.accepted, timeout=10000, raising=True):
            widget._dialog_project_action.accept()

    timer = QTimer()
    timer.setInterval(10000)
    timer.timeout.connect(_interact)
    timer.start()

    with qtbot.waitSignal(widget.sig_ready, timeout=30000, raising=True):
        widget.showMaximized()


def test_learning(qtbot, hide_dialogs_config):
    widget = MainWindow(
        tab_home=False,
        tab_environments=False,
        tab_project=False,
        tab_community=False,
        tab_learning=True,
        config=hide_dialogs_config,
    )

    qtbot.addWidget(widget)
    with qtbot.waitSignal(widget.sig_ready, timeout=30000, raising=True):
        widget.showMaximized()


def test_community(qtbot, hide_dialogs_config):
    widget = MainWindow(
        tab_home=False,
        tab_environments=False,
        tab_project=False,
        tab_community=True,
        tab_learning=False,
        config=hide_dialogs_config,
    )

    qtbot.addWidget(widget)
    with qtbot.waitSignal(widget.sig_ready, timeout=30000, raising=True):
        widget.showMaximized()


def test_geo_save(qtbot, hide_dialogs_config):
    widget = MainWindow(config=hide_dialogs_config)
    qtbot.addWidget(widget)
    with qtbot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
        pass
    widget.close()
    assert hide_dialogs_config.get('main', 'geo')


class TestMainWindowInteractions:
    def test_dialog_about(self, botmain):
        def _interact():
            dlg = widget._dialog_about
            dlg.accept()

        bot, widget = botmain
        with bot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
            pass
        timer = QTimer()
        timer.setInterval(5000)
        timer.timeout.connect(_interact)
        timer.start()
        widget.show_about()

    def test_dialog_channels(self, botmain):
        def _interact():
            dlg = widget._dialog_channels
            dlg.reject()

        bot, widget = botmain
        with bot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
            pass
        timer = QTimer()
        timer.setInterval(5000)
        timer.timeout.connect(_interact)
        timer.start()
        bot.mouseClick(widget.tab_home.button_channels, Qt.LeftButton)

    def test_dialog_licenses(self, botmain):
        def _interact():
            dlg = widget._dialog_licenses
            dlg.accept()

        bot, widget = botmain
        with bot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
            pass
        timer = QTimer()
        timer.setInterval(5000)
        timer.timeout.connect(_interact)
        timer.start()
        widget.show_license_manager()

    def test_dialog_logs(self, botmain):
        def _interact():
            dlg = widget._dialog_logs
            dlg.accept()

        bot, widget = botmain
        with bot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
            pass
        timer = QTimer()
        timer.setInterval(10000)
        timer.timeout.connect(_interact)
        timer.start()
        widget.show_log_viewer()

    def test_dialog_preferences(self, botmain):
        def _interact():
            dlg = widget._dialog_preferences
            dlg.reject()

        bot, widget = botmain
        with bot.waitSignal(widget.sig_ready, timeout=60000, raising=True):
            pass
        timer = QTimer()
        timer.setInterval(5000)
        timer.timeout.connect(_interact)
        timer.start()
        widget.show_preferences()

    def test_get_logged_ae4_label(self, hide_dialogs_config):
        hide_dialogs_config.set('main', 'enterprise_4_repo_api_url', 'http://api.some-repo4-url.com/api')
        hide_dialogs_config.set('main', 'logged_api_url', 'http://api.some-repo4-url.com/api')
        hide_dialogs_config.set('main', 'logged_brand', ENTERPRISE_EDITION_BRAND)
        widget = MainWindow(config=hide_dialogs_config)
        assert (ENTERPRISE_EDITION_BRAND, 'http://some-repo4-url.com') == widget.get_logged_api_label_url()

    def test_get_logged_te_label(self, hide_dialogs_config):
        hide_dialogs_config.set('main', 'team_edition_api_url', 'http://some-team-edition-url.com')
        hide_dialogs_config.set('main', 'logged_api_url', 'http://some-team-edition-url.com')
        hide_dialogs_config.set('main', 'logged_brand', TEAM_EDITION_BRAND)
        widget = MainWindow(config=hide_dialogs_config)

        assert (TEAM_EDITION_BRAND, "http://some-team-edition-url.com/channels/") == widget.get_logged_api_label_url()

    def test_get_logged_anaconda_label(self, hide_dialogs_config):
        hide_dialogs_config.set('main', 'logged_api_url', 'http://not_anaconda.org')
        hide_dialogs_config.set('main', 'logged_brand', ANACONDA_ORG_BRAND)
        widget = MainWindow(config=hide_dialogs_config)

        assert (ANACONDA_ORG_BRAND, 'https://anaconda.org') == widget.get_logged_api_label_url()

    def test_get_logged_label_failed(self, hide_dialogs_config):
        hide_dialogs_config.set('main', 'enterprise_4_repo_api_url', 'http://api.some-repo4-url.com/api')
        hide_dialogs_config.set('main', 'logged_api_url', 'http://api.some-repo4-url.com/api')
        hide_dialogs_config.set('main', 'logged_brand', ENTERPRISE_EDITION_BRAND)
        widget = MainWindow(config=hide_dialogs_config)
        expected_user_link = 'http://some-repo4-url.com'

        widget.update_login_status({'login': TEST_USER})
        assert (ENTERPRISE_EDITION_BRAND, expected_user_link) == widget.get_logged_api_label_url()

        widget.config.set('main', 'logged_api_url', None)
        widget.config.set('main', 'logged_brand', None)
        assert widget.get_logged_api_label_url() == (ANACONDA_ORG_BRAND, 'https://anaconda.org')
