package wx

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/medivhzhan/weapp/v2"
	"io/ioutil"
	"time"
)

func GetUnlimited(token string, scene string, page string, width int, colorR string, colorG string, colorB string) (string, error) {
	getter := weapp.UnlimitedQRCode{
		Scene:     scene,
		Page:      page,
		Width:     width,
		AutoColor: true,
		LineColor: weapp.Color{colorR, colorG, colorB},
		IsHyaline: true,
	}

	fmt.Println(token)
	reply, _ := json.Marshal(getter)
	fmt.Println(string(reply))
	resp, res, err := getter.Get(token)
	if err != nil {
		// 处理一般错误信息
		return "", err
	}

	if err := res.GetResponseError(); err != nil {
		// 处理微信返回错误信息
		return "", err
	}
	defer resp.Body.Close()

	content, err := ioutil.ReadAll(resp.Body)
	sEnc := base64.StdEncoding.EncodeToString(content)
	// 处理图片内容
	return sEnc, nil
}

func GetScheme(token string, path string, query string) string {
	schemedInfo := weapp.SchemedInfo{
		Path:  path,
		Query: query,
	}
	getter := weapp.URLScheme{
		SchemedInfo: &schemedInfo,
		ExpireTime:  time.Now().AddDate(0, 1, 0).Unix(),
		IsExpire:    true,
	}

	res, err := getter.Generate(token)
	if err != nil {
		// 处理一般错误信息
		return err.Error()
	}

	if err := res.GetResponseError(); err != nil {
		// 处理微信返回错误信息
		return err.Error()
	}
	//defer resp.Body.Close()

	//content, err := ioutil.ReadAll(resp.Body)
	//sEnc := base64.StdEncoding.EncodeToString(content)
	// 处理图片内容
	return res.Openlink
}
