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/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 FriendRandListView struct {
	BaseformView
	itemList       *common.ItemList
	friendRandList func()
	flag           bool
}

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

	this.itemList.ItemList = this.itemList.CreateList()

	this.friendRandList = func() {
		if err := service.GetPttService().SendToClient(t.MainType, t.SubType,
			&pb.FriendRandlistReq{}); err != nil {
			logrus.Error(err)
		}
	}
	defer this.friendRandList()

	refreshBtn := widget.NewButtonWithIcon("", theme.ViewRefreshIcon(), func() {
		this.itemList.Reset()
		this.friendRandList()
	})
	//好友申请
	friendApplyBtn := widget.NewButton("申请", func() {
		selItemId := this.itemList.SelItemId
		logrus.Debug(selItemId)
		if selItemId == ""{
			common.ShowTip("请选择项目")
			return
		}

		if err := service.GetPttService().SendToClient(t.MainType, "apply", &pb.FriendApplyReq{
			FriendId: selItemId,
		}); err != nil {
			logrus.Error(err)
		}
	})

	nicknameEntry := widget.NewEntry()
	nicknameEntry.PlaceHolder = "搜索昵称"
	searchBtn := widget.NewButtonWithIcon("", theme.SearchIcon(), func() {
		if nicknameEntry.Text == "" {
			common.ShowTip("请输入昵称")
			return
		}
		this.itemList.Reset()
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"",
			&pb.FriendSearchReq{NickName: nicknameEntry.Text},
		); err != nil {
			logrus.Error(err)
			return
		}
	})

	btnBar := container.NewBorder(nil, nil, container.NewHBox(refreshBtn, friendApplyBtn), searchBtn, nicknameEntry)

	// layout
	c := container.NewBorder(btnBar, nil, nil, nil, this.itemList.ItemList)

	this.dataListener()
	return c
}

func (this *FriendRandListView) dataListener() {
	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 == string("friend") && data.SubType == "" {
				rsp := &pb.FriendRandlistResp{}
				if !comm.ProtoUnmarshal(data, rsp) {
					logrus.Error("unmarshal err")
					return
				}
				for i, v := range rsp.List {
					item := common.Item{
						Id:   v.UserId,
						Text: fmt.Sprintf("%d 昵称:%s 服务:%s", i+1, v.NickName, v.ServerId),
					}
					this.itemList.AddItem(item)
				}
			} else if data.MainType == string("friend") && data.SubType == "" {
				rsp := &pb.FriendSearchResp{}
				if !comm.ProtoUnmarshal(data, rsp) {
					logrus.Error("unmarshal err")
					return
				}
				for i, v := range rsp.Friends {
					item := common.Item{
						Id:   v.UserId,
						Text: fmt.Sprintf("%d 昵称:%s 服务:%s", i+1, v.NickName, v.ServerId),
					}
					this.itemList.AddItem(item)
				}
			}

		},
	})
	this.flag = true
}
