package originalJd

import (
	"context"
	"encoding/json"
	"kami/internal/consts"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/glog"
)

// AppleRecharge 所有权益充值——下单接口
func (c *Client) AppleRecharge(ctx context.Context, input *AppleRechargeReq) (*AppleRechargeResp, error) {
	var resp = &AppleRechargeResp{}
	glog.Info(ctx, "苹果权益充值", input)

	for range 3 {
		response, err := c.Client.ContentJson().Post(ctx, "http://kami-spider-monorepo:8000/api/jd-anti-risk-spider/app/store", input)
		if err != nil {
			return resp, err
		}

		respData := response.ReadAllString()
		clientResp := AppleRechargeClientResp{}
		glog.Info(ctx, "获取信息", respData)
		if err = json.Unmarshal([]byte(respData), &clientResp); err != nil {
			glog.Error(ctx, "获取信息失败", err)
			return resp, err
		}
		resp = &AppleRechargeResp{
			Code:     clientResp.Code.JDOrderStatus(),
			Deeplink: clientResp.Data.Deeplink,
			OrderId:  clientResp.Data.OrderId,
			PayId:    clientResp.Data.PayId,
			Msg:      clientResp.Message,
		}
		if clientResp.Code == RiskError {
			continue
		}
		if clientResp.Code != Success {
			return resp, gerror.New("苹果权益充值失败，" + clientResp.Message)
		}
		break
	}
	if resp == nil {
		return resp, gerror.New("苹果权益充值失败")
	}
	if resp.Code != consts.CardCookieJDStatusSuccess {
		return resp, gerror.New("苹果权益充值失败" + resp.Msg)
	}
	return resp, nil
}

// GetCardInfo 获取京东订单的卡号和卡密
func (c *Client) GetCardInfo(ctx context.Context, input *AppleRechargeCardInfoReq) (resp *AppleRechargeCardInfoResp, err error) {
	response, err := c.Client.ContentJson().Post(ctx, "http://kami-spider-monorepo:8000/api/jd-anti-risk-spider/query/card", input)
	if err != nil {
		return
	}
	clientResp := struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Data    struct {
			CardPwd     string `json:"card_pwd"`
			CardNo      string `json:"card_num"`
			OrderStatus string `json:"order_status"`
		} `json:"data,omitempty"`
	}{}
	err = json.Unmarshal(response.ReadAll(), &clientResp)
	glog.Info(ctx, "获取京东订单的卡号和卡密", input, clientResp, err)
	if err != nil {
		return
	}
	resp = &AppleRechargeCardInfoResp{
		OrderStatus:  clientResp.Data.OrderStatus,
		Remark:       clientResp.Message,
		CardNo:       clientResp.Data.CardNo,
		IsCkFailed:   clientResp.Code == int(CkError),
		CardPassword: clientResp.Data.CardPwd,
	}
	if clientResp.Code != int(Success) {
		return resp, gerror.New("状态不正确")
	}
	return
}

// RefreshPayment 重新获取收银链接
func (c *Client) RefreshPayment(ctx context.Context, input *RefreshPaymentReq) (*RefreshPaymentRes, error) {
	resp := &RefreshPaymentRes{}
	glog.Info(ctx, "刷新收银台", input)
	response, err := c.Client.ContentJson().Post(ctx, "http://kami-spider-monorepo:8000/api/jd-anti-risk-spider/order/refresh-payment", input)
	if err != nil {
		return resp, err
	}
	clientResp := &AppleRechargeClientResp{}
	err = json.Unmarshal(response.ReadAll(), clientResp)
	glog.Info(ctx, "刷新收银台返回值", clientResp)
	if err != nil {
		return resp, err
	}
	resp = &RefreshPaymentRes{
		Code:     clientResp.Code.JDOrderStatus(),
		Deeplink: clientResp.Data.Deeplink,
		OrderId:  clientResp.Data.OrderId,
		PayId:    clientResp.Data.PayId,
		Msg:      clientResp.Message,
	}
	if clientResp.Code != Success {
		return resp, gerror.New("刷新收银台失败")
	}
	return resp, nil
}
