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

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

# Local imports
from anaconda_navigator.utils.fixtures import tmpconfig
from anaconda_navigator.widgets.dialogs.quit import (ClosePackageManagerDialog,
                                                     QuitApplicationDialog)


# --- Fixtures
# -----------------------------------------------------------------------------
@pytest.fixture
def botquittrue(qtbot, tmpconfig):
    tmpconfig.set('main', 'hide_quit_dialog', True)
    widget = QuitApplicationDialog(config=tmpconfig)
    widget.show()
    qtbot.addWidget(widget)
    return qtbot, widget


@pytest.fixture
def botquitfalse(qtbot, tmpconfig):
    tmpconfig.set('main', 'hide_quit_dialog', False)
    widget = QuitApplicationDialog(config=tmpconfig)
    widget.show()
    qtbot.addWidget(widget)
    return qtbot, widget


@pytest.fixture
def closecondawidget(qtbot):
    widget = ClosePackageManagerDialog()
    widget.show()
    qtbot.addWidget(widget)
    return qtbot, widget


# --- Tests
# -----------------------------------------------------------------------------
class TestQuitDialog:

    def test_hide(self, botquitfalse):
        qtbot, widget = botquitfalse
        assert widget.checkbox.checkState() == Qt.Unchecked

    def test_show(self, botquittrue):
        qtbot, widget = botquittrue
        assert widget.checkbox.checkState() == Qt.Checked

    def test_accept(self, botquitfalse):
        qtbot, widget = botquitfalse
        qtbot.mouseClick(widget.checkbox, Qt.LeftButton)
        widget.accept()
        assert widget.config.get('main', 'hide_quit_dialog')

    def test_reject(self, botquittrue):
        qtbot, widget = botquittrue
        qtbot.mouseClick(widget.checkbox, Qt.LeftButton)
        widget.reject()
        assert widget.config.get('main', 'hide_quit_dialog')


class TestCloseCondaDialog:

    def test_ok(self, closecondawidget):
        qtbot, widget = closecondawidget
        with qtbot.waitSignal(widget.accepted, 1000, True):
            qtbot.mouseClick(widget.button_ok, Qt.LeftButton)

    def test_cancel(self, closecondawidget):
        qtbot, widget = closecondawidget
        with qtbot.waitSignal(widget.rejected, 1000, True):
            qtbot.mouseClick(widget.button_cancel, Qt.LeftButton)
