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

"""
"""

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

# Local imports
from anaconda_navigator import images
from anaconda_navigator.config import CONF
from anaconda_navigator.utils.styles import load_style_sheet
from anaconda_navigator.widgets import ButtonCancel
from anaconda_navigator.widgets.dialogs import DialogBase


class QuitApplicationDialog(DialogBase):
    """
    """
    def __init__(self, *args, **kwargs):
        super(QuitApplicationDialog, self).__init__(*args, **kwargs)
        self.label_icon = QLabel()
        self.label_about = QLabel('Quit Anaconda Navigator?')
        self.button_ok = QPushButton('Yes')
        self.button_cancel = ButtonCancel('No')
        self.buttonbox = QDialogButtonBox(Qt.Horizontal)
        self.checkbox = QCheckBox("Don't show again")

        # Widget setup
        if CONF.get('main', 'hide_quit_dialog'):
            hide_dialog = Qt.Checked
        else:
            hide_dialog = Qt.Unchecked

        self.checkbox.setChecked(hide_dialog)
        self.buttonbox.addButton(self.button_ok, QDialogButtonBox.ActionRole)
        self.buttonbox.addButton(self.button_cancel,
                                 QDialogButtonBox.ActionRole)
        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.addSpacing(10)
        h_layout.addWidget(self.label_about)

        main_layout = QVBoxLayout()
        main_layout.addLayout(h_layout)
        main_layout.addWidget(self.checkbox, 0, Qt.AlignRight)
        main_layout.addSpacing(24)
        main_layout.addWidget(self.buttonbox)
        self.setLayout(main_layout)

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

        # Setup
        self.update_style_sheet()

    def accept(self):
        hide_dialog = self.checkbox.checkState() == Qt.Checked
        CONF.set('main', 'hide_quit_dialog', hide_dialog)
        super(QuitApplicationDialog, self).accept()


class ClosePackageManagerDialog(QuitApplicationDialog):
    def __init__(self, *args, **kwargs):
        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)


def test():
    from anaconda_navigator.utils.qthelpers import qapplication
    app = qapplication()
    dlg = QuitApplicationDialog(parent=None)
#    dlg = ClosePackageManagerDialog(parent=None)
    reply = dlg.exec_()
    print(dlg.checkbox.checkState() == Qt.Checked)
    print(reply)


if __name__ == '__main__':
    test()
