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

"""
Splash screen for Anaconda Navigator and intial startup splash.
"""

# Standard library imports
from __future__ import absolute_import, division, print_function

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

# Local imports
from anaconda_navigator.config import CONF
from anaconda_navigator.images import (ANACONDA_ICON_256_PATH,
                                       ANACONDA_NAVIGATOR_LOGO)
from anaconda_navigator.utils.qthelpers import qapplication
from anaconda_navigator.widgets.dialogs import DialogBase


class SplashScreen(QSplashScreen):
    """
    Splash screen for the main window
    """
    def __init__(self, *args, **kwargs):
        super(SplashScreen, self).__init__(*args, **kwargs)

        self._effect = QGraphicsOpacityEffect()
        self._font = self.font()
        self._pixmap = QPixmap(ANACONDA_ICON_256_PATH)

        # Setup
        self._font.setPixelSize(10)
        self._effect.setOpacity(0.9)
        self.setFont(self._font)
        self.setGraphicsEffect(self._effect)
        self.setPixmap(self._pixmap)
        self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint |
                            Qt.FramelessWindowHint)

    def show_message(self, message):
        """
        Show message in the screen.
        """
        message += '\n'
        self.show()
        self.showMessage(message, Qt.AlignBottom | Qt.AlignCenter |
                         Qt.AlignAbsolute, QColor(Qt.white))
        QApplication.processEvents()


class FirstSplash(DialogBase):
    """
    Startup splash to display the first time that Navigator runs.
    """
    def __init__(self, parent=None, analytics=True):
        super(FirstSplash, self).__init__()

        text = """
        Thanks for installing Anaconda!

        Anaconda Navigator helps you easily start important Python applications
        and manage the packages in your local Anaconda installation. It also
        connects you to online resources for learning and engaging with the
        Python, SciPy, and PyData community.

        To help us improve Anaconda Navigator, fix bugs, and make it even easier
        for everyone to use Python, we gather anonymized usage information,
        just like most web browsers and mobile apps.

        To opt out of this, please uncheck below (You can always change this
        setting in the Preferences menu).
        """

        # Widgets
        self.button_ok = QPushButton('Ok')
        self.button_ok_dont_show = QPushButton("Ok, and don't show again")
        self.buttonbox = QDialogButtonBox(Qt.Horizontal)
        self.checkbox_track = QCheckBox("Yes, I'd like to help improve "
                                        "Anaconda.")
        self.label_about = QLabel(text)
        self.label_icon = QLabel()

        # Widget setup
        self.buttonbox.addButton(self.button_ok, QDialogButtonBox.ActionRole)
        self.buttonbox.addButton(self.button_ok_dont_show,
                                 QDialogButtonBox.ActionRole)
        self.checkbox_track.setChecked(analytics)
        self.checkbox_track.setChecked(CONF.get('main', 'provide_analytics',
                                                True))
        self.frame_title_bar.hide()
        self.label_icon.setPixmap(QPixmap(ANACONDA_NAVIGATOR_LOGO))

        # Layouts
        layout = QVBoxLayout()
        layout.addWidget(self.label_icon, 0, Qt.AlignCenter)
        layout.addWidget(self.label_about)
        layout.addWidget(self.checkbox_track, 0, Qt.AlignCenter)
        layout.addSpacing(20)
        layout.addWidget(self.buttonbox)
        self.setLayout(layout)

        # Signals
        self.button_ok.clicked.connect(lambda: self.accept(show_startup=True))
        self.button_ok_dont_show.clicked.connect(
            lambda: self.accept(show_startup=False))

    def accept(self, show_startup):
        """
        Qt override.
        """
        CONF.set('main', 'provide_analytics',
                 self.checkbox_track.checkState() == Qt.Checked)
        CONF.set('main', 'show_startup', show_startup)
        DialogBase.accept(self)

    def reject(self):
        """
        Qt override.

        Force user to select one of the two options bellow and disalow
        canceling the dialog (pressing escape)
        """
        pass


def test():
    app = qapplication()
    w1 = SplashScreen()
    w1.show_message('Initializing...')
    w1.show()
    w2 = FirstSplash()
    w2.show()
    w2.update_style_sheet()
    app.exec_()


if __name__ == '__main__':
    test()
