package pay

import (
	"crypto/tls"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"time"
)

var (
	PackageName  = "com.DefaultCompany.PetHomeMergeStory" //包名
	GrantType    = "refresh_token"
	ClientId     = "757279522891-nrfqc345ng13d92br5cd579e1k7f2jsj.apps.googleusercontent.com"                                //客户端id
	ClientSecret = "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"                                                                     //客户端秘钥
	RefreshToken = "1//0ewdqw7P83uNRCgYIARAAGA4SNwF-L9IreCVIYzd-6T5Cz0J-UoAqdbGL7AmB849q_YKy7CbvJIjmvwQelezgs1utmU4nDI4_kWU" //RefreshToken，上面第二步获取的
)

const (
	PAY_STATUS_SUCCESS = 1
	PAY_STATUS_FAIL    = 0
)

type TokenInfo struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
	Scope       string `json:"scope"`
	TokenType   string `json:"token_type"`
}

type OrderInfo struct {
	Kind                 string `json:"kind"`
	PurchaseTimeMillis   string `json:"purchaseTimeMillis"`   // 支付时间, 毫秒
	PurchaseState        int    `json:"purchaseState"`        // 是否付费: 0 已支付, 1 取消
	ConsumptionState     int    `json:"consumptionState"`     // 是否被消费: 0 未消费, 1 已消费
	DeveloperPayload     string `json:"developerPayload"`     // 开发者透传参数
	OrderId              string `json:"orderId"`              // 谷歌订单号
	AcknowledgementState int    `json:"acknowledgementState"` // 支付类型:  0 测试, 1 真实
}

// 获取token凭证
func PostRefreshToken() (*TokenInfo, error) {
	client := &http.Client{
		Timeout: 10 * time.Second,
	}

	var resp *http.Response
	var err error
	for i := 0; i < 3; i++ { // 重试3次
		resp, err = client.PostForm("https://accounts.google.com/o/oauth2/token", url.Values{
			"grant_type":    {GrantType},
			"client_id":     {ClientId},
			"client_secret": {ClientSecret},
			"refresh_token": {RefreshToken},
		})
		if err == nil {
			break
		}
		fmt.Printf("PostToken attempt %d failed: %v\n", i+1, err)
		time.Sleep(2 * time.Second) // 等待2秒后重试
	}
	if err != nil {
		return nil, fmt.Errorf("PostToken err:%v", err)
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("PostToken body err:", err)
	}
	fmt.Println("token:", string(body))
	info := new(TokenInfo)
	err = json.Unmarshal(body, &info)
	if err != nil {
		return nil, fmt.Errorf("PostToken Unmarshal err:%v", err)
	}
	fmt.Println("token info:", info)
	return info, nil
}

// 获取订单信息
func GetOrder(productId, token, accessToken string) (*OrderInfo, error) {
	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				Certificates: []tls.Certificate{},
			},
		},
	}
	resp, err := client.Get("https://www.googleapis.com/androidpublisher/v3/applications/" +
		PackageName + "/purchases/products/" + productId + "/tokens/" + token + "?access_token=" + accessToken + "")
	if err != nil {
		return nil, fmt.Errorf("GetOrder err:%v", err)
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("GetOrder body err:", err)
	}
	fmt.Println("GetOrder：", string(body))
	info := new(OrderInfo)
	err = json.Unmarshal(body, &info)
	if err != nil {
		return nil, fmt.Errorf("GetOrder Unmarshal err:%v", err)
	}
	fmt.Println("GetOrder info:", info)
	return info, nil
}
