package ui

import (
	"go_dreamfactory/cmd/v2/lib/common"
	"go_dreamfactory/cmd/v2/service"
	"go_dreamfactory/cmd/v2/service/observer"
	"io/ioutil"
	"path/filepath"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/theme"
	"github.com/sirupsen/logrus"
)

//半自动化 依据采集的数据
type appAuto struct {
	appAdapter

	obs observer.Observer

	caseList func()
	itemList *common.ItemList
}

// 读文件
func (app *appAuto) loadData() error {
	bytes, err := ioutil.ReadFile(filepath.Join(".", "robot.log"))
	if err != nil {
		return err
	}

	logrus.Debug(bytes)
	return nil
}

func (app *appAuto) LazyInit(service service.PttService, obs observer.Observer) error {
	app.obs = obs
	app.tabItem = container.NewTabItemWithIcon(common.TOOLBAR_AUTO, theme.StorageIcon(), nil)

	content := container.NewMax()
	content.Objects = []fyne.CanvasObject{}

	// 初始化列表
	app.itemList = common.NewItemList()
	app.itemList.ItemList = app.itemList.CreateList()

	// 读取采集的数据文件
	app.caseList = func() {
		app.loadData()
	}

	defer func() {
		app.caseList()
	}()

	// layout
	// panel := container.NewVSplit(container.NewBorder(app.monitorHeader, nil, nil, nil, app.monitorList), resPanel)
	// panel.Offset = 0.8
	content.Objects = append(content.Objects)
	app.tabItem.Content = content
	return nil
}

func (a *appAuto) OpenDefault() string {
	return common.TOOLBAR_AUTO
}

func (a *appAuto) GetAppName() string {
	return "自动化"
}

func (a appAuto) OnClose() bool {
	return false
}

func (this *appAuto) ShortCut() fyne.Shortcut {
	return &desktop.CustomShortcut{KeyName: fyne.Key3, Modifier: desktop.AltModifier}
}

func (this *appAuto) Icon() fyne.Resource {
	return theme.StorageIcon()
}
