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

# Form implementation generated from reading ui file 'server_win.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# 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.QtGui import QStandardItemModel, QStandardItem
from server_config.db.server_db import DB
from server_config.db.models.messanger_db import User, User_history, Contact_list
from server import Server
import os

class Ui_MainWindow(object):
    db = DB().session
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.btn_user_list = QtWidgets.QPushButton(self.centralwidget)
        self.btn_user_list.setGeometry(QtCore.QRect(30, 10, 75, 23))
        self.btn_user_list.setObjectName("user_list")
        self.btn_user_list.clicked.connect(self.user_list)

        self.btn_history = QtWidgets.QPushButton(self.centralwidget)
        self.btn_history.setGeometry(QtCore.QRect(320, 10, 75, 23))
        self.btn_history.setObjectName("user_history")
        self.btn_history.clicked.connect(self.user_history)

        self.btn_server = QtWidgets.QPushButton(self.centralwidget)
        self.btn_server.setGeometry(QtCore.QRect(570, 320, 101, 23))
        self.btn_server.setObjectName("server_settings")
        self.btn_server.clicked.connect(self.server_settings)

        self.answer=QtWidgets.QLabel(self.centralwidget)
        self.answer.setGeometry(QtCore.QRect(570, 380, 101, 23))


        self.Database = QtWidgets.QLabel(self.centralwidget)
        self.Database.setGeometry(QtCore.QRect(30, 60, 401, 481))
        self.Database.setObjectName("Database")

        self.path = QtWidgets.QLineEdit(self.centralwidget)
        self.path.setGeometry(QtCore.QRect(620, 80, 241, 31))
        self.path.setObjectName("path")

        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(460, 85, 141, 21))
        self.label.setObjectName("label")

        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(460, 140, 141, 21))
        self.label_2.setObjectName("label_2")

        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(460, 200, 141, 21))
        self.label_3.setObjectName("label_3")

        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(460, 260, 141, 21))
        self.label_4.setObjectName("label_4")

        self.database_name = QtWidgets.QLineEdit(self.centralwidget)
        self.database_name.setGeometry(QtCore.QRect(620, 260, 161, 31))
        self.database_name.setObjectName("database_name")

        self.host = QtWidgets.QLineEdit(self.centralwidget)
        self.host.setGeometry(QtCore.QRect(620, 200, 161, 31))
        self.host.setObjectName("host")

        self.port = QtWidgets.QLineEdit(self.centralwidget)
        self.port.setGeometry(QtCore.QRect(620, 140, 161, 31))
        self.port.setObjectName("port")

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btn_user_list.setText(_translate("MainWindow", "user list"))
        self.btn_history.setText(_translate("MainWindow", "user history"))
        self.btn_server.setText(_translate("MainWindow", "Set server settings"))
        self.label.setText(_translate("MainWindow", "Open database"))
        self.path.setText(_translate("MainWindow", "/home/ural-fox/Projects/GB-AsincChat/main_project/server_config/db"))
        self.label_2.setText(_translate("MainWindow", "Port"))
        self.label_3.setText(_translate("MainWindow", "host"))
        self.label_4.setText(_translate("MainWindow", "Database name"))
        self.port.setText(_translate("MainWindow", "7777"))
        self.host.setText(_translate("MainWindow", "localhost"))
        self.database_name.setText(_translate("MainWindow", "messenger.db3"))

    def user_list(self):
        user_list = self.db.query(User).all()
        self.Database.setText(f"{'Id':>3} {'user login':>20} {'Is_active':>10}\n")
        for user in user_list:
            line = f"{user.id:>3} {user.login:>20} {user.is_active:>10}\n"
            self.Database.setText(self.Database.text() + line)
        pass

    def user_history(self):
        user_list = self.db.query(User_history).all()
        self.Database.setText('Id' + '                ' + 'host' + '                                 ' + 'Come in (time)'+'\n')
        for user in user_list:
            line = f"{user.user_id:>3} {user.ip_address:>20} {user.entry_time:>10}\n"
            self.Database.setText(self.Database.text() + line)
        pass

    def server_settings(self):
        port=int(self.port.text())
        host=self.host.text()
        database='sqlite:///'+self.path.text()+'/'+self.database_name.text()
        
        try:
            Server(host=host, port=port)
            os.path.exists(database)
            self.answer.setText('Settings are correct!')
        except:
            self.answer.setText('Error settings!')

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())