package api

import (
	"github.com/gin-gonic/gin"
	"gogs.daxia.dev/huanan/pkg.daxia.dev/validate_utils"
	"gogs.daxia.dev/huanan/pkg.daxia.dev/webutils"
	"nn.daxia.dev/nxd"
	"nn.daxia.dev/online"
)

type RecvInfo struct {
	RecvUserID uint32 `json:"recv_user_id" validate:"required"`

	NotifyType uint32 `json:"notify_type" validate:"required"`

	Content string `json:"content" validate:"required"`
}

type NotifyReq struct {
	RoomID uint32 `json:"room_id" validate:"required"`

	TableID uint32 `json:"table_id" validate:"required"`

	SenderID uint `json:"sender_id" validate:"required"`

	RecvList []RecvInfo `json:"recv_list" validate:"required"`
}

func Notify(ctx *gin.Context) {
	req := NotifyReq{}
	if err := ctx.ShouldBindJSON(&req); err != nil {
		errStr := validate_utils.SetErrorStr(err, func(tag string) string {
			switch tag {
			case "RoomID.required":
				return "房间ID不能为空"
			case "TableID.required":
				return "TableID不能为空"
			case "SenderID.required":
				return "发送者ID不能为空"
			case "RecvList.required":
				return "接收者不能为空"
			}

			return "内部错误"
		})

		webutils.FailedResponse(ctx, errStr(0))
		return
	}

	for _, item := range req.RecvList {
		nxd.SendMsgToUser(item.RecvUserID, online.Msg{
			RoomID:     req.RoomID,
			Offset:     0,
			NotifyType: item.NotifyType,
			Data:       item.Content,
		})
	}

	webutils.SuccessResponse(ctx, nil)
}
