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

# Third party imports
from qtpy.QtCore import Qt, QUrl, Signal
from qtpy.QtGui import QPixmap, QDesktopServices
from qtpy.QtWidgets import (QHBoxLayout, QLabel, QVBoxLayout)

# Local imports
from anaconda_navigator import __version__
from anaconda_navigator import images
from anaconda_navigator.utils.analytics import GATracker
from anaconda_navigator.utils.styles import load_style_sheet
from anaconda_navigator.widgets import (ButtonLink, ButtonLabel, ButtonNormal,
                                        SpacerVertical, SpacerHorizontal)
from anaconda_navigator.widgets.dialogs import DialogBase


class AboutDialog(DialogBase):
    """About dialog."""

    GITHUB_URL = 'https://github.com/ContinuumIO/anaconda-issues/issues'
    sig_url_clicked = Signal(str)

    def __init__(self, *args, **kwargs):
        """About dialog."""
        super(AboutDialog, self).__init__(*args, **kwargs)
        self.tracker = GATracker()
        text = """<b>Anaconda Navigator {version}</b><br>
            <br>Copyright &copy; 2016 Continuum Analytics
            <p>Created by Continuum Analytics
            <br>
            <p>For bug reports and feature requests, please visit our
            """.format(version=__version__)
        self.label_icon = QLabel()
        self.label_about = QLabel(text)
        self.button_link = ButtonLink('Issue Tracker')
        self.button_label = ButtonLabel('on GitHub.')
        self.button_ok = ButtonNormal('Ok')

        # Widget setup
        self.button_ok.setMinimumWidth(70)
        self.button_ok.setDefault(True)
        self.label_icon.setPixmap(QPixmap(images.ANACONDA_ICON_64_PATH))
        self.setWindowTitle("About Anaconda Navigator")

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

        content_layout = QVBoxLayout()
        content_layout.addWidget(self.label_about, 0, Qt.AlignBottom)
        h_content_layout = QHBoxLayout()
        h_content_layout.addWidget(self.button_link, 0, Qt.AlignLeft)
        h_content_layout.addWidget(self.button_label, 0, Qt.AlignLeft)
        h_content_layout.addStretch(0)

        content_layout.addLayout(h_content_layout)
        h_layout.addLayout(content_layout)

        buttons_layout = QHBoxLayout()
        buttons_layout.addStretch()
        buttons_layout.addWidget(self.button_ok)

        main_layout = QVBoxLayout()
        main_layout.addLayout(h_layout)
        main_layout.addWidget(SpacerVertical())
        main_layout.addWidget(SpacerVertical())
        main_layout.addLayout(buttons_layout)
        self.setLayout(main_layout)

        # Signals
        self.button_ok.clicked.connect(self.accept)
        self.button_link.clicked.connect(
            lambda: self.open_url(self.GITHUB_URL))

        self.button_ok.setFocus()

    def open_url(self, url):
        """Open url in default webbrowser and track event."""
        self.tracker.track_event('content', 'click', url)
        QDesktopServices.openUrl(QUrl(url))
        self.sig_url_clicked.emit(url)


# --- Local testing
# -----------------------------------------------------------------------------
def test():    # pragma: no cover
    """Run local test."""
    from anaconda_navigator.utils.qthelpers import qapplication

    app = qapplication()
    widget = AboutDialog(parent=None)
    widget.setStyleSheet(load_style_sheet())
    widget.show()
    app.exec_()


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