"""
Provides the graphical front-end for Piket.
"""
import collections
import logging
import os
import sys

# pylint: disable=E0611
from PySide2.QtWidgets import (
    QAction,
    QActionGroup,
    QApplication,
    QGridLayout,
    QInputDialog,
    QMainWindow,
    QMessageBox,
    QPushButton,
    QSizePolicy,
    QToolBar,
    QWidget,
)
from PySide2.QtGui import QIcon
from PySide2.QtCore import QObject, QSize, Qt, Signal, Slot

# pylint: enable=E0611

try:
    import dbus
except ImportError:
    dbus = None

from piket_client.sound import PLOP_WAVE, UNDO_WAVE
from piket_client.model import Person, ConsumptionType, Consumption
import piket_client.logger

LOG = logging.getLogger(__name__)


def plop() -> None:
    """ Asynchronously play the plop sound. """
    PLOP_WAVE.play()


class NameButton(QPushButton):
    """ Wraps a QPushButton to provide a counter. """

    consumption_created = Signal(Consumption)

    def __init__(self, person: Person, active_id: str, *args, **kwargs) -> None:
        self.person = person
        self.active_id = active_id

        super().__init__(self.current_label, *args, **kwargs)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.consumption_created.connect(self.window().consumption_added)

        self.clicked.connect(self.process_click)

    @Slot(str)
    def new_active_id(self, new_id: str) -> None:
        """ Change the active ConsumptionType id, update the label. """
        self.active_id = new_id
        self.setText(self.current_label)

    @Slot()
    def rebuild(self) -> None:
        """ Refresh the Person object and the label. """
        self.person = self.person.reload()
        self.setText(self.current_label)

    @property
    def current_count(self) -> int:
        """ Return the count of the currently active ConsumptionType for this
        Person. """
        return self.person.consumptions.get(self.active_id, 0)

    @property
    def current_label(self) -> str:
        """ Return the label to show on the button. """
        return f"{self.person.name} ({self.current_count})"

    def process_click(self) -> None:
        """ Process a click on this button. """
        result = self.person.add_consumption(self.active_id)
        if result:
            plop()
            self.setText(self.current_label)
            self.consumption_created.emit(result)
        else:
            print("Jantoeternuitje, kapot")


