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

# Form implementation generated from reading ui file 'form.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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 PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject

from controllers.login_controller import LoginController
from controllers.map_controller import MapController
from views.map_cell import MapCell


class Ui_MainLounge(object):

    def __init__(self):
        self._rows = 0
        self._columns = 0
        self._map_controller = MapController()
        self._location_dict = self.load_locations()
        self._location_cell_dict = {}
        self._login_controller = LoginController()

    def load_locations(self) -> dict[tuple[int, int], str]:
        return self._map_controller.get_all_location()

    def setupUi(self, MainLounge):
        MainLounge.setObjectName("MainLounge")
        MainLounge.resize(800, 600)
        self.gridLayout_2 = QtWidgets.QGridLayout(MainLounge)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.LoginSearch = QtWidgets.QGroupBox(MainLounge)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.LoginSearch.sizePolicy().hasHeightForWidth())
        self.LoginSearch.setSizePolicy(sizePolicy)
        self.LoginSearch.setObjectName("LoginSearch")
        self.gridLayout_7 = QtWidgets.QGridLayout(self.LoginSearch)
        self.gridLayout_7.setObjectName("gridLayout_7")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.loginBox = QtWidgets.QGroupBox(self.LoginSearch)
        self.loginBox.setObjectName("loginBox")
        self.gridLayout_9 = QtWidgets.QGridLayout(self.loginBox)
        self.gridLayout_9.setObjectName("gridLayout_9")
        self.gridLayout_8 = QtWidgets.QGridLayout()
        self.gridLayout_8.setObjectName("gridLayout_8")
        self.label_5 = QtWidgets.QLabel(self.loginBox)
        self.label_5.setObjectName("label_5")
        self.gridLayout_8.addWidget(self.label_5, 0, 0, 1, 1)
        self.label_6 = QtWidgets.QLabel(self.loginBox)
        self.label_6.setObjectName("label_6")
        self.gridLayout_8.addWidget(self.label_6, 1, 0, 1, 1)
        self.loginButton = QtWidgets.QPushButton(self.loginBox)
        self.loginButton.setObjectName("loginButton")
        self.gridLayout_8.addWidget(self.loginButton, 2, 0, 1, 1)
        self.userNameLineEdit = QtWidgets.QLineEdit(self.loginBox)
        self.userNameLineEdit.setObjectName("userNameLineEdit")
        self.gridLayout_8.addWidget(self.userNameLineEdit, 0, 1, 1, 1)
        self.passwordLineEdit = QtWidgets.QLineEdit(self.loginBox)
        self.passwordLineEdit.setObjectName("passwordLineEdit")
        self.gridLayout_8.addWidget(self.passwordLineEdit, 1, 1, 1, 1)
        self.loginText = QtWidgets.QLabel(self.loginBox)
        self.loginText.setObjectName("loginText")
        self.gridLayout_8.addWidget(self.loginText, 2, 1, 1, 1)
        self.gridLayout_9.addLayout(self.gridLayout_8, 0, 0, 1, 1)
        self.verticalLayout_3.addWidget(self.loginBox)
        self.searchBox = QtWidgets.QGroupBox(self.LoginSearch)
        self.searchBox.setObjectName("searchBox")
        self.gridLayout_11 = QtWidgets.QGridLayout(self.searchBox)
        self.gridLayout_11.setObjectName("gridLayout_11")
        self.gridLayout_10 = QtWidgets.QGridLayout()
        self.gridLayout_10.setObjectName("gridLayout_10")
        self.source_selector = QtWidgets.QComboBox(self.searchBox)
        self.source_selector.setObjectName("source_selector")
        self.gridLayout_10.addWidget(self.source_selector, 0, 1, 1, 1)
        self.label_7 = QtWidgets.QLabel(self.searchBox)
        self.label_7.setObjectName("label_7")
        self.gridLayout_10.addWidget(self.label_7, 0, 0, 1, 1)
        self.destination_selector = QtWidgets.QComboBox(self.searchBox)
        self.destination_selector.setObjectName("destination_selector")
        self.gridLayout_10.addWidget(self.destination_selector, 1, 1, 1, 1)
        self.label_8 = QtWidgets.QLabel(self.searchBox)
        self.label_8.setObjectName("label_8")
        self.gridLayout_10.addWidget(self.label_8, 1, 0, 1, 1)
        self.showRouteButton = QtWidgets.QPushButton(self.searchBox)
        self.showRouteButton.setObjectName("showRouteButton")
        self.gridLayout_10.addWidget(self.showRouteButton, 2, 0, 1, 1)
        self.gridLayout_11.addLayout(self.gridLayout_10, 0, 0, 1, 1)
        self.verticalLayout_3.addWidget(self.searchBox)
        self.gridLayout_7.addLayout(self.verticalLayout_3, 0, 0, 1, 1)
        self.horizontalLayout.addWidget(self.LoginSearch)
        self.mapBox = QtWidgets.QGroupBox(MainLounge)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(2)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.mapBox.sizePolicy().hasHeightForWidth())
        self.mapBox.setSizePolicy(sizePolicy)
        self.mapBox.setObjectName("mapBox")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.mapBox)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.map_grid = QtWidgets.QGridLayout()
        self.map_grid.setObjectName("map_grid")
        self.gridLayout_3.addLayout(self.map_grid, 0, 0, 1, 1)
        self.horizontalLayout.addWidget(self.mapBox)
        self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)

        self.retranslateUi(MainLounge)
        self.init_signals()
        QtCore.QMetaObject.connectSlotsByName(MainLounge)

    def init_signals(self):
        self.destination_selector.activated.connect(self.on_destination_combobox_select)
        self.source_selector.activated.connect(self.on_source_combobox_select)
        self.showRouteButton.clicked.connect(self.on_search_button_clicked)
        self.loginButton.clicked.connect(self.onLoginClick)

    def on_search_button_clicked(self):
        start_point_cell = self.source_selector.currentData()
        start_point = (start_point_cell.get_row(), start_point_cell.get_column())

        end_point_cell = self.destination_selector.currentData()
        end_point = (end_point_cell.get_row(), end_point_cell.get_column())

        shortest_paths = \
            self._map_controller.get_shortest_path(total_rows=self._rows,
                                                   total_columns=self._columns,
                                                   start_point=start_point,
                                                   end_point=end_point
                                                   )
        for path in shortest_paths:
            (self._location_cell_dict[path]).highlight_path()
        print(shortest_paths)

    def on_source_combobox_select(self, i):
        # self.destination_selector.currentText()

        for count in range(self.source_selector.count()):
            self.source_selector.itemData(count).remove_highlight(is_source=True)

        selected_cell = self.source_selector.currentData()
        selected_cell.__class__ = MapCell
        selected_cell.highlight(is_source=True)

    def on_destination_combobox_select(self, i):

        for count in range(self.destination_selector.count()):
            self.destination_selector.itemData(count).remove_highlight(is_destination=True)

        selected_cell = self.destination_selector.currentData()
        selected_cell.__class__ = MapCell
        selected_cell.highlight(is_destination=True)


    def retranslateUi(self, MainLounge):
        _translate = QtCore.QCoreApplication.translate
        MainLounge.setWindowTitle(_translate("MainLounge", "MianLounge"))
        self.LoginSearch.setTitle(_translate("MainLounge", ""))
        self.loginBox.setTitle(_translate("MainLounge", "Login"))
        self.label_5.setText(_translate("MainLounge", "UserName"))
        self.label_6.setText(_translate("MainLounge", "Password"))
        self.loginButton.setText(_translate("MainLounge", "Login"))
        self.loginText.setText(_translate("MainLounge", ""))
        self.searchBox.setTitle(_translate("MainLounge", "Search"))
        self.label_7.setText(_translate("MainLounge", "Source"))
        self.label_8.setText(_translate("MainLounge", "Destination"))
        self.showRouteButton.setText(_translate("MainLounge", "Show Route"))
        self.mapBox.setTitle(_translate("MainLounge", "Map"))


        # Populate ComboBox and Map
        for pos, store in self._location_dict.items():
            self._rows = pos[0] if pos[0] > self._rows else self._rows
            self._columns = pos[1] if pos[1] > self._columns else self._columns
            map_cell = MapCell(pos[0], pos[1], store)
            map_cell_widget = map_cell.getCell()
            self.destination_selector.addItem(str(pos) + " --> " + store, map_cell)
            self.source_selector.addItem(str(pos) + " --> " + store, map_cell)

            self.map_grid.addWidget(map_cell_widget, pos[0], pos[1])
            self._location_cell_dict[pos] = map_cell

        self.searchBox.setEnabled(False)
        self.mapBox.setEnabled(False)

    def onLoginClick(self):
        x = self._login_controller.on_login_request(self.userNameLineEdit.text(), self.passwordLineEdit.text())

        if x[0]:
            self.searchBox.setEnabled(True)
            self.mapBox.setEnabled(True)
        else:
            self.searchBox.setEnabled(False)
            self.mapBox.setEnabled(False)

        self.loginText.setText(x[1])
