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

"""
Wigets module.
"""

# Third party imports
from qtpy.QtCore import Qt, QUrl
from qtpy.QtGui import QPainter, QDesktopServices
from qtpy.QtWidgets import (QStyle, QFrame, QLabel, QListWidget, QPushButton,
                            QStyleOption, QToolButton,
                            QWidget
                            )

# Local imports
from anaconda_navigator.utils.qthelpers import update_pointer
from anaconda_navigator.utils.analytics import GATracker
from anaconda_navigator.widgets.helperwidgets import (MessageCheckBox,
                                                      EditableLineEdit,
                                                      EditablePathEdit,
                                                      LineEditSearch,
                                                      )


class WidgetBase(QWidget):
    def setProperty(self, name, value):
        QWidget.setProperty(self, name, value)
        self.style().unpolish(self)
        self.style().polish(self)
        self.update()

    def paintEvent(self, event):
        """
        Needed to be able to use stylesheets on custom widgets that subclass
        QWidget.
        """
        QWidget.paintEvent(self, event)
        opt = QStyleOption()
        opt.initFrom(self)
        p = QPainter(self)
        self.style().drawPrimitive(QStyle.PE_Widget, opt, p, self)


# --- Buttons
# -----------------------------------------------------------------------------
class ButtonPrimaryAction(QPushButton):
    pass


class ButtonCancel(QPushButton):
    pass


class ButtonSecondaryTextual(QPushButton):
    pass


class ButtonSecondaryIcon(QPushButton):
    pass


class ButtonEnvironmentCancel(QPushButton):
    pass


class ButtonEnvironmentPrimary(QPushButton):
    pass


class ButtonIcon(QPushButton):
    pass


class ButtonTab(QToolButton):
    def setProperty(self, name, value):
        super(ButtonTab, self).setProperty(name, value)
        self.style().unpolish(self)
        self.style().polish(self)
        self.update()

    def keyPressEvent(self, event):
        key = event.key()
        if key in [Qt.Key_Enter, Qt.Key_Return]:
            self.animateClick()
        super(ButtonTab, self).keyPressEvent(event)


class ButtonBase(QPushButton):
    def __init__(self, *args, **kwargs):
        super(ButtonBase, self).__init__(*args, **kwargs)
        self.setDefault(True)

    def focusInEvent(self, event):
        super(ButtonBase, self).focusInEvent(event)
        self.setProperty('_focus', True)

    def focusOutEvent(self, event):
        super(ButtonBase, self).focusOutEvent(event)
        self.setProperty('_focus', False)

    def enterEvent(self, event):
        super(ButtonBase, self).enterEvent(event)
        self.setProperty('_hover', True)

    def setDisabled(self, value):
        super(ButtonBase, self).setDisabled(value)
        self.setProperty('_disabled', value)

    def setEnabled(self, value):
        super(ButtonBase, self).setEnabled(value)
        self.setProperty('_disabled', not value)

    def leaveEvent(self, event):
        super(ButtonBase, self).leaveEvent(event)
        self.setProperty('_hover', False)

    def setProperty(self, name, value):
        super(ButtonBase, self).setProperty(name, value)
        self.style().unpolish(self)
        self.style().polish(self)
        self.update()


# --- Buttons that are used as labels.
class ButtonLink(QPushButton):
    """
    A button use to represent a clickable (and keyboard focusable) web link.
    It is styled to be used as a label.
    """
    def enterEvent(self, event):
        super(ButtonLink, self).enterEvent(event)
        update_pointer(Qt.PointingHandCursor)

    def leaveEvent(self, event):
        super(ButtonLink, self).leaveEvent(event)
        update_pointer()


class ButtonLabel(QPushButton):
    """
    A button that is used next to ButtonLink to avoid missalignments in
    different platforms. It looks and acts like a label.
    """
    def __init__(self, *args, **kwargs):
        super(ButtonLabel, self).__init__(*args, **kwargs)
        self.setDisabled(True)
        self.setFocusPolicy(Qt.NoFocus)


class ButtonEnvironmentOptions(ButtonBase):
    pass


class FrameCommunity(QFrame):
    pass


class FrameEnvironments(QFrame):
    pass


class FrameEnvironmentsList(QFrame):
    pass


class FrameEnvironmentsListButtons(QFrame):
    pass


class FrameEnvironmentsPackages(QFrame):
    pass


# --- Labels
# -----------------------------------------------------------------------------
class LabelLinks(QLabel):
    LINKS_STYLE = """<style>
    a {
        color:green;
        text-decoration: underline;
    }
    </style>
    """

    def __init__(self, *args, **kwargs):
        super(LabelLinks, self).__init__(*args, **kwargs)
        self.setOpenExternalLinks(False)
        self.linkActivated.connect(self._link_activated)
        self._original_text = self.text()
        self._add_style()

    def _add_style(self):
        text = self._original_text
        if self.LINKS_STYLE not in text:
            self.setText(self.LINKS_STYLE + text)

    def _link_activated(self, url):
        QDesktopServices.openUrl(QUrl(url))
        tracker = GATracker()
        tracker.track_event('content', 'link', url)

    def setText(self, text):
        self._original_text = text
        super(LabelLinks, self).setText(text)
        self._add_style()


# --- Other
# -----------------------------------------------------------------------------
class ItemWidgetVideoCard(QFrame):
    pass


class ProjectsListWidget(QListWidget):
    pass


class ListWidgetPrimary(QListWidget):
    pass
