package ui

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/theme"
	"github.com/sirupsen/logrus"
)

type mainMenu struct {
	*fyne.MainMenu
	sysMenu  *fyne.Menu
	appMenus []*fyne.MenuItem

	settingMenu *fyne.Menu
	testingMenu *fyne.MenuItem

	quite *fyne.MenuItem

	helpMenu *fyne.Menu
	sysLog   *fyne.MenuItem
	// aboutSelf *fyne.MenuItem
}

func newMainMenu() *mainMenu {
	var mm mainMenu

	// system
	mm.appMenus = make([]*fyne.MenuItem, len(appRegister))
	for i, app := range appRegister {
		app := app
		mm.appMenus[i] = fyne.NewMenuItem(app.GetAppName(), func() {
			err := globalWin.at.openApp(app)
			if err != nil {
				logrus.Errorf("打开 %s, err:%v", app.GetAppName(), err)
			}
		})
		mm.appMenus[i].Icon = app.Icon()
	}

	mm.quite = fyne.NewMenuItem("退出", globalWin.quiteHandle)
	mm.quite.Icon = theme.LogoutIcon()
	mm.quite.IsQuit = true
	mm.sysMenu = fyne.NewMenu("应用", mm.appMenus...)
	mm.sysMenu.Items = append(mm.sysMenu.Items, fyne.NewMenuItemSeparator(), mm.quite)

	// setting
	mm.testingMenu = fyne.NewMenuItem("压测配置", func() {
		newTestConfigWindow().Win.Show()
	})
	mm.testingMenu.Icon = theme.SettingsIcon()
	mm.settingMenu = fyne.NewMenu("设置",
		mm.testingMenu,
	)

	// help
	mm.sysLog = fyne.NewMenuItem("日志", func() {
		newLogViewer().Win.Show()
	})
	mm.helpMenu = fyne.NewMenu("帮助",
		mm.sysLog,
		// mm.aboutSelf,
	)

	mm.MainMenu = fyne.NewMainMenu(
		mm.sysMenu,
		// mm.settingMenu,
		// mm.helpMenu,
	)
	return &mm
}
