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

# Third party imports
from qtpy.QtCore import Qt
from qtpy.QtGui import QPixmap
from qtpy.QtWidgets import (QCheckBox, QDialogButtonBox, QHBoxLayout, QLabel,
                            QVBoxLayout)

# Local imports
from anaconda_navigator import images
from anaconda_navigator.config import CONF
from anaconda_navigator.widgets import (ButtonNormal, ButtonPrimary,
                                        SpacerVertical, SpacerHorizontal)
from anaconda_navigator.widgets.dialogs import DialogBase


class QuitApplicationDialog(DialogBase):
    """Quit application confirmation dialog."""

    def __init__(self, config=CONF, **kwargs):
        """Quit application confirmation dialog."""
        super(QuitApplicationDialog, self).__init__(**kwargs)
        self.label_icon = QLabel()
        self.label_about = QLabel('Quit Anaconda Navigator?')
        self.button_ok = ButtonPrimary('Yes')
        self.button_cancel = ButtonNormal('No')
        self.buttonbox = QDialogButtonBox(Qt.Horizontal)
        self.checkbox = QCheckBox("Don't show again")
        self.config = config

        self.label_icon.setPixmap(QPixmap(images.ANACONDA_ICON_64_PATH))
        self.setWindowTitle("Quit application")

        # Layouts
        h_layout = QHBoxLayout()
        h_layout.addWidget(self.label_icon, 0, Qt.AlignTop)
        h_layout.addWidget(SpacerHorizontal())
        h_layout.addWidget(self.label_about)

        layout_buttons = QHBoxLayout()
        layout_buttons.addStretch()
        layout_buttons.addWidget(self.button_cancel)
        layout_buttons.addWidget(SpacerHorizontal())
        layout_buttons.addWidget(self.button_ok)

        main_layout = QVBoxLayout()
        main_layout.addLayout(h_layout)
        main_layout.addWidget(self.checkbox, 0, Qt.AlignRight)
        main_layout.addWidget(SpacerVertical())
        main_layout.addWidget(SpacerVertical())
        main_layout.addLayout(layout_buttons)
        self.setLayout(main_layout)

        # Signals
        self.button_ok.clicked.connect(self.accept)
        self.button_cancel.clicked.connect(self.reject)

        # Setup
        self.update_style_sheet()
        self.setup()
        self.button_cancel.setFocus()

    def setup(self):
        """Setup widget content."""
        # Widget setup
        if self.config.get('main', 'hide_quit_dialog'):
            hide_dialog = Qt.Checked
        else:
            hide_dialog = Qt.Unchecked
        self.checkbox.setChecked(hide_dialog)

    def accept(self):
        """
        Qt overide.

        Update the configuration preferences.
        """
        hide_dialog = self.checkbox.checkState() == Qt.Checked
        self.config.set('main', 'hide_quit_dialog', hide_dialog)
        super(QuitApplicationDialog, self).accept()


class ClosePackageManagerDialog(QuitApplicationDialog):
    """Confirm application quit if package manager is running."""

    def __init__(self, *args, **kwargs):
        """Confirm application quit if package manager is running."""
        super(ClosePackageManagerDialog, self).__init__(*args, **kwargs)

        self.label_about.setText('Conda is still busy.\n\n'
                                 'Do you want to cancel the process?')
        self.setWindowTitle('Cancel Process')
        self.checkbox.setVisible(False)


# --- Local testing
# -----------------------------------------------------------------------------
def test():   # pragma: no cover
    """Run local test."""
    from anaconda_navigator.utils.qthelpers import qapplication
    app = qapplication()
    widget = QuitApplicationDialog(parent=None)
    widget.show()
    app.exec_()


if __name__ == '__main__':   # pragma: no cover
    test()
