package ui

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cast"
)

type TestConfigWindow struct {
	computer *widget.Button

	Win fyne.Window
}

func newTestConfigWindow() *TestConfigWindow {
	var configWin TestConfigWindow
	conf := globalWin.UIImpl.config

	// 压测form
	timeoutEntry := widget.NewEntry()
	timeoutEntry.PlaceHolder = "毫秒数"
	timeoutEntry.Text = cast.ToString(conf.Pressure.TimeoutMs)

	lpsEntry := widget.NewEntry()
	lpsEntry.PlaceHolder = "并发数量"
	lpsEntry.Text = cast.ToString(conf.Pressure.Concurrency)

	durationEntry := widget.NewEntry()
	durationEntry.PlaceHolder = "秒数"
	durationEntry.Text = cast.ToString(conf.Pressure.DurationS)

	form := widget.NewForm(
		widget.NewFormItem("超时(ms)", timeoutEntry),
		widget.NewFormItem("并发量", lpsEntry),
		widget.NewFormItem("持续时间(s)", durationEntry),
	)
	form.OnSubmit = func() {
		pressure := conf.Pressure
		pressure.TimeoutMs = cast.ToInt32(timeoutEntry.Text)
		pressure.Concurrency = 10
		pressure.DurationS = 2
		conf.Pressure = pressure
		if err := globalWin.UIImpl.storage.StoreConfig(conf); err != nil {
			logrus.Error(err)
		}
	}
	form.SubmitText = "确定"

	// 计算器
	concurrencyEntry := widget.NewEntry()
	computerForm := widget.NewForm(
		widget.NewFormItem("并发", concurrencyEntry),
	)
	computerForm.OnSubmit = func() {
	}
	computerForm.SubmitText = "确定"

	// layout
	configWin.Win = fyne.CurrentApp().NewWindow("压测配置")
	//压测配置
	settingLayout := container.NewBorder(widget.NewLabel("压测配置"), nil, nil, nil, form)
	// computer
	computerLayout := container.NewBorder(widget.NewLabel("并发量计算"), nil, nil, nil, computerForm)
	configWin.Win.SetContent(container.NewGridWithRows(2, settingLayout, computerLayout))
	configWin.Win.Resize(fyne.NewSize(800, 600))
	configWin.Win.CenterOnScreen()

	return &configWin
}
