package whatsapp

import (
	"context"
	"wechat-api/hook/aliyun"

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

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

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

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

func (l *GetQrcodeLogic) GetQrcode(req *types.GetQrcodeReq) (*types.GetQrcodeResp, error) {
	resp := types.GetQrcodeResp{}
	result, err := aliyun.GetCamsQrcode(*req.Phone, *req.WaId)
	l.Logger.Infof("result=%v err=%v\n", result, err)

	if err != nil {
		resp.Msg = err.Error()
		resp.Code = 1
	} else {
		if result.Body.Data != nil {
			for _, v := range result.Body.Data {
				generateQrImage := "PNG"
				if v.GenerateQrImage != nil {
					generateQrImage = *v.GenerateQrImage
				}

				resp.Data.Data = append(resp.Data.Data, types.Qrcode{
					Phone:            *v.PhoneNumber,
					QrdlCode:         *v.QrdlCode,
					GenerateQrImage:  generateQrImage,
					PrefilledMessage: *v.PrefilledMessage,
					DeepLinkUrl:      *v.DeepLinkUrl,
					QrImageUrl:       *v.QrImageUrl,
				})
			}
		}
		resp.Data.Total = uint64(len(resp.Data.Data))
	}

	return &resp, nil
}
