package kami_gateway

import (
	"encoding/json"
	"fmt"
	"strconv"
)

type SubmitOrderReq struct {
	OrderPeriod int    `json:"orderPeriod"`
	NotifyUrl   string `json:"notifyUrl"`
	OrderPrice  string `json:"orderPrice"`
	OrderNo     string `json:"orderNo"`
	ProductCode string `json:"productCode"`
	ExValue     string `json:"exValue"`
	Ip          string `json:"ip"`
	PayKey      string `json:"payKey"`
	PaySecret   string `json:"paySecret"`
	Url         string `json:"url"`
}

func (s *SubmitOrderReq) GetUrl() string {
	return fmt.Sprintf("%s/gateway/scan", s.Url)
}

func (s *SubmitOrderReq) ToStrMap() map[string]any {
	data := map[string]any{
		"orderPeriod": strconv.Itoa(s.OrderPeriod),
		"notifyUrl":   s.GetNotifyUrl(),
		"orderPrice":  s.OrderPrice,
		"orderNo":     s.OrderNo,
		"productCode": s.ProductCode,
		"exValue":     s.ExValue,
		"ip":          s.Ip,
		"payKey":      s.PayKey,
	}
	return data
}

func (s *SubmitOrderReq) GetNotifyUrl() string {
	notifyUrl := s.NotifyUrl
	if notifyUrl != "" {
		return notifyUrl
	}
	return fmt.Sprintf("%s/myself/notify", s.Url)
}

type SubmitOrderResponse struct {
	PayKey     string `json:"payKey"`
	StatusCode string `json:"statusCode"`
	Msg        string `json:"msg"`
	Code       int    `json:"code"`
}

type RedeemCardInfo struct {
	FaceType     string `json:"faceType"`               // 面额
	RecoveryType string `json:"recoveryType,omitempty"` // 类型 2 仅卡密 8 卡号卡密
	Data         string `json:"data"`                   // 卡密
	CardNo       string `json:"cardNo,omitempty"`       // 卡号
}

func (d *RedeemCardInfo) ToJson() string {
	jsonStr, _ := json.Marshal(d)
	return string(jsonStr)
}
