import wx
import os


class MainFrame(wx.Frame):
    def __init__(self, parent, title):
        super().__init__(parent, title=title, size=(800, 600))

        # 创建菜单栏
        menubar = wx.MenuBar()
        menu1 = wx.Menu()
        menu1.Append(wx.ID_EXIT, "退出")
        menubar.Append(menu1, "菜单1")

        menu2 = wx.Menu()
        menu2.Append(wx.ID_ANY, "选项1")
        menu2.Append(wx.ID_ANY, "选项2")
        menubar.Append(menu2, "菜单2")

        menu3 = wx.Menu()
        menu3.Append(wx.ID_ANY, "设置")
        menubar.Append(menu3, "菜单3")

        menu4 = wx.Menu()
        menu4.Append(wx.ID_ANY, "关于")
        menubar.Append(menu4, "菜单4")

        self.SetMenuBar(menubar)

        # 创建主面板和布局
        main_panel = wx.Panel(self)
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # 创建左侧导航栏
        left_panel = wx.Panel(main_panel)
        left_sizer = wx.BoxSizer(wx.VERTICAL)

        self.navigation_list = wx.ListBox(left_panel, choices=["tab1", "tab2", "tab3", "tab4", "..."], style=wx.LB_SINGLE)
        left_sizer.Add(self.navigation_list, 1, wx.EXPAND | wx.ALL, 5)

        left_panel.SetSizer(left_sizer)
        main_sizer.Add(left_panel, 0, wx.EXPAND | wx.ALL, 5)

        # 创建右侧内容区域
        right_panel = wx.Panel(main_panel)
        right_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # 创建 tab 对应的右侧内容面板
        self.tab_panels = {}
        for tab_name in ["tab1", "tab2", "tab3", "tab4", "..."]:
            tab_panel = wx.Panel(right_panel)
            tab_sizer = wx.BoxSizer(wx.VERTICAL)

            # 将 main_content 创建的内容添加到 tab 面板中
            content = self.main_content(tab_panel, tab_name)
            tab_sizer.Add(content, 1, wx.EXPAND | wx.ALL, 5)

            tab_panel.SetSizer(tab_sizer)
            self.tab_panels[tab_name] = tab_panel
            right_sizer.Add(tab_panel, 1, wx.EXPAND | wx.ALL, 5)
            tab_panel.Hide()  # 初始隐藏所有面板

        right_panel.SetSizer(right_sizer)
        main_sizer.Add(right_panel, 1, wx.EXPAND | wx.ALL, 5)

        main_panel.SetSizer(main_sizer)

        # 绑定 ListBox 的选择事件
        self.navigation_list.Bind(wx.EVT_LISTBOX, self.on_tab_selected)

        self.Show(True)
        self.show_tab_content("tab1")  # 初始显示 tab1 的内容

    def main_content(self, parent, tab_name):
        # 创建右侧内容区域
        right_panel = wx.Panel(parent)
        right_sizer = wx.BoxSizer(wx.HORIZONTAL)

        if tab_name == "tab1":
            # tab1 的内容
            left_content_panel = wx.Panel(right_panel)
            left_content_sizer = wx.BoxSizer(wx.VERTICAL)

            form_panel = wx.Panel(left_content_panel)
            form_sizer = wx.GridBagSizer(5, 5)

            form_sizer.Add(wx.StaticText(form_panel, label="设置1:"), pos=(0, 0), flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
            form_sizer.Add(wx.TextCtrl(form_panel), pos=(0, 1), flag=wx.EXPAND)

            form_sizer.Add(wx.StaticText(form_panel, label="设置2:"), pos=(1, 0), flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
            form_sizer.Add(wx.TextCtrl(form_panel), pos=(1, 1), flag=wx.EXPAND)

            form_panel.SetSizer(form_sizer)
            left_content_sizer.Add(form_panel, 0, wx.EXPAND | wx.ALL, 5)

            self.log_text = wx.TextCtrl(left_content_panel, style=wx.TE_MULTILINE | wx.TE_READONLY)
            left_content_sizer.Add(self.log_text, 1, wx.EXPAND | wx.ALL, 5)

            left_content_panel.SetSizer(left_content_sizer)
            right_sizer.Add(left_content_panel, 1, wx.EXPAND | wx.ALL, 5)

            image_panel = wx.Panel(right_panel)
            image_sizer = wx.BoxSizer(wx.HORIZONTAL)

            current_dir = os.path.dirname(os.path.abspath(__file__))
            image_path = os.path.join(current_dir, "../../assets/img.png")
            img = wx.Image(image_path, wx.BITMAP_TYPE_PNG).Scale(200, 200)
            self.image_bitmap = wx.StaticBitmap(image_panel, wx.ID_ANY, wx.Bitmap(img))
            image_sizer.Add(self.image_bitmap, 1, wx.CENTER)

            image_panel.SetSizer(image_sizer)
            right_sizer.Add(image_panel, 0, wx.CENTER | wx.ALL, 5)
        elif tab_name == "tab2":
            # tab2 的内容
            text = wx.StaticText(right_panel, label="这是 tab2 的特殊内容")
            right_sizer.Add(text, 1, wx.CENTER)
        elif tab_name == "tab3":
            # tab3 的内容
            button = wx.Button(right_panel, label="点击我")
            right_sizer.Add(button, 1, wx.CENTER)
        else:
            # 其他 tab 的内容
            text = wx.StaticText(right_panel, label=f"这是 {tab_name} 的默认内容")
            right_sizer.Add(text, 1, wx.CENTER)

        right_panel.SetSizer(right_sizer)

        return right_panel

    def on_tab_selected(self, event):
        selected_tab = self.navigation_list.GetStringSelection()
        self.show_tab_content(selected_tab)

    def show_tab_content(self, tab_name):
        for name, panel in self.tab_panels.items():
            if name == tab_name:
                panel.Show()
            else:
                panel.Hide()
        self.Layout()  # 重新布局


if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None, "我的GUI布局")
    app.MainLoop()
