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 FriendListView struct {
	friendList func()
	BaseformView
	itemList   *common.ItemList
	flag       bool
	qiecuoFlag bool
	Uid        string //发起者ID
	acceptBtn  *widget.Button
}

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

	this.itemList.ItemList = this.itemList.CreateDefaultCheckList()

	// new friendlist btn
	this.friendList = func() {
		if err := service.GetPttService().SendToClient(
			string("friend"),
			"",
			&pb.FriendListReq{}); err != nil {
			logrus.Error(err)
		}
	}

	defer this.friendList()

	refreshBtn := widget.NewButtonWithIcon("", theme.ViewRefreshIcon(), func() {
		this.itemList.Reset()
		this.friendList()
	})

	//点赞
	zanBtn := widget.NewButtonWithIcon("点赞", theme.ConfirmIcon(), func() {
		if len(this.itemList.SelItemIds) == 0 {
			common.ShowTip("请选择项目")
			return
		}
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"",
			&pb.FriendZanReq{
				FriendIds: this.itemList.SelItemIds,
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	})

	//切磋
	qiecuoBtn := widget.NewButtonWithIcon("切磋", theme.ConfirmIcon(), func() {
		if len(this.itemList.SelItemIds) == 0 {
			common.ShowTip("请选择项目")
			return
		}

		logrus.Debug("selId:", this.itemList.SelItemIds[0])
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"friend.FriendSubTypeQiecuo",
			&pb.FriendQiecuoReq{
				TargetUid: this.itemList.SelItemIds[0],
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	})
	//接受
	this.acceptBtn = widget.NewButton("接受", func() {
		if this.Uid == "" {
			common.ShowTip("发起者UID空")
			return
		}
		if err := service.GetPttService().SendToClient(
			t.MainType,
			"friend.FriendSubTypeAccept",
			&pb.FriendAcceptReq{
				Uid: this.Uid,
			},
		); err != nil {
			logrus.Error(err)
			return
		}
	})

	buttonBar := container.NewHBox(refreshBtn, zanBtn, qiecuoBtn, this.acceptBtn)

	// layout
	// split := container.NewHSplit(this.itemList.ItemList, container.NewVBox(friendListBtn, zanBtn))
	// split.Offset = 1
	c := container.NewBorder(buttonBar, nil, nil, nil, this.itemList.ItemList)
	//data listener for
	this.dataListener()
	this.qiecuoListener()
	return c
}

func (this *FriendListView) Load() {
	this.friendList()
}

func (this *FriendListView) 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 == "") {
				return
			}
			rsp := &pb.FriendListResp{}

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

			for _, v := range rsp.List {
				var status string

				if v.OfflineTime == -1 {
					status = "在线"
				} else {
					status = "离线"
				}
				item := common.Item{
					Id:   v.UserId,
					Text: fmt.Sprintf("%s %s", v.NickName, status),
				}
				this.itemList.AddItem(item)
			}

		},
	})
	this.flag = true
}

func (this *FriendListView) qiecuoListener() {
	if this.qiecuoFlag {
		return
	}

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

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

			this.Uid = rsp.Uid
			this.acceptBtn.SetText("接受" + this.Uid)
			this.acceptBtn.Refresh()
		},
	})

	this.qiecuoFlag = true
}
