package lib

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func yidong_token() (string,bool) {

	url := "https://api.iot.10086.cn/v5/ec/get/token?transid=851AIOT202204251553361888100000003&password=L44tB,J5EQhj&appid=851AIOT2022042515533618881&refresh=0"
	method := "GET"

	client := &http.Client {
	}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return "",false
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return "",false
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return "",false
	}
	fmt.Println(string(body))


	type Token struct {
		Token string `json:"token"`
	}
	type TestJson struct {
		Result []Token `json:"result"`
	}

	var c TestJson
	err = json.Unmarshal(body, c)
	if err := json.Unmarshal([]byte(body), &c); err != nil {
		fmt.Println("Error =", err)
	}

	fmt.Println("bodyjson:",c)

	//dd := c["result"]
	if(len(c.Result) == 0){
		return "",false
	}
	fmt.Println("Token:",c.Result[0].Token)

	return c.Result[0].Token,true

}


// 实时查询群组本月套餐内GPRS流量使用量信息。
type FlowPoolSharingInfo struct {
	TotalAmount string `json:"totalAmount"`
	RemainAmount string `json:"remainAmount"`
	UseAmount string `json:"useAmount"`
}
func Yidong_group_data_margin() (FlowPoolSharingInfo,bool) {
	token,token_is := yidong_token()
	if !token_is{
		return FlowPoolSharingInfo{},false
	}
	url := "https://api.iot.10086.cn/v5/ec/query/group-data-margin?transid=851AIOT202204251553361888100000003&token="+token+"&groupId=9921000021089000"
	method := "GET"

	client := &http.Client {
	}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return FlowPoolSharingInfo{},false
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return FlowPoolSharingInfo{},false
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return FlowPoolSharingInfo{},false
	}
	fmt.Println(string(body))


	type List_data struct {
		FlowPoolSharingInfo []FlowPoolSharingInfo `json:"flowPoolSharingInfo"`
	}
	type TestJson struct {
		Result []List_data `json:"result"`
	}

	var c TestJson
	err = json.Unmarshal(body, c)
	if err := json.Unmarshal([]byte(body), &c); err != nil {
		fmt.Println("Error =", err)
	}

	fmt.Println("bodyjson:",c)

	//dd := c["result"]
	if(len(c.Result) == 0){
		return FlowPoolSharingInfo{},false
	}
	//fmt.Println("Token:",c.Result[0].UseAmount)

	return c.Result[0].FlowPoolSharingInfo[0],true

}

// 实时查询物联卡本月套餐内流量使用量。
func yidong_sim_data_usage(msisdn string) (string,bool) {
	token,token_is := yidong_token()
	if !token_is{
		return "",false
	}
	url := "https://api.iot.10086.cn/v5/ec/query/sim-data-usage?transid=851AIOT202204251553361888100000003&token="+token+"&msisdn="+msisdn
	method := "GET"

	client := &http.Client {
	}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return "",false
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return "",false
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return "",false
	}
	fmt.Println(string(body))


	type List_data struct {
		DataAmount string `json:"dataAmount"`
	}
	type TestJson struct {
		Result []List_data `json:"result"`
	}

	var c TestJson
	err = json.Unmarshal(body, c)
	if err := json.Unmarshal([]byte(body), &c); err != nil {
		fmt.Println("Error =", err)
	}

	fmt.Println("bodyjson:",c)

	//dd := c["result"]
	if(len(c.Result) == 0){
		return "",false
	}
	//fmt.Println("Token:",c.Result[0].UseAmount)

	return c.Result[0].DataAmount,true

}


//
//
//flowPoolSharingInfo,_ := yidong_group_data_margin()
//println("总量:",flowPoolSharingInfo.RemainAmount)
//println("使用:",flowPoolSharingInfo.RemainAmount)
//println("剩余:",flowPoolSharingInfo.RemainAmount)
//
//sim_data_usage,_ := yidong_sim_data_usage("1440761145498")
//println("单卡使用量:",sim_data_usage)

