# -*- 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.
"""

# Third party imports
from qtpy.QtCore import Qt  # Its important to import something from qtpy first
from pytestqt import qtbot
import pytest

# 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 = 'gpena-castellanos+testusage@continuum.io'
NAVIGATOR_TEST_USERNAME = 'navigatortestuser'
NAVIGATOR_TEST_PASSWORD = 'testuser12345678rocks'


# --- 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, NAVIGATOR_TEST_USERNAME)
    qtbot.keyClicks(widget.text_password, NAVIGATOR_TEST_PASSWORD)
    assert widget.button_login.isEnabled()


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

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


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=1000, raising=True):
        qtbot.mouseClick(widget.button_login, Qt.LeftButton)
    assert widget.isVisible()


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=1000, 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, NAVIGATOR_TEST_USERNAME)
    qtbot.keyClicks(widget.text_password, 'wrongpassword')
    with qtbot.waitSignal(signal=widget.sig_authentication_failed,
                          timeout=1000, 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=1000, 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=1000, 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=1000, raising=True):
        qtbot.mouseClick(widget.button_register, Qt.LeftButton)
    assert widget.isVisible()
