# This Python file uses the following encoding: utf-8
import sys
from pathlib import Path
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
#from PySide6.QtCore import QResource
from lib.ConfigLoader import ConfigLoader
from lib.DB.BusinessModel import BusinessModel
import rc_pyqcrm
import rc_qml
from lib.DB.DbManager import DbManager

# [pyqcrm]
# program-name=""
# version=

# [database]
# server=""
# port=
# user=""
# password=""
# name=""
# type=""


if __name__ == "__main__":
    #QResource.registerResource("rc_qml.py")
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    engine.addImportPath("qrc:/");



    bad_config = False



    qml_file = Path(__file__).resolve().parent / "Gui/main.qml"

    #qml_file = ":/gui/main.qml"

    config = ConfigLoader()


    if not config.getConfig():
        bad_config = True
        bm = False
    else:
        dbconf = config.getConfig()['database']
        DbManager(dbconf)
        bm = BusinessModel(DbManager().getConnection())

        #print(con is con2)

    engine.rootContext().setContextProperty("bm", bm)
    engine.rootContext().setContextProperty("bad_config", bad_config) #     print(f"Fehler: {i}")
    engine.rootContext().setContextProperty("config", config)
    engine.load(qml_file)


    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec())