class NameButtons(QWidget):
    """ Main widget responsible for capturing presses and registering them.
    """

    new_id_set = Signal(str)

    def __init__(self, consumption_type_id: str, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        self.layout = None
        self.layout = QGridLayout()
        self.setLayout(self.layout)
        self.active_consumption_type_id = consumption_type_id
        self.init_ui()

    @Slot(str)
    def consumption_type_changed(self, new_id: str):
        """ Process a change of the consumption type and propagate to the
        contained buttons. """
        self.active_consumption_type_id = new_id
        self.new_id_set.emit(new_id)

    def init_ui(self) -> None:
        """ Initialize UI: build GridLayout, retrieve People and build a button
        for each. """

        ps = Person.get_all()
        num_columns = round(len(ps) / 10) + 1

        if self.layout:
            LOG.debug("Removing %s widgets for rebuild", self.layout.count())
            for index in range(self.layout.count()):
                item = self.layout.itemAt(0)
                LOG.debug("Removing item %s: %s", index, item)
                if item:
                    w = item.widget()
                    LOG.debug("Person %s", w.person)
                    self.layout.removeItem(item)
                    w.deleteLater()

        for index, person in enumerate(ps):
            button = NameButton(person, self.active_consumption_type_id, self)
            self.new_id_set.connect(button.new_active_id)
            self.layout.addWidget(button, index // num_columns, index % num_columns)


class PiketMainWindow(QMainWindow):
    """ QMainWindow subclass responsible for showing the main application
    window. """

    consumption_type_changed = Signal(str)

    def __init__(self) -> None:
        super().__init__()

        self.main_widget = None
        self.toolbar = None
        self.osk = None
        self.undo_action = None
        self.undo_queue = collections.deque([], 20)
        self.init_ui()

    def init_ui(self) -> None:
        """ Initialize the UI: construct main widget and toolbar. """

        # Connect to dbus, get handle to virtual keyboard
        if dbus:
            try:
                session_bus = dbus.SessionBus()
                self.osk = session_bus.get_object(
                    "org.onboard.Onboard", "/org/onboard/Onboard/Keyboard"
                )
            except dbus.exceptions.DBusException:
                # Onboard not present or dbus broken
                self.osk = None

        # Go full screen
        self.setWindowState(Qt.WindowActive | Qt.WindowFullScreen)

        font_metrics = self.fontMetrics()
        icon_size = font_metrics.height() * 2

        # Initialize toolbar
        self.toolbar = QToolBar()
        self.toolbar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
        self.toolbar.setIconSize(QSize(icon_size, icon_size))

        # Left
        self.toolbar.addAction(
            self.load_icon("add_person.svg"), "Nieuw persoon", self.add_person
        )
        self.undo_action = self.toolbar.addAction(
            self.load_icon("undo.svg"), "Oeps", self.do_undo
        )
        self.undo_action.setDisabled(True)

        self.toolbar.addAction(
            self.load_icon("quit.svg"), "Afsluiten", self.confirm_quit
        )

        self.toolbar.addWidget(self.create_spacer())

        # Right
        ag = QActionGroup(self.toolbar)
        ag.setExclusive(True)

        cts = ConsumptionType.get_all()
        if not cts:
            self.show_keyboard()
            name, ok = QInputDialog.getItem(
                self,
                "Consumptietype toevoegen",
                (
                    "Dit lijkt de eerste keer te zijn dat Piket start. Wat wil je "
                    "tellen? Je kunt later meer typen toevoegen."
                ),
                ["Bier", "Wijn", "Cola"],
                current=0,
                editable=True,
            )
            self.hide_keyboard()

            if ok and name:
                c_type = ConsumptionType(name=name)
                c_type = c_type.create()

                cts.append(c_type)

            else:
                QMessageBox.critical(
                    self,
                    "Kan niet doorgaan",
                    (
                        "Je drukte op 'Annuleren' of voerde geen naam in, dus ik"
                        "sluit af."
                    ),
                )
                sys.exit()

        for ct in cts:
            action = QAction(self.load_icon(ct.icon or "beer_bottle.svg"), ct.name, ag)
            action.setCheckable(True)
            action.setData(str(ct.consumption_type_id))

        ag.actions()[0].setChecked(True)
        [self.toolbar.addAction(a) for a in ag.actions()]
        ag.triggered.connect(self.consumption_type_change)

        self.addToolBar(self.toolbar)

        # Initialize main widget
        self.main_widget = NameButtons(ag.actions()[0].data(), self)
        self.consumption_type_changed.connect(self.main_widget.consumption_type_changed)
        self.setCentralWidget(self.main_widget)

    @Slot(QAction)
    def consumption_type_change(self, action: QAction):
        self.consumption_type_changed.emit(action.data())

    def show_keyboard(self) -> None:
        """ Show the virtual keyboard, if possible. """
        if self.osk:
            self.osk.Show()

    def hide_keyboard(self) -> None:
        """ Hide the virtual keyboard, if possible. """
        if self.osk:
            self.osk.Hide()

    def add_person(self) -> None:
        """ Ask for a new Person and register it, then rebuild the central
        widget. """
        self.show_keyboard()
        name, ok = QInputDialog.getItem(
            self,
            "Persoon toevoegen",
            "Voer de naam van de nieuwe persoon in, of kies uit de lijst.",
            ["Cas", "Frenk"],
            0,
            True,
        )
        self.hide_keyboard()
        if ok and name:
            person = Person(name=name)
            person = person.create()

            self.main_widget.init_ui()

    def confirm_quit(self) -> None:
        """ Ask for confirmation that the user wishes to quit, then do so. """
        ok = QMessageBox.warning(
            self,
            "Wil je echt afsluiten?",
            "Bevestig dat je wilt afsluiten.",
            QMessageBox.Yes,
            QMessageBox.Cancel,
        )

        if ok == QMessageBox.Yes:
            LOG.warning("Shutdown by user.")
            QApplication.instance().quit()

    def do_undo(self) -> None:
        """ Undo the last marked consumption. """
        UNDO_WAVE.play()

        to_undo = self.undo_queue.pop()
        LOG.warning("Undoing consumption %s", to_undo)

        result = to_undo.reverse()

        if not result or not result.reversed:
            LOG.error("Reversed consumption %s but was not reversed!", to_undo)
            self.undo_queue.append(to_undo)

        elif not self.undo_queue:
            self.undo_action.setDisabled(True)

        self.main_widget.init_ui()

    @Slot(Consumption)
    def consumption_added(self, consumption):
        """ Mark an added consumption in the queue. """

        self.undo_queue.append(consumption)
        self.undo_action.setDisabled(False)

    @staticmethod
    def create_spacer() -> QWidget:
        """ Return an empty QWidget that automatically expands. """
        spacer = QWidget()
        spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        return spacer

    icons_dir = os.path.join(os.path.dirname(__file__), "icons")

    @classmethod
    def load_icon(cls, filename: str) -> QIcon:
        """ Return a QtIcon loaded from the given `filename` in the icons
        directory. """

        return QIcon(os.path.join(cls.icons_dir, filename))


def main() -> None:
    """ Main entry point of GUI client. """
    app = QApplication(sys.argv)
    font = app.font()
    size = font.pointSize()
    font.setPointSize(size * 1.75)
    app.setFont(font)

    main_window = PiketMainWindow()
    main_window.show()

    app.exec_()


if __name__ == "__main__":
    main()