# -*- 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 the license manager dialog."""

# Standard library imports
import glob
import os

# Third party imports
from qtpy.QtCore import QMimeData, Qt, QTimer, QUrl  # analysis:ignore
from qtpy.QtGui import QDropEvent  # analysis:ignore
import pytest
import pytestqt.qtbot as qtbot  # analysis:ignore

# Local imports
from anaconda_navigator.utils.fixtures import tmpfolder
from anaconda_navigator.widgets.dialogs.license import LicenseManagerDialog

HERE = os.path.dirname(os.path.abspath(__file__))
EXPIRED_LICENSE_PATH = os.path.join(
    HERE, 'data', 'license_bundle_20160829215841_expired.txt'
)
xfail = pytest.mark.xfail
skipif = pytest.mark.skipif


# --- Helpers
# -----------------------------------------------------------------------------
def drop_item(widget, mime_data):
    """Emulate a drop event on widget."""
    action = Qt.CopyAction | Qt.MoveAction
    pt = widget.rect().center()
    drag_drop = QDropEvent(pt, action, mime_data, Qt.LeftButton, Qt.NoModifier)
    drag_drop.acceptProposedAction()
    widget.dropEvent(drag_drop)


def patch_license_paths(path=None):
    # return paths to the license files
    paths = []
    if tmpfolder:
        paths = glob.glob(os.path.join(path, 'license*.txt'))
    return paths


# --- Fixtures
# -----------------------------------------------------------------------------
@pytest.fixture
def botlicense(qtbot, tmpfolder):
    # Patch license path to a temporal location
    LicenseManagerDialog.license_paths = lambda: patch_license_paths(
        path=tmpfolder
    )
    LicenseManagerDialog.license_location = lambda: tmpfolder
    widget = LicenseManagerDialog()
    qtbot.addWidget(widget)
    widget.show()
    return qtbot, widget


# --- Tests
# -----------------------------------------------------------------------------
def test_license_button_contact(botlicense):
    qtbot, widget = botlicense
    with qtbot.waitSignal(
        signal=widget.sig_url_clicked, timeout=2000, raising=True
    ):
        qtbot.mouseClick(widget.button_contact, Qt.LeftButton)


def test_license_button_close(botlicense):
    qtbot, widget = botlicense
    with qtbot.waitSignal(signal=widget.accepted, timeout=1000, raising=True):
        qtbot.mouseClick(widget.button_close, Qt.LeftButton)


def test_license_add(botlicense):
    qtbot, widget = botlicense
    widget.add_license(path=EXPIRED_LICENSE_PATH)
    assert bool(widget.count())
    assert widget.count() == 5


def test_license_remove_accept(botlicense):
    qtbot, widget = botlicense

    def interact():
        qtbot.mouseClick(widget.message_box.button_remove, Qt.LeftButton)

    timer = QTimer()
    timer.singleShot(1000, interact)
    timer.start()
    widget.add_license(path=EXPIRED_LICENSE_PATH)
    widget.remove_license(0)

    assert bool(widget.count())
    assert widget.count() == 4


def test_license_remove_reject(botlicense):
    qtbot, widget = botlicense

    def interact():
        qtbot.mouseClick(widget.message_box.button_cancel, Qt.LeftButton)

    timer = QTimer()
    timer.singleShot(1000, interact)
    timer.start()
    widget.add_license(path=EXPIRED_LICENSE_PATH)
    widget.remove_license(0)

    assert bool(widget.count())
    assert widget.count() == 5


@xfail
def test_license_add_drop(botlicense):
    qtbot, widget = botlicense
    mime_data = QMimeData()
    mime_data.setUrls([QUrl(EXPIRED_LICENSE_PATH)])
    drop_item(widget.table, mime_data)

    assert bool(widget.count())
    assert widget.count() == 5
