import sys
import base64
import json
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QSizePolicy
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtCore import Qt
from qt_material import apply_stylesheet
import arabic_reshaper
from bidi.algorithm import get_display
import ctypes
import requests
from MainDashbord import MainDashboard
from MainActivation import MainActivation
from MainDashbord import LoadingScreen
from PyQt5.QtCore import  QTimer
import os
from PyQt5.QtCore import QLocale
from  selectLang import LanguageSelector
def changeWindow(window):
    main_window = MainDashboard()
    main_window.start_loading(window)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    QLocale.setDefault(QLocale(QLocale.Persian, QLocale.Iran))
    thems="dark_blue"
    if os.path.exists("selected_theme.json") and os.path.getsize("selected_theme.json") > 0:
        with open("selected_theme.json", "r", encoding='utf-8') as file:
            login = json.load(file)
            if login.get("selected_theme") != "":
                thems = login.get("selected_theme")


    # اعمال تم متریال دیزاین
    apply_stylesheet(app, theme=thems+'.xml')
    if os.path.exists("login.json") and os.path.getsize("login.json") > 0:
        with open("login.json", "r", encoding='utf-8') as file:
            content = file.read()
            if content:
                data = json.loads(content)
            else:
                data = {}
    else:
        data = {}

    login_status = data.get("login")
    if login_status == "True":
        window = LoadingScreen()
        window.show()

        # استفاده از lambda برای پاس دادن متد به QTimer.singleShot
        QTimer.singleShot(2000, lambda: changeWindow(window))  # Close after 2 seconds


    else:
        window = LanguageSelector()
        window.show()



    sys.exit(app.exec_())
