#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from PyQt6.QtGui import QIcon, QScreen
from PyQt6.QtCore import (
    QUrl,
    QLocale,
    QTranslator,
    QLibraryInfo,
    QVariant,
    Qt,
    QObject,
    QCoreApplication,
)
from PyQt6.QtWidgets import QApplication
import sys
import os
from helpers import get_desktop_name, get_desktop_name2, is_installed, NetworkState, ConfList, Autostart
from ui import SlideShowApp

# Workaround for opengl lib selection
from OpenGL import GL

translate = QCoreApplication.translate


if __name__ == "__main__":
    # old method of autostart
    old_path = os.getenv("HOME") + "/.config/mageiawelcome/norun.flag"
    if os.path.exists(old_path):
        # disable  autostart according to new method and remove old method
        Autostart().disable()
        os.remove(old_path)
        
    app = QApplication(sys.argv)
    locale = QLocale.system().name()
    qtTranslator = QTranslator()
    if qtTranslator.load(
        "qt_" + locale, QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath) 
    ):
        app.installTranslator(qtTranslator)
    appTranslator = QTranslator()
    if appTranslator.load(
        QLocale(), "mageiawelcome", "_", "/usr/share/mageiawelcome/translations"
    ):
        app.installTranslator(appTranslator)
    app.setOrganizationName("Mageia")
    app.setApplicationName("Mageiawelcome")
    direction = Qt.LayoutDirection.LeftToRight if  translate("mw-ui", "LTR") == "LTR" else Qt.LayoutDirection.RightToLeft
    app.setLayoutDirection(direction)
    app.setWindowIcon(QIcon("/usr/share/icons/hicolor/32x32/apps/mageiawelcome.png"))
    ns = NetworkState()
    cl = ConfList(ns)
    # sc = nr.startupcheck()
    factor = cl.factor(app)
    screen = app.primaryScreen()
    # if density is high, use at least factor 2 to scale the initial window. Considering high is more than 190 px/inches.
    # if screen.logicalDotsPerInch() > 190:
    #     factor = max(2.0, factor)
    # defaultHeight = min(int(700 * factor), screen.availableGeometry().height())
    # defaultWidth = min(int(1000 * factor), screen.availableGeometry().width())
    # centerPoint = screen.availableGeometry().center()
    # view.setGeometry(
    #     centerPoint.x() - defaultWidth // 2,
    #     centerPoint.y() - defaultHeight // 2,
    #     defaultWidth,
    #     defaultHeight,
    # )
    view = SlideShowApp()
    view.show()
    res = app.exec()
    del view
    sys.exit(res)
