from importlib.util import find_spec
from logging import getLogger, NOTSET, FileHandler, Formatter, StreamHandler, info
from os import environ
from pathlib import Path
from sys import argv
from time import strftime, localtime, time
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication
from func.Module_mainwindow import MainWindow

if __name__ == '__main__':
    # 设置日志
    logger = getLogger()
    logger.setLevel(NOTSET)
    realtime = strftime('%Y%m%d', localtime(time()))
    if not Path("logs").exists():
        Path("logs").mkdir(exist_ok=True)
    fh = FileHandler(Path("logs") / (realtime + ".log"), mode='a')
    fh.setLevel(NOTSET)
    fh.setFormatter(Formatter("%(asctime)s: %(message)s"))
    logger.addHandler(fh)

    ch = StreamHandler()
    ch.setLevel(NOTSET)
    ch.setFormatter(Formatter("%(asctime)s: %(message)s"))
    logger.addHandler(ch)
    getLogger('matplotlib.font_manager').disabled = True
    info("程序启动")

    # 解决 Could not find the Qt platform plugin "windows"
    spec = find_spec("PySide6")
    if spec and spec.origin:
        dirname = Path(spec.origin).parent
        plugin_path = dirname / 'plugins' / 'platforms'
        environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path.__str__()

    # 解决 Error #15: Initializing libiomp5md.dll
    environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    app = QApplication(argv)
    app.setStyle("Fusion")
    mainWindow = MainWindow()
    mainWindow.show()
    exit(app.exec())