package ui

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

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

	//退出
	quite *fyne.MenuItem

	// 工具
	toolMenu *fyne.Menu
	//同步配置
	syncConfMenu *fyne.MenuItem
}

func newToolMenu() *toolMenu {
	var mm toolMenu
	// system
	mm.appMenus = make([]*fyne.MenuItem, len(toolRegister))
	for i, app := range toolRegister {
		mm.appMenus[i] = fyne.NewMenuItem(app.GetAppName(), func() {
			err := toolWin.at.openApp(app)
			if err != nil {
				logrus.Errorf("打开 %s, err:%v", app.GetAppName(), err)
			}
		})
		mm.appMenus[i].Icon = app.Icon()
	}
	mm.quite = fyne.NewMenuItem("退出", toolWin.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)

	//tool
	mm.syncConfMenu = fyne.NewMenuItem("同步云配置", toolWin.syncConfig)
	mm.toolMenu = fyne.NewMenu("工具",
		mm.syncConfMenu,
	)

	mm.MainMenu = fyne.NewMainMenu(
		mm.sysMenu,
		mm.toolMenu,
	)
	return &mm
}
