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

"""
Custom tab widget with custom tabbar.
"""

# Third party imports
from qtawesome import icon as qta
from qtpy.QtCore import Qt, Signal
from qtpy.QtWidgets import (QLabel, QHBoxLayout,  QStackedWidget,
                            QVBoxLayout, QWidget)

# Local imports
from anaconda_navigator.widgets import ButtonTab


class StackBody(QStackedWidget):
    pass


class TabBarBody(QWidget):
    """
    Custom QTabBar that includes centered icons and text bellow the icon.
    """
    sig_index_changed = Signal(int)

    def __init__(self, *args, **kwargs):
        super(TabBarBody, self).__init__(*args, **kwargs)
        self.buttons = []

        self.main_layout = QVBoxLayout()
        self.main_layout.setContentsMargins(0, 0, 0, 0)
        self.main_layout.setSpacing(0)
        self.setLayout(self.main_layout)

    def add_tab(self, text, icon=None):
        """
        Create the widget that replaces the normal tab content.
        """
#        button = ButtonTab()  # If a pushbutton
        button = ButtonTab()
        button.setCheckable(True)
        button.setText(text)
        button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        button.setObjectName(text.lower())

        if icon:
            button.setIcon(icon)

        self.main_layout.addWidget(button)
        self.buttons.append(button)
        index = self.buttons.index(button)
        button.clicked.connect(lambda b=button, i=index:
                               self.refresh(button, index))

    def refresh(self, button, index):
        for b in self.buttons:
            b.setChecked(False)
            b.setProperty('checked', False)

        button.setChecked(True)
        button.setProperty('checked', True)

        self.sig_index_changed.emit(index)
        self.current_index = index


class TabWidgetBody(QWidget):
    """
    Curstom Tab Widget that includes a more customizable "tabbar".
    """
    currentChanged = Signal(int)

    def __init__(self, *args, **kwargs):
        super(TabWidgetBody, self).__init__(*args, **kwargs)
        self.stack = StackBody()
        self.tabbar = TabBarBody()

        vlayout = QVBoxLayout()
        vlayout.addWidget(self.tabbar)
        vlayout.addStretch()
        vlayout.setContentsMargins(0, 0, 0, 0)
        vlayout.setSpacing(0)

        hlayout = QHBoxLayout()
        hlayout.addLayout(vlayout)
        hlayout.addWidget(self.stack)
        hlayout.setContentsMargins(0, 0, 0, 0)
        hlayout.setSpacing(0)

        self.setLayout(hlayout)
        self.tabbar.sig_index_changed.connect(self.setCurrentIndex)

    def currentIndex(self):
        return self.tabbar.current_index

    def setCurrentIndex(self, index):
        self.tabbar.current_index = index
        self.tabbar.buttons[index].setChecked(True)
        self.tabbar.buttons[index].setFocus()
        self.stack.setCurrentIndex(index)
        self.currentChanged.emit(index)

    def currentText(self):
        return self.tabbar.buttons[self.currentIndex()].text()

    def addTab(self, widget, icon=None, text=''):
        """
        Qt override.
        """
        self.tabbar.add_tab(text, icon)
        self.stack.addWidget(widget)
        self.setCurrentIndex(0)


def test():
    from anaconda_navigator.utils.qthelpers import qapplication
    app = qapplication(test_time=3)
    w = TabWidgetBody()
    w.addTab(QLabel('HELLO 1'), text='Home',
             icon=qta('fa.home', color_active="#0000FF"))
    w.addTab(QLabel('HELLO 2'), text='Add',
             icon=qta('fa.plus', color_active="#FF0000"))
    w.showMaximized()
    app.exec_()


if __name__ == '__main__':
    test()
