package formview

import (
	"fmt"
	"go_dreamfactory/cmd/v2/lib/common"
	"go_dreamfactory/cmd/v2/model"
	"go_dreamfactory/cmd/v2/service"
	"go_dreamfactory/cmd/v2/service/observer"
	"go_dreamfactory/comm"

	"go_dreamfactory/pb"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"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"
)

type WorldtaskMineView struct {
	mineReq func()
	BaseformView
	itemList  *common.ItemList
	flag      bool
	juqingBtn *widget.Button
}

func (this *WorldtaskMineView) CreateView(t *model.TestCase) fyne.CanvasObject {
	this.itemList = common.NewItemList()
	this.itemList.ItemList = this.itemList.CreateList()

	this.mineReq = func() {
		this.itemList.Reset()
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"mine",
			&pb.WorldtaskMineReq{}); err != nil {
			logrus.Error(err)
		}
	}
	defer this.mineReq()

	// 刷新
	refreshBtn := widget.NewButtonWithIcon("", theme.ViewRefreshIcon(), func() {
		this.itemList.Reset()
		this.mineReq()
	})

	//接取
	acceptBtn := widget.NewButton("接取", func() {
		// groupId := widget.NewEntry()
		// groupId.PlaceHolder = "分组ID"
		taskIdEntry := widget.NewEntry()
		taskIdEntry.PlaceHolder = "任务ID"
		form := widget.NewForm(
			// widget.NewFormItem("组ID", groupId),
			widget.NewFormItem("任务ID", taskIdEntry),
		)

		dconf := dialog.NewCustom("接取任务", "关闭", form, this.w)

		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"accept",
				&pb.WorldtaskAcceptReq{
					TaskId: cast.ToInt32(taskIdEntry.Text),
				}); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
			this.mineReq()
		}
		form.SubmitText = "确定"
		dconf.Resize(fyne.NewSize(400, 200))
		dconf.Show()
	})

	// 完成任务条件
	completeBtn := widget.NewButton("任务条件", func() {
		// groupId := widget.NewEntry()
		// groupId.PlaceHolder = "分组ID"
		taskIdEntry := widget.NewEntry()
		taskIdEntry.PlaceHolder = "任务ID"
		condiId := widget.NewEntry()
		condiId.PlaceHolder = "条件"
		form := widget.NewForm(
			// widget.NewFormItem("组ID", groupId),
			widget.NewFormItem("任务ID", taskIdEntry),
			widget.NewFormItem("完成条件", condiId),
		)

		dconf := dialog.NewCustom("完成条件", "关闭", form, this.w)

		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"completecondi",
				&pb.WorldtaskCompleteCondiReq{
					TaskId:  cast.ToInt32(taskIdEntry.Text),
					CondiId: cast.ToInt32(condiId.Text),
				}); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
			this.mineReq()
		}
		form.SubmitText = "确定"
		dconf.Resize(fyne.NewSize(400, 200))
		dconf.Show()
	})

	// 交付任务
	this.juqingBtn = widget.NewButton("交付任务", func() {
		taskIdEntry := widget.NewEntry()
		taskIdEntry.PlaceHolder = "任务ID"
		form := widget.NewForm(
			widget.NewFormItem("任务ID", taskIdEntry),
		)

		dconf := dialog.NewCustom("交付任务", "关闭", form, this.w)

		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"finish",
				&pb.WorldtaskFinishReq{
					TaskId: cast.ToInt32(taskIdEntry.Text),
				}); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
			this.mineReq()
		}
		form.SubmitText = "确定"
		dconf.Resize(fyne.NewSize(400, 200))
		dconf.Show()
	})

	// 战斗完成
	battleBtn := widget.NewButton("战斗完成", func() {
		confId := widget.NewEntry()
		confId.PlaceHolder = "战斗配置ID"
		taskIdEntry := widget.NewEntry()
		taskIdEntry.PlaceHolder = "任务ID"
		form := widget.NewForm(
			widget.NewFormItem("战斗配置ID", confId),
			widget.NewFormItem("任务ID", taskIdEntry),
		)

		dconf := dialog.NewCustom("战斗完成", "关闭", form, this.w)

		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"worldtask.WorldtaskBattleFinish",
				&pb.WorldtaskBattleFinishReq{
					BattleConfId: cast.ToInt32(confId.Text),
					TaskId:       cast.ToInt32(taskIdEntry.Text),
					Report:       &pb.BattleReport{},
				}); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
			this.mineReq()
		}
		form.SubmitText = "确定"
		dconf.Resize(fyne.NewSize(400, 200))
		dconf.Show()
	})

	// 章节奖励
	chapterBtn := widget.NewButton("章节奖励", func() {
		groupId := widget.NewEntry()
		groupId.PlaceHolder = "章节ID"
		form := widget.NewForm(
			widget.NewFormItem("章节ID", groupId),
		)
		dconf := dialog.NewCustom("章节奖励", "关闭", form, this.w)
		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"chapterreward",
				&pb.WorldtaskChapterrewardReq{
					GroupId: cast.ToInt32(groupId.Text),
				}); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
		}
		form.SubmitText = "确定"

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

	mt := common.NewMogoTool()
	mt.OpenConn()

	// 任务查询
	queryTaskBtn := widget.NewButton("任务查询", func() {
		taskIdEntry := widget.NewEntry()
		form := widget.NewForm(
			widget.NewFormItem("任务ID", taskIdEntry),
		)

		dconf := dialog.NewCustom("任务查询", "关闭", form, this.w)
		form.OnSubmit = func() {
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"chapterreward",
				&pb.WorldtaskChapterrewardReq{}); err != nil {
				logrus.Error(err)
			}
			dconf.Hide()
		}
		form.SubmitText = "确定"

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

	barBtn := container.NewHBox(refreshBtn, acceptBtn, completeBtn, this.juqingBtn, battleBtn, chapterBtn, layout.NewSpacer(), queryTaskBtn)

	c := container.NewBorder(barBtn, nil, nil, nil, this.itemList.ItemList)
	this.mineData()
	return c
}

func (this *WorldtaskMineView) mineData() {
	if this.flag {
		return
	}
	this.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
		OnNotify: func(d interface{}, args ...interface{}) {
			data := d.(*pb.UserMessage)

			if !(data.MainType == "worldtask" &&
				data.SubType == "mine") {
				return
			}

			rsp := &pb.WorldtaskMineResp{}
			if !comm.ProtoUnmarshal(data, rsp) {
				logrus.Error("unmarshal err")
				return
			}
			if rsp.Task == nil {
				logrus.Debug(rsp.Task)
				return
			}

			for _, v := range rsp.Task.TaskList {
				item := common.Item{
					Id:   cast.ToString(v),
					Text: fmt.Sprintf("%d ", v),
				}
				this.itemList.AddItem(item)
			}
			this.juqingBtn.Disabled()
			this.juqingBtn.Refresh()
		},
	})
	this.flag = true
}
