# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from typing import List
from bindings import parseBuoOutput
from buo_py_bindings import get_buo_output
from focused_cache_widget import TopCachedWidget
from PyQt5.QtGui import QFont, QIcon, QPixmap
from PyQt5.QtCore import Qt, QSize, QStringListModel, QTimer, QMetaObject, QCoreApplication, QRect
from PyQt5.QtWidgets import QMainWindow, QWidget, QLineEdit, QCheckBox, QVBoxLayout, QHBoxLayout, QSizePolicy, QListView, QScrollArea, QFrame, QGridLayout, QStackedWidget, QAbstractScrollArea

DEBOUNCE_INTERVAL = 600


class BuoPrototype(QMainWindow):
    debouncer: QTimer

    def __init__(self):
        super().__init__()
        self.debouncer = QTimer(self)
        self.debouncer.setSingleShot(True)

        self.setupUi()
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.cache_hit_model = QStringListModel(self.cache_hit_list)

    def debounceEvent(self, callback):
        self.debouncer.timeout.connect(callback)
        self.debouncer.start(DEBOUNCE_INTERVAL)

    def updateTopWidget(self, query: str):
        def debouncedUpdate():
            try:
                buo_response = get_buo_output(query)
                parsed_response = parseBuoOutput(buo_response)
                self.top_cached_widget.update(parsed_response)
            except Exception as E:
                print(E)

        self.debounceEvent(debouncedUpdate)

    def refreshCacheHitList(self, items: List[str]):
        self.cache_hit_model.setStringList(items)

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.resize(560, 70)
        sizePolicy = QSizePolicy(
            QSizePolicy.Preferred, QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(
            self.sizePolicy().hasHeightForWidth())
        self.setSizePolicy(sizePolicy)
        self.setMinimumSize(QSize(560, 70))
        self.setMaximumSize(QSize(560, 600))

        font = QFont()
        font.setFamily("Inter Light")
        font.setPointSize(10)
        self.setFont(font)

        self.setContextMenuPolicy(Qt.NoContextMenu)
        icon = QIcon()
        icon.addPixmap(QPixmap(
            "../../../../Pictures/Link to Pictures/155410143_3717931704964661_1567340756741086546_n.jpg"), QIcon.Normal, QIcon.Off)
        self.setWindowIcon(icon)

        self.centralwidget = QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_3 = QGridLayout(self.centralwidget)
        self.gridLayout_3.setObjectName("gridLayout_3")

        self.page_stack = QStackedWidget(self.centralwidget)
        sizePolicy = QSizePolicy(
            QSizePolicy.MinimumExpanding, QSizePolicy.Minimum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(
            self.page_stack.sizePolicy().hasHeightForWidth())
        self.page_stack.setSizePolicy(sizePolicy)
        self.page_stack.setContextMenuPolicy(Qt.NoContextMenu)
        self.page_stack.setAutoFillBackground(False)
        self.page_stack.setLineWidth(0)
        self.page_stack.setObjectName("page_stack")

        self.no_items_page = QWidget()
        self.no_items_page.setObjectName("no_items_page")
        self.gridLayout_2 = QGridLayout(self.no_items_page)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.horizontalLayout_2 = QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.cache_query_2 = QLineEdit(self.no_items_page)
        self.cache_query_2.setContextMenuPolicy(Qt.NoContextMenu)
        self.cache_query_2.setObjectName("cache_query_2")
        self.horizontalLayout_2.addWidget(self.cache_query_2)
        self.checkBox_2 = QCheckBox(self.no_items_page)
        self.checkBox_2.setText("")
        self.checkBox_2.setObjectName("checkBox_2")
        self.horizontalLayout_2.addWidget(self.checkBox_2)
        self.gridLayout_2.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
        self.page_stack.addWidget(self.no_items_page)

        self.results_page = QWidget()
        self.results_page.setObjectName("results_page")
        self.verticalLayout = QVBoxLayout(self.results_page)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.cache_query = QLineEdit(self.results_page)
        self.cache_query.setContextMenuPolicy(Qt.NoContextMenu)
        self.cache_query.setMaxLength(150)
        self.cache_query.setFrame(True)
        self.cache_query.setAlignment(Qt.AlignCenter)
        self.cache_query.setClearButtonEnabled(True)
        self.cache_query.setObjectName("cache_query")
        self.horizontalLayout.addWidget(self.cache_query)
        self.checkBox = QCheckBox(self.results_page)
        self.checkBox.setText("")
        self.checkBox.setObjectName("checkBox")
        self.horizontalLayout.addWidget(self.checkBox)
        self.verticalLayout.addLayout(self.horizontalLayout)

        self.top_cached_widget = TopCachedWidget(self.results_page)
        sizePolicy = QSizePolicy(
            QSizePolicy.Preferred, QSizePolicy.MinimumExpanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(2)
        sizePolicy.setHeightForWidth(
            self.top_cached_widget.sizePolicy().hasHeightForWidth())
        self.top_cached_widget.setSizePolicy(sizePolicy)
        self.top_cached_widget.setObjectName("top_cached_widget")
        self.verticalLayout.addWidget(self.top_cached_widget)

        self.scrollArea = QScrollArea(self.results_page)
        sizePolicy = QSizePolicy(
            QSizePolicy.Expanding, QSizePolicy.MinimumExpanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(9)
        sizePolicy.setHeightForWidth(
            self.scrollArea.sizePolicy().hasHeightForWidth())
        self.scrollArea.setSizePolicy(sizePolicy)
        self.scrollArea.setFrameShape(QFrame.Box)
        self.scrollArea.setHorizontalScrollBarPolicy(
            Qt.ScrollBarAlwaysOff)
        self.scrollArea.setSizeAdjustPolicy(
            QAbstractScrollArea.AdjustToContents)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setAlignment(Qt.AlignCenter)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QWidget()
        self.scrollAreaWidgetContents.setGeometry(QRect(0, 0, 518, 78))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.gridLayout = QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout.setObjectName("gridLayout")

        self.cache_hit_list = QListView(
            self.scrollAreaWidgetContents)
        self.cache_hit_list.setContextMenuPolicy(Qt.NoContextMenu)
        self.cache_hit_list.setAutoFillBackground(False)
        self.cache_hit_list.setFrameShape(QFrame.HLine)
        self.cache_hit_list.setFrameShadow(QFrame.Plain)
        self.cache_hit_list.setLineWidth(0)
        self.cache_hit_list.setObjectName("cache_hit_list")
        self.gridLayout.addWidget(self.cache_hit_list, 1, 1, 1, 1)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.verticalLayout.addWidget(self.scrollArea)
        self.page_stack.addWidget(self.results_page)
        self.gridLayout_3.addWidget(self.page_stack, 0, 0, 1, 1)
        self.setCentralWidget(self.centralwidget)

        self.retranslateUi()
        self.page_stack.setCurrentIndex(0)
        QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QCoreApplication.translate
        self.setWindowTitle(_translate("MainWindow", "Buo Prototype"))
        self.cache_query.setPlaceholderText(_translate("MainWindow", "search"))
