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

from PyQt5.QtCore import pyqtSlot, QObject
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QLabel

from .about import AboutDialog


class MainMenubar(QObject):
    def __init__(self, window, menubar, toolbar):
        QObject.__init__(self)
        self.window = window
        window.ActionAbout.triggered.connect(self.__about)
        self.__createToolbar(window, toolbar)

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

    def __createToolbar(self, window, bar):
        items = [
            [ ':/images/document-open', 'Open database', True , window.open_dialog ],
            [ ':/images/window-close', 'Close database', True , window.close_database ],
            [ ':/images/dialog-password', 'Keys', True , window.show_keys ],
            [ ':/images/settings', 'Settings', True , window.open_settings ],
            [ '' ], # separator
            [ ':/images/gtk-about', 'About', True , self.__about ],
            [ '' ], # separator
            [ ':/images/exit', 'Exit', True, window.close_program ],
        ]

        self.__actions = { }
        for item in items:
            if 0 == len(item[0]):
                bar.addSeparator()
                continue
            icon = QIcon.fromTheme(item[0])
            action = bar.addAction(icon, item[1])
            action.setEnabled(item[2])
            action.triggered.connect(item[3])
            self.__actions[item[1]] = action


class StatusBar(QObject):
    def __init__(self, panel, bar, *args, **kwargs):
        self.bar = bar
        super().__init__(*args, **kwargs)
        panel.signalDatabase.connect(self._database)

        self.database = QLabel("")
        self.bar.addPermanentWidget(self.database)
        self.bar.showMessage('Ready')

    @pyqtSlot()
    def show(self, msg):
        self.bar.showMessage(msg)

    def _database(self, db):
        self.database.setText(db)
