# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
"""Tests for login dialog."""

# Standard library imports
import os

# Third party imports
from flaky import flaky
from qtpy.QtCore import Qt  # analysis:ignore
import pytest
import pytestqt.qtbot as qtbot  # analysis:ignore

# Local imports
from anaconda_navigator.api.anaconda_api import AnacondaAPI
from anaconda_navigator.utils.fixtures import tmpconfig
from anaconda_navigator.widgets.dialogs.login import AuthenticationDialog

NAVIGATOR_TEST_EMAIL = os.environ.get('NAVIGATOR_TEST_EMAIL')
NAVIGATOR_TEST_USERNAME = os.environ.get('NAVIGATOR_TEST_USERNAME')
NAVIGATOR_TEST_PASSWORD = os.environ.get('NAVIGATOR_TEST_PASSWORD')

xfail = pytest.mark.xfail
skipif = pytest.mark.skipif


# --- Fixtures
# -----------------------------------------------------------------------------
@pytest.fixture
def botlogin(qtbot, tmpconfig):
    tmpconfig.set('main', 'anaconda_api_url', 'https://api.anaconda.org/')
    widget = AuthenticationDialog(api=AnacondaAPI())
    widget.config = tmpconfig  # Patch with a temporal config file
    widget.setup()
    qtbot.addWidget(widget)
    widget.show()
    return qtbot, widget


# --- Tests
# -----------------------------------------------------------------------------
def test_login_button_username_filled(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_username, NAVIGATOR_TEST_USERNAME)
    widget.text_password.setText('')
    assert not widget.button_login.isEnabled()


def test_login_button_password_filled(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_password, NAVIGATOR_TEST_PASSWORD)
    widget.text_username.setText('')
    assert not widget.button_login.isEnabled()


def test_login_button_filled(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_username, 'randomuserstring')
    qtbot.keyClicks(widget.text_password, 'randomuserstringpassword')
    assert widget.button_login.isEnabled()


def test_username_text_valid(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_username, '123456test@#$')
    assert widget.username == '123456test'

    widget.text_username.setText('')
    qtbot.keyClicks(widget.text_username, 'alpha-alpha_123456789')
    assert widget.username == 'alpha-alpha_123456789'


@skipif(
    NAVIGATOR_TEST_PASSWORD is None,
    reason='User and password are available as env variables on CI'
)
@flaky(max_runs=4, min_passes=1)
def test_username_invalid(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_username, 'hopefully-not-an-existing-user')
    qtbot.keyClicks(widget.text_password, NAVIGATOR_TEST_PASSWORD)
    with qtbot.waitSignal(
        signal=widget.sig_authentication_failed, timeout=5000, raising=True
    ):
        qtbot.mouseClick(widget.button_login, Qt.LeftButton)
    assert widget.isVisible()


@skipif(
    NAVIGATOR_TEST_PASSWORD is None,
    reason='User and password are available as env variables on CI'
)
@flaky(max_runs=4, min_passes=1)
def test_username_valid_password_valid(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_username, NAVIGATOR_TEST_USERNAME)
    qtbot.keyClicks(widget.text_password, NAVIGATOR_TEST_PASSWORD)
    with qtbot.waitSignal(
        signal=widget.sig_authentication_succeeded, timeout=5000, raising=True
    ):
        qtbot.mouseClick(widget.button_login, Qt.LeftButton)
    assert not widget.isVisible()


def test_username_valid_password_invalid(botlogin):
    qtbot, widget = botlogin
    qtbot.keyClicks(widget.text_username, 'goanpeca')
    qtbot.keyClicks(widget.text_password, 'wrongpassword')
    with qtbot.waitSignal(
        signal=widget.sig_authentication_failed, timeout=5000, raising=True
    ):
        qtbot.mouseClick(widget.button_login, Qt.LeftButton)
    assert widget.isVisible()


def test_forgot_username_link(botlogin):
    qtbot, widget = botlogin
    with qtbot.waitSignal(
        signal=widget.sig_url_clicked, timeout=5000, raising=True
    ):
        qtbot.mouseClick(widget.button_forgot_username, Qt.LeftButton)
    assert widget.isVisible()


def test_forgot_password_link(botlogin):
    qtbot, widget = botlogin
    with qtbot.waitSignal(
        signal=widget.sig_url_clicked, timeout=5000, raising=True
    ):
        qtbot.mouseClick(widget.button_forgot_password, Qt.LeftButton)
    assert widget.isVisible()


def test_register_link(botlogin):
    qtbot, widget = botlogin
    with qtbot.waitSignal(
        signal=widget.sig_url_clicked, timeout=5000, raising=True
    ):
        qtbot.mouseClick(widget.button_register, Qt.LeftButton)
    assert widget.isVisible()
