package formview

import (
	"go_dreamfactory/cmd/v2/lib/common"
	"go_dreamfactory/cmd/v2/model"
	"go_dreamfactory/cmd/v2/service"
	"go_dreamfactory/pb"

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

type ReputationView struct {
	BaseformView
	itemList *common.ItemList
	flag     bool
}

func (d *ReputationView) CreateView(t *model.TestCase) fyne.CanvasObject {
	d.itemList = common.NewItemList()

	d.itemList.ItemList = d.itemList.CreateList()

	swBtn := widget.NewButton("声望升级", func() {
		//阵营
		zy := widget.NewEntry()
		//好感度
		fv := widget.NewEntry()

		form := widget.NewForm(
			widget.NewFormItem("阵营ID", zy),
			widget.NewFormItem("好感度", fv),
		)

		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"talenttest",
				&pb.ReputationTalenttestReq{
					RaceType:    cast.ToInt32(zy.Text),
					FriendValue: cast.ToInt32(fv.Text),
				},
			); err != nil {
				logrus.Error(err)
				return
			}
		}

		paiWin := dialog.NewCustom("升级", "关闭", form, d.w)
		paiWin.Resize(fyne.NewSize(600, 300))
		paiWin.Show()
	})

	upgradeBtn := widget.NewButton("天赋升级", func() {
		nodeId := widget.NewEntry()
		form := widget.NewForm(
			widget.NewFormItem("节点ID", nodeId),
		)

		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"upgrade",
				&pb.ReputationUpgradeReq{
					NodeId: cast.ToInt32(nodeId.Text),
				},
			); err != nil {
				logrus.Error(err)
				return
			}
		}

		paiWin := dialog.NewCustom("升级", "关闭", form, d.w)
		paiWin.Resize(fyne.NewSize(600, 300))
		paiWin.Show()
	})

	resetBtn := widget.NewButton("重置", func() {

	})

	topBtns := container.NewHBox(swBtn, upgradeBtn, resetBtn)
	c := container.NewBorder(topBtns, nil, nil, nil, d.itemList.ItemList)
	return c
}
