package formview

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

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

type BingoView struct {
	BaseformView
}

func (this *BingoView) CreateView(t *model.TestCase) fyne.CanvasObject {
	var (
		gridContainer *fyne.Container
		ttxt          string //T text
		aSel          *widget.Select
	)

	tEntry := widget.NewEntry()
	tEntry.PlaceHolder = "配置ID"
	tEntry.OnChanged = func(s string) {
		ttxt = s
	}

	nEntry := widget.NewEntry()
	nEntry.PlaceHolder = "数量"

	attrSel := &widget.Select{}
	attrSel.Options = []string{"选择", "gold", "diamond", "exp", "moongold", "friendPoint", "ps", "guildcoin", "arenacoin", "merchantmoney"}
	attrSel.SetSelected("选择")
	attrSel.OnChanged = func(s string) {
		ttxt = s
	}

	aSel = &widget.Select{}
	gridContainer = container.NewGridWithColumns(3, aSel, tEntry, nEntry)

	aSel.Options = []string{"选择", "attr", "item", "hero", "equi", "mapid", "pataid", "alltask", "worldtask", "sociatyexp", "sociatyactivity", "allgrowtask", "lv"}
	aSel.SetSelected("选择")
	aSel.OnChanged = func(s string) {
		if s == "attr" {
			gridContainer.Objects[1] = attrSel
			ttxt = attrSel.Selected
		} else {
			gridContainer.Objects[1] = tEntry
			ttxt = tEntry.Text
		}
	}

	this.form.AppendItem(widget.NewFormItem(common.APP_TESTCASE_FORM_BINGO, gridContainer))

	this.form.OnSubmit = func() {
		var bingo string
		if ttxt == "" && nEntry.Text == "" {
			bingo = fmt.Sprintf("bingo:%s", aSel.Selected)
		} else if ttxt != "" && nEntry.Text == "" {
			bingo = fmt.Sprintf("bingo:%s,%s", aSel.Selected, ttxt)
		} else if ttxt == "" && nEntry.Text != "" {
			bingo = fmt.Sprintf("bingo:%s,%s", aSel.Selected, nEntry.Text)
		} else if ttxt != "" && nEntry.Text != "" {
			bingo = fmt.Sprintf("bingo:%s,%s,%s", aSel.Selected, ttxt, nEntry.Text)
		}
		logrus.Info(bingo)
		if aSel.Selected == "选择" || nEntry.Text == "0" {
			dialog.ShowError(errors.New("请填写完整且正确的数值"), this.w)
			return
		}
		if err := service.GetPttService().SendToClient(
			t.MainType,
			t.SubType,
			&pb.GMCmdReq{Cmod: bingo},
		); err != nil {
			logrus.Error(err)
			return
		}

		aSel.SetSelected("")
		attrSel.SetSelected("")
		nEntry.SetText("")
		tEntry.SetText("")
	}

	bingoEntry := widget.NewEntry()
	bingoEntry.PlaceHolder = "bingo:item,10001,1"

	customForm := widget.NewForm(widget.NewFormItem("自定义", bingoEntry))
	customForm.OnSubmit = func() {
		if bingoEntry.Text == "" || !strings.HasPrefix(bingoEntry.Text, "bingo:") {
			dialog.ShowError(errors.New("请填写完整并加上bingo:前缀"), this.w)
			return
		}
		if err := service.GetPttService().SendToClient(
			t.MainType,
			t.SubType,
			&pb.GMCmdReq{Cmod: bingoEntry.Text},
		); err != nil {
			logrus.Error(err)
			return
		}
	}
	customForm.SubmitText = common.BUTTON_OK

	c := container.NewVBox(this.form, customForm)
	return c
}
