package formview

import (
	"fmt"
	os_storage "go_dreamfactory/cmd/v2/lib/storage"
	"go_dreamfactory/cmd/v2/model"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cast"
)

// 全局实例配置
// redisDb mongoDb

type GlobalConfView struct {
	BaseformView
	storage os_storage.Storage
	conf    *os_storage.Config
}

func (this *GlobalConfView) CreateView(t *model.TestCase) fyne.CanvasObject {
	// load
	this.storage, _ = os_storage.NewOSStorage()
	var err error
	this.conf, err = this.storage.LoadConfig()
	if err != nil {
		logrus.Error(err)
		return &fyne.Container{}
	}

	// 表单
	createForm := func() *widget.Form {
		name := widget.NewEntry()
		mongoUrl := widget.NewEntry()
		port := widget.NewEntry()
		user := widget.NewEntry()
		passd := widget.NewEntry()
		mongoDatabase := widget.NewEntry()
		mongoForm := widget.NewForm(
			widget.NewFormItem("Name", name),
			widget.NewFormItem("Addr", container.NewBorder(nil, nil, nil, port, mongoUrl)),
			widget.NewFormItem("User", user),
			widget.NewFormItem("Pass", passd),
			widget.NewFormItem("DBName", mongoDatabase),
		)
		return mongoForm
	}

	center := container.NewHBox()

	topBtn := widget.NewButton("新建", func() {
		//mongo form
		mongoForm := createForm()
		dconf := dialog.NewCustom("创建数据源", "关闭", mongoForm, this.w)
		mongoForm.OnSubmit = func() {
			if this.conf.MultiMgo == nil {
				this.conf.MultiMgo = make(map[string]*os_storage.MgoDB)
			}
			name := mongoForm.Items[0]
			mongoUrl := mongoForm.Items[1]
			port := mongoForm.Items[2]
			user := mongoForm.Items[3]
			passd := mongoForm.Items[4]
			mongoDatabase := mongoForm.Items[5]
			if _, ok := this.conf.MultiMgo[name.Text]; ok {
				dialog.NewInformation("提示", name.Text+" 数据源已存在", this.w)
				return
			}

			this.conf.MultiMgo[name.Text] = &os_storage.MgoDB{
				Name:     name.Text,
				Host:     mongoUrl.Text,
				Port:     cast.ToInt32(port.Text),
				User:     user.Text,
				Password: passd.Text,
				Database: mongoDatabase.Text,
			}
			if err := this.storage.StoreConfig(this.conf); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
			center.Refresh()
		}
		mongoForm.SubmitText = "确定"

		dconf.Resize(fyne.NewSize(400, 200))
		dconf.Show()
	})

	editBtn := widget.NewButtonWithIcon("", theme.SettingsIcon(), func() {})
	// 创建Card
	for k, v := range this.conf.MultiMgo {
		data := binding.BindStringList(&[]string{})
		data.Append(fmt.Sprintf("%-3s\t: %s", "addr", strings.Join([]string{v.Host, cast.ToString(v.Port)}, ":")))
		data.Append(fmt.Sprintf("user:%v", v.User))
		data.Append(fmt.Sprintf("pass:%v", v.Password))
		data.Append(fmt.Sprintf("DB:%v", v.Database))
		dataList := widget.NewListWithData(data,
			func() fyne.CanvasObject {
				return widget.NewLabel("template")
			},
			func(i binding.DataItem, o fyne.CanvasObject) {
				o.(*widget.Label).Bind(i.(binding.String))
			},
		)
		
		obj := widget.NewCard("", "",
			container.NewBorder(container.NewHBox(widget.NewLabel(k), layout.NewSpacer(), editBtn), nil, nil, nil, dataList),
		)
		obj.Resize(fyne.NewSize(400, 450))
		center.AddObject(obj)
	}

	c := container.NewBorder(container.NewHBox(topBtn), nil, nil, nil, container.NewWithoutLayout(container.NewHBox(center)))
	return c
}
