package wp_wecom

import (
	"context"
	"encoding/base64"
	"encoding/json"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendMsgLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSendMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendMsgLogic {
	return &SendMsgLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *SendMsgLogic) SendMsg(req *types.WpWecomMsgReq) (resp *types.BaseMsgResp, err error) {
	data := map[string]interface{}{
		"MsgType": "TalkToFriendTask",
		"Content": map[string]interface{}{
			"WxId":        req.WxId,
			"ConvId":      req.ConvId,
			"ContentType": req.ContentType,
			"Content":     base64.StdEncoding.EncodeToString([]byte(*req.Content)),
		},
	}
	jsonStr, err := json.Marshal(data)
	err = l.svcCtx.WechatWs["wecom"].SendMsg([]byte(jsonStr))
	if err != nil {
		return nil, err
	}

	return
}
