package community

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/logic/yb_community_question"
	"hongze/hongze_yb/models/request"
	responseModel "hongze/hongze_yb/models/response"
	"hongze/hongze_yb/models/tables/yb_community_question_comment"
	"hongze/hongze_yb/services"
	userService "hongze/hongze_yb/services/user"
	"strconv"
)

// Comment 发布留言
func Comment(c *gin.Context) {
	// 是否为备用小程序
	copyYb := c.Request.Header.Get("CopyYb")
	var req request.ReqComment
	if c.ShouldBind(&req) != nil {
		response.Fail("入参错误", c)
		return
	}
	userInfo := userService.GetInfoByClaims(c)

	// 随机头像
	var qaAvatarUrl string
	avatar, e := yb_community_question_comment.GetUserAvatarRandom()
	if e != nil {
		response.FailMsg("发布失败", e.Error(), c)
		return
	}
	qaAvatarUrl = avatar.AvatarUrl

	req.IsShowName = 0
	if userInfo.NickName != `` { //非空串时为实名
		req.IsShowName = 1
	}
	// 不传来源默认为问答社区
	if req.Source == 0 {
		req.Source = 1
	}

	ybCommunityQuestionComment, err, errMsg := yb_community_question.Comment(userInfo, req.CommunityQuestionID, req.Content, req.SourceAgent, req.IsShowName, req.Source, qaAvatarUrl, copyYb)
	if err != nil {
		response.FailMsg(errMsg, err.Error(), c)
		return
	}
	response.OkData("留言成功", responseModel.RespCommunityQuestionCommentAdd{
		CommunityQuestionCommentID: ybCommunityQuestionComment.CommunityQuestionCommentID,
	}, c)
	return
}

// DeleteComment 删除留言
func DeleteComment(c *gin.Context) {
	var req *request.ReqDel
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}
	userInfo := userService.GetInfoByClaims(c)
	err, errMsg := yb_community_question.Delete(userInfo, req.CommunityQuestionCommentID)
	if err != nil {
		response.FailMsg(errMsg, err.Error(), c)
		return
	}
	response.Ok("删除留言成功", c)
	return
}

// HotList 获取精选留言
func HotList(c *gin.Context) {
	communityQuestionIDStr := c.DefaultQuery("community_question_id", "")
	communityQuestionID, err := strconv.Atoi(communityQuestionIDStr)
	if err != nil {
		response.Fail("问答id异常", c)
		return
	}
	// 来源: 默认为问答社区
	reqSource := c.DefaultQuery("source", "1")
	source, _ := strconv.Atoi(reqSource)
	if source == 0 {
		source = 1
	}

	userinfo := userService.GetInfoByClaims(c)
	page := services.GetCurrPageByClaims(c)
	pageSize := services.GetPageSizeByClaims(c)

	list, hotTotal, myTotal, err, errMsg := yb_community_question.List(userinfo.UserID, communityQuestionID, source, true, page, pageSize)
	if err != nil {
		response.FailMsg(errMsg, err.Error(), c)
		return
	}
	response.OkData("查询成功", responseModel.RespCommunityQuestionCommentList{
		List:     list,
		Paging:   responseModel.GetPaging(page, pageSize, int(hotTotal)),
		HotTotal: hotTotal,
		MyTotal:  myTotal,
	}, c)

	return
}

// MyList 获取我的留言
func MyList(c *gin.Context) {
	communityQuestionIDStr := c.DefaultQuery("community_question_id", "")
	communityQuestionID, err := strconv.Atoi(communityQuestionIDStr)
	if err != nil {
		response.Fail("问答id异常", c)
		return
	}
	// 来源: 默认为问答社区
	reqSource := c.DefaultQuery("source", "1")
	source, _ := strconv.Atoi(reqSource)
	if source == 0 {
		source = 1
	}
	userinfo := userService.GetInfoByClaims(c)
	page := services.GetCurrPageByClaims(c)
	pageSize := services.GetPageSizeByClaims(c)

	list, hotTotal, myTotal, err, errMsg := yb_community_question.MyList(userinfo.UserID, communityQuestionID, source, page, pageSize)
	if err != nil {
		response.FailMsg(errMsg, err.Error(), c)
		return
	}
	response.OkData("查询成功", responseModel.RespCommunityQuestionCommentList{
		List:     list,
		Paging:   responseModel.GetPaging(page, pageSize, int(myTotal)),
		HotTotal: hotTotal,
		MyTotal:  myTotal,
	}, c)
	return
}

// GetNeedCommentAnonymousUserTips 获取是否 弹出让去设置头像 的提示框
func GetNeedCommentAnonymousUserTips(c *gin.Context) {
	userinfo := userService.GetInfoByClaims(c)

	// true 不弹框，false弹框
	if userinfo.NickName != `` {
		response.OkData("获取成功", true, c)
		return
	}
	ok, err := yb_community_question.GetNeedCommentAnonymousUserTips(userinfo.UserID)
	if err != nil {
		response.FailMsg("获取失败", err.Error(), c)
		return
	}

	response.OkData("获取成功", ok, c)
}

// SetCommentAnonymousUserTips 设置不再提示 弹出让去设置头像 的提示框
func SetCommentAnonymousUserTips(c *gin.Context) {
	userinfo := userService.GetInfoByClaims(c)
	err := yb_community_question.SetYbCommentAnonymousUserTips(userinfo.UserID)
	if err != nil {
		response.FailMsg("设置失败", err.Error(), c)
		return
	}
	response.Ok("设置成功", c)
}
