package client

import (
	"fmt"
	"github.com/ouxuanserver/osmanthuswine/src/core"
	"hudongzhuanjia/controllers"
	"hudongzhuanjia/models"
	ws_send_service "hudongzhuanjia/services/ws_send"
	"hudongzhuanjia/utils/code"
	"hudongzhuanjia/utils/define"
	"time"
)

//弹幕
type BarrageCtl struct {
	controllers.AuthorCtl
}

//发送弹幕
func (t *BarrageCtl) Send() {
	uid := t.MustGetUID()
	customerId := t.MustGetCustomerId()
	activityId := t.MustGetActivityId()
	content := t.MustGet("content")

	//检查内容是否包含敏感
	if models.IsSensitive(content) {
		t.ERROR("内容包含敏感字", code.MSG_ERR)
	}

	//查询该活动的所属客户
	activity := new(models.Activity)
	exist, err := models.GetById(activity, activityId)
	t.CheckErr(err)
	t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")

	//查询当前的用户信息
	user := new(models.User)
	exist, err = models.GetById(user, uid)
	t.CheckErr(err)
	t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")

	//插入弹幕消息
	_, err = core.GetXormAuto().InsertOne(&models.BarrageHistory{
		ActivityId: activityId,
		UserId:     uid,
		Content:    content,
		CreateAt:   time.Now(),
		UpdateAt:   time.Now(),
	})
	t.CheckErr(err)

	customer := new(models.Customer)
	exist, err = models.GetById(customer, customerId)
	t.CheckErr(err)
	t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")

	go ws_send_service.SendBarrage(fmt.Sprintf("%d", activity.Id),
		define.TYPE_CUSTOMER, 0, map[string]interface{}{
			"type":        "barrage",
			"customer_id": 0,
			"data": map[string]interface{}{
				"avatar":  user.Avatar,
				"content": content,
			},
		})

	t.SUCCESS("发送成功")
}