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/widget"
	"github.com/sirupsen/logrus"
)

type FriendBlacklistView struct {
	ListBaseView
}

func (this *FriendBlacklistView) CreateView(t *model.TestCase) fyne.CanvasObject {
	// init required
	this.initItemList()
	// create friend list view
	this.createItemListWithCheck()

	friendList := func() {
		if err := service.GetPttService().SendToClient(
			string("friend"),
			"",
			&pb.FriendBlackListReq{}); err != nil {
			logrus.Error(err)
		}
		this.itemListData = model.NewItemModelList()
	}
	friendListBtn := widget.NewButton("黑名单列表", friendList)

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

	//data listener for
	this.dataListener()

	return split
}

func (this *FriendBlacklistView) 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.FriendBlackListResp{}

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

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