package main

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

//QRLimitScene  生成永久二维码的信息
type QRLimitScene struct {
	Name string `json:"action_name"`
	Info struct {
		Scene struct {
			ID    int32  `json:"scene_id"`
			IDstr string `json:"scene_str"` //对于永久二维码，我们使用 字符串， 用来区分 数字为主的 临时二维码
		} `json:"scene"`
	} `json:"action_info"`
}

//QRTmpScene  生成临时二维码的信息
type QRTmpScene struct { //only valid for 30 days
	Name   string `json:"action_name"`    // 'QR_SCENE'
	Expire int32  `json:"expire_seconds"` //max 2592000 = 30 days
	Info   struct {
		Scene struct {
			ID int32 `json:"scene_id"`
		} `json:"scene"`
	} `json:"action_info"`
}

//QRSrcInfo QrCode Information
type QRSrcInfo struct {
	Ticket string `json:"ticket"`
	URL    string `json:"url"`
	Expire int64  `json:"expire_seconds"`
}

//CreateProfileEditQR  give user a temp password to login
//for editing one's own profile
func CreateProfileEditQR() (result QRSrcInfo, err error) {
	// http请求方式: POST
	// URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式：json
	// POST数据例子：{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}
	// 或者也可以使用以下POST数据创建字符串形式的二维码参数：
	// {"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "123"}}}
	u := getURL4CreateQR()
	s := QRLimitScene{}
	//s.Name = "QR_LIMIT_SCENE" //use integer
	s.Name = "QR_LIMIT_STR_SCENE" //use string
	s.Info.Scene.IDstr = "edit_profile"
	j, _ := json.Marshal(s)
	log.Println(string(j))
	resp, err := postJSON(j, u)
	if err != nil {
		return
	}
	//{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"}
	//image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA
	err = json.Unmarshal([]byte(resp), &result)
	return
}

func getURL4CreateQR() (URL string) {
	atk, _ := GetAccessToken()
	u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", atk)
	log.Println(u)
	return u
}

//CreateTempQr  Create a temp QR code
func CreateTempQr(sceneID int32, expire int32) (result QRSrcInfo, err error) {
	// http请求方式: POST
	// URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式：json
	// POST数据例子：{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}
	u := getURL4CreateQR()
	s := QRTmpScene{}
	s.Name = "QR_SCENE" //
	s.Info.Scene.ID = sceneID
	if expire <= 0 || expire >= 259200 {
		log.Println("invalid expire time for Temp QR code, set to 30days instead")
		s.Expire = 2592000
	} else {
		s.Expire = expire
	}

	j, _ := json.Marshal(s)
	log.Println(string(j))
	resp, err := postJSON(j, u)
	if err != nil {
		return
	}
	//{"ticket":"gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA","url":"http:\/\/weixin.qq.com\/q\/02K_TnQ9gucSh10000M03N"}
	//image: https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEm8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyS19UblE5Z3VjU2gxMDAwME0wM04AAgRMYh1ZAwQAAAAA
	err = json.Unmarshal([]byte(resp), &result)
	return
}

func qrImgByTicket(ticket string) (url string) {
	return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + ticket
}
