import wx
import sys

# Function to detect dark mode on Windows
def is_dark_mode_windows():
    if sys.platform != 'win32':
        return None  # Pas Windows
    try:
        import winreg
        registry = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
        key_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize'
        key = winreg.OpenKey(registry, key_path)
        # The value 'AppsUseLightTheme' is 0 for dark, 1 for light
        value, regtype = winreg.QueryValueEx(key, 'AppsUseLightTheme')
        winreg.CloseKey(key)
        return 'Light' if value == 1 else 'Dark'
    except Exception:
        return None

# Function to detect dark mode on macOS
def is_dark_mode_macos():
    import subprocess
    try:
        result = subprocess.run(
            ['defaults', 'read', '-g', 'AppleInterfaceStyle'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        if 'Dark' in result.stdout:
            return 'Dark'
        else:
            return 'Light'
    except Exception:
        return None

# Function to detect dark mode on Linux (GNOME)
def is_dark_mode_linux():
    import subprocess
    try:
        # Check if gsettings is available
        result = subprocess.run(
            ['gsettings', 'set', 'org.gnome.desktop.interface', 'gtk-theme'],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        # Check the current theme
        theme = subprocess.check_output(
            ['gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'],
            text=True
        ).strip()

        # If the theme contains 'dark' or 'Dark', it is considered dark mode
        if 'dark' in theme.lower():
            return 'Dark'
        else:
            return 'Light'
    except Exception:
        return None

# Function to detect the overall theme
def get_os_theme():
    if sys.platform == 'win32':
        mode = is_dark_mode_windows()
        if mode:
            return mode
        else:
            return 'Light'
    elif sys.platform == 'darwin':
        mode = is_dark_mode_macos()
        if mode:
            return mode
        else:
            return 'Light'
    else:
        # Linux (GNOME)
        mode = is_dark_mode_linux()
        if mode:
            return mode
        else:
            return 'Light'

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        
        theme = get_os_theme()

        # A font can be retrieved from the OS default font
        # and modified
        font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
        font.SetStyle(wx.NORMAL)
        fontSize = self.GetFont().GetPointSize()
        font.SetPointSize(fontSize+4)
        
        # Display the information in the panel
        msg_1 = f"Current system theme : {theme}"
        label_1 = wx.StaticText(self, label=msg_1, style=wx.ALIGN_CENTER)
        msg_2 = f"Works correctly with :\n● wxPython 4.2.4 and wxWidgets 3.2.8\n● wxpython-4.3.0a16030 and wxWidgets 3.3.0"
        label_2 = wx.StaticText(self, label=msg_2, style=wx.ALIGN_CENTER)
        msg_3 = f"Tested with Windows 11"
        label_3 = wx.StaticText(self, label=msg_3, style=wx.ALIGN_CENTER)
        
        font.SetWeight(wx.BOLD)
        label_1.SetFont(font)
        font.SetWeight(wx.NORMAL)
        label_2.SetFont(font)
        label_3.SetFont(font)
        
        # Layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddStretchSpacer()
        sizer.Add(label_1, 0, wx.ALL | wx.CENTER, 10)
        sizer.Add(label_2, 0, wx.ALL | wx.CENTER, 10)
        sizer.Add(label_3, 0, wx.ALL | wx.CENTER, 10)
        sizer.AddStretchSpacer()
        self.SetSizer(sizer)
        
class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title="Retrieve the system theme", size=(600, 400))
        
        self.panel = MyPanel(self)
        self.Show()

class MyApp(wx.App):
    def OnInit(self):
        
        self.frame = MyFrame()
        self.frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()
