package user

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
	"microGame/app/usercenter/cmd/api/internal/svc"
	"microGame/app/usercenter/cmd/api/internal/types"
	"microGame/app/usercenter/cmd/rpc/pb/usercenter"
)

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

// NewLoginLogic login
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
	fmt.Printf("Login1输出：%+v\n", req)
	logc.Errorf(l.ctx, "Login2输出：%+v\n", req)

	//l.ctx = metadata.AppendToOutgoingContext(l.ctx, "Authorization", "Bearer 3333333333")

	loginResp, err := l.svcCtx.UserRpc.Login(l.ctx, &usercenter.LoginReq{
		Username: req.Username,
		Password: req.Password,
	})
	if err != nil {
		return nil, err
	}
	fmt.Printf("loginResp 输出：%+v\n", loginResp)

	msg, _ := json.Marshal(loginResp)
	RErr := l.svcCtx.RabbitSender.Send("poker-bet", "poker-bet", msg)
	if RErr != nil {
		logc.Errorf(l.ctx, "Login-RabbitSender：%+v\n", RErr)
	}
	fmt.Printf("RabbitSender 输出：%+v\n", RErr)
	//_ = copier.Copy(&resp, loginResp)
	resp = &types.LoginResp{
		AccessToken:  loginResp.AccessToken,
		RefreshAfter: loginResp.RefreshAfter,
		Nickname:     "go air",
		Username:     req.Username,
		Id:           loginResp.Uid,
	}

	//detailResp, err := l.svcCtx.UserRpc.GetUserInfo(l.ctx, &usercenter.UserInfoReq{
	//	UserId: 1,
	//})
	//if err != nil {
	//	return nil, err
	//}
	//value := l.ctx.Value("jwtUserId")
	//logc.Info(l.ctx, value)
	//resp = &types.LoginResp{
	//	Id:       detailResp.Uid,
	//	Username: detailResp.Username,
	//}

	return resp, nil
}
