package formview

import (
	"go_dreamfactory/cmd/v2/model"
	"go_dreamfactory/cmd/v2/service"
	"go_dreamfactory/cmd/v2/service/observer"
	"go_dreamfactory/comm"
	// "go_dreamfactory/modules/friend"
	"go_dreamfactory/pb"

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

type FriendZanView struct {
	ListBaseView
}

func (this *FriendZanView) CreateView(t *model.TestCase) fyne.CanvasObject {
	//init required
	this.initItemList()

	receiveBtn := widget.NewButtonWithIcon("获赞", theme.ConfirmIcon(), func() {

		if err := service.GetPttService().SendToClient(
			t.MainType,
			"zanreceive",
			&pb.FriendZanreceiveReq{
				FriendIds: this.selItemIds,
			},
		); err != nil {
			logrus.Error(err)
			return
		}
		this.listBtnFun()
	})

	// new zanlist btn
	zanList := func() {
		if err := service.GetPttService().SendToClient(
			string("friend"),
			"",
			&pb.FriendZanlistReq{}); err != nil {
			logrus.Error(err)
		}
		this.itemListData = model.NewItemModelList()
	}
	zanListBtn := widget.NewButton("点赞列表", zanList)

	// create friend list view
	this.createItemListWithCheck()

	// layout
	split := container.NewHSplit(this.dataListWidget, container.NewVBox(zanListBtn, receiveBtn))
	split.Offset = 1

	this.dataListener()
	return split
}

func (this *FriendZanView) dataListener() {
	this.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
		OnNotify: func(d interface{}, args ...interface{}) {
			data := d.(*pb.UserMessage)
			if !(data.MainType == string("friend") &&
				data.SubType == "") {
				return
			}
			rsp := &pb.FriendZanlistResp{}

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

			this.itemListData = model.NewItemModelList()
			for _, v := range rsp.List {
				fm := &model.ItemModel{
					Id:    v.UserId,
					Label: v.NickName,
				}
				this.itemListData.DataList = append(this.itemListData.DataList, fm)
			}
			this.reloadListData()
		},
	})
}
