#!/usr/bin/env python3
#
# Copyright (C) 2020 IOFactory
#
# Author: Luis Galdos <luis.galdos@iofactory.es>
#

from PyQt5.QtWidgets import QFrame
from PyQt5.QtCore import Qt, QTimer, pyqtSlot
from PyQt5.QtGui import QIcon

from ui.ui_buttons import Ui_Buttons

from .about import AboutDialog

from functools import partial


class Buttons(QFrame, Ui_Buttons):
    def __init__(self, parent):
        QFrame.__init__(self, parent)
        Ui_Buttons.setupUi(self, self)
        self.tabWidget.tabBarClicked.connect(self.__clicked)
        self.setFixedSize(self.width(), self.height())
        self.tabWidget.tabBar().setFixedWidth(self.width())

        self.OpenButton.clicked.connect(partial(self.__action, parent.open_dialog))
        self.AboutButton.clicked.connect(partial(self.__action, self.__about))
        self.CloseButton.clicked.connect(partial(self.__action, parent.close_database))
        self.SettingsButton.clicked.connect(partial(self.__action, parent.open_settings))

        self.__timer = QTimer(self)
        self.__timer.setInterval(3000)
        self.__timer.timeout.connect(self.__hide)
        self.__hide()

    def enterEvent(self, ev):
        self.__timer.stop()
        self.__show()
        super().enterEvent(ev)

    def leaveEvent(self, ev):
        self.__timer.start()
        super().leaveEvent(ev)

    @pyqtSlot(int)
    def __clicked(self, idx):
        #self.__show()
        pass

    @pyqtSlot()
    def __hide(self):
        self.__timer.stop()
        y = self.parent().height() - self.tabWidget.tabBar().height() - 4
        self.move(20, y)

    @pyqtSlot()
    def __show(self):
        y = self.parent().height() - self.height() # - self.tabWidget.tabBar().height()
        self.move(20, y)

    @pyqtSlot()
    def __about(self):
        dlg = AboutDialog(self.parent())
        dlg.exec()

    @pyqtSlot(object)
    def __action(self, slot):
        self.__hide()
        slot()
