package formview

import (
	"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 SmithyView struct {
	BaseformView
}

func (s *SmithyView) CreateView(t *model.TestCase) fyne.CanvasObject {
	//查询顾客
	loadCustomer := func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"customer",
			&pb.SmithyCustomerReq{},
		); err != nil {
			logrus.Error(err)
			return
		}
	}

	//交易
	jiaoyi := func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"sell",
			&pb.SmithySellReq{
				EquipIds:   []string{},
				CustomerId: 1,
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	}

	//拒绝
	refuse := func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"refuse",
			&pb.SmithyRefuseReq{
				CustomerId: 1,
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	}

	//图鉴任务
	tujiantask := func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"tasklist",
			&pb.SmithyTasklistReq{},
		); err != nil {
			logrus.Error(err)
			return
		}
	}

	//领任务
	taskId := widget.NewEntry()
	form := widget.NewForm(
		widget.NewFormItem("任务ID", taskId),
	)

	form.OnSubmit = func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"taskaward",
			&pb.SmithyTaskAwardReq{
				TaskId: cast.ToInt32(taskId.Text),
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	}

	receiveTask := func() {
		paiWin := dialog.NewCustom("领任务", "关闭", form, s.w)
		paiWin.Resize(fyne.NewSize(600, 300))
		paiWin.Show()
	}

	customerBtn := widget.NewButton("顾客", loadCustomer)
	refuseBtn := widget.NewButton("拒绝", refuse)
	jiaoyiBtn := widget.NewButton("交易", jiaoyi)
	tujianBtn := widget.NewButton("图鉴任务", tujiantask)
	receiveBtn := widget.NewButton("任务奖励", receiveTask)

	btns := container.NewHBox(customerBtn, jiaoyiBtn, refuseBtn, tujianBtn, receiveBtn)
	c := container.NewBorder(btns, nil, nil, nil)
	return c
}
