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"
	"time"

	"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 DispatchView struct {
	BaseformView
	itemList   *common.ItemList
	flag       bool
	noticeLabl *widget.Label //公告信息
}

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

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

	//选择任务
	d.itemList.ItemList.OnSelected = func(id widget.ListItemID) {
		item := d.itemList.CachedList.Items[id]
		d.itemList.SelItemId = item.Id
		logrus.Debug(item)

	}

	//玩家公告
	d.noticeLabl = new(widget.Label)
	noticeFun := func() {
		d.itemList.Reset()
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"notice",
			&pb.DispatchNoticeReq{},
		); err != nil {
			logrus.Error(err)
			return
		}
	}
	noticeBtn := widget.NewButton("玩家公告", func() {
		noticeFun()
	})
	//刷新公告
	refresh := func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"refresh",
			&pb.DispatchRefreshReq{},
		); err != nil {
			logrus.Error(err)
			return
		}
		noticeFun()
	}
	refreshBtn := widget.NewButton("刷新公告", func() {
		refresh()
	})
	// 测试刷新
	testRefreshBtn := widget.NewButton("测试重复", func() {
		for i := 0; i < 100; i++ {
			refresh()
			time.Sleep(time.Millisecond * 200)
		}
	})

	//领取奖励
	receiveBtn := widget.NewButton("领取奖励", func() {
		if d.itemList.SelItemId == "" {
			logrus.Debug("参数是空")
			return
		}
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"receive",
			&pb.DispatchReceiveReq{
				TaskId: cast.ToInt32(d.itemList.SelItemId),
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	})
	//一键领取奖励
	autoReceiveBtn := widget.NewButton("一键领取", func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"autoreceive",
			&pb.DispatchAutoReceiveReq{},
		); err != nil {
			logrus.Error(err)
			return
		}
	})
	//派遣
	hero1 := widget.NewEntry()
	hero2 := widget.NewEntry()
	form := widget.NewForm(
		widget.NewFormItem("英雄1", hero1),
		widget.NewFormItem("英雄2", hero2),
	)

	paiqianBtn := widget.NewButton("派遣", func() {
		//弹窗
		form.OnSubmit = func() {
			if hero1.Text == "" && hero2.Text == "" {
				logrus.Debug("heroId参数不能为空")
				return
			}

			heroIds := []string{}
			if hero1.Text != "" {
				heroIds = append(heroIds, hero1.Text)
			}
			if hero2.Text != "" {
				heroIds = append(heroIds, hero2.Text)
			}
			if err := service.GetPttService().SendToClient(
				t.MainType,
				"do",
				&pb.DispatchDoReq{
					TaskId:  cast.ToInt32(d.itemList.SelItemId),
					HeroIds: heroIds,
				},
			); err != nil {
				logrus.Error(err)
				return
			}
		}
		form.SubmitText = "派遣"
		paiWin := dialog.NewCustom("派遣", "关闭", form, d.w)
		paiWin.Resize(fyne.NewSize(600, 300))
		paiWin.Show()
	})

	//周奖励领取
	weekReceiveBtn := widget.NewButton("周奖励", func() {
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"weekrecive",
			&pb.DispatchWeekReciveReq{
				Idx: 1,
			},
		); err != nil {
			logrus.Error(err)
			return
		}

	})

	top := container.NewHBox(noticeBtn, refreshBtn, testRefreshBtn, receiveBtn,
		autoReceiveBtn, paiqianBtn, weekReceiveBtn)
	bottom := container.NewHBox(d.noticeLabl)
	c := container.NewBorder(top, bottom, nil, nil, d.itemList.ItemList)
	d.noticeList()
	return c
}

func (a *DispatchView) noticeList() {
	if a.flag {
		return
	}

	a.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
		OnNotify: func(d interface{}, args ...interface{}) {
			data := d.(*pb.UserMessage)
			if data.MainType == string("dispatch") &&
				data.SubType == "notice" {

				rsp := &pb.DispatchNoticeResp{}

				if !comm.ProtoUnmarshal(data, rsp) {
					logrus.Error("unmarshal err")
					return
				}

				if rsp.Dispatch == nil {
					return
				}

				//验证结果是否有重复
				tidMap := make(map[int32]int32)
				var r bool
				for _, v := range rsp.Dispatch.Tasks {
					if _, ok := tidMap[v.TaskId]; ok {
						r = true
					}
					tidMap[v.TaskId] = v.TaskId
					t1 := time.Unix(v.Duration, 0)
					lt1 := t1.Format("15:04:05")
					t2 := time.Unix(v.LeftTime, 0)
					lt2 := t2.Format("15:04:05")
					item := common.Item{
						Id:   cast.ToString(v.TaskId),
						Text: fmt.Sprintf("ID:%v 状态:%v 任务截至:%v 派遣截至:%v ", v.TaskId, v.Status, lt1, lt2),
						Data: v,
					}
					a.itemList.AddItem(item)
				}

				a.noticeLabl.SetText(fmt.Sprintf("公告等级:%v 免费次数:%v 刷新次数:%v 任务数:%v 周任务数:%v 周奖励领取:%v 重复:%v",
					rsp.Dispatch.Lv,
					rsp.Dispatch.FreeCount,
					rsp.Dispatch.RefreshCount,
					rsp.Dispatch.TaskCount,
					rsp.Dispatch.WeekCount,
					rsp.Dispatch.WeekReceived,
					r,
				))
				a.noticeLabl.Refresh()
			}
		},
	})
	a.flag = true
}
