package main

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

//get a list of material
type materialQuery struct {
	Type    string `json:"type"`
	Offiset int    `json:"offset"`
	Count   int    `json:"count"`
}

//overall material count
type materialCount struct {
	VoiceCount int64 `json:"voice_count"`
	VideoCount int64 `json:"video_count"`
	ImageCount int64 `json:"image_count"`
	NewsCount  int64 `json:"news_count"`
}

//Material Image Item
type materialItemImage struct {
	MediaID    string `json:"media_id"`
	Name       string `json:"name"`
	UpdateTime int    `json:"update_time"`
	URL        string `json:"url"`
}

type materialImageList struct {
	TotalCount int                 `json:"total_count"`
	ItemCount  int                 `json:"item_count"`
	Items      []materialItemImage `json:"item"`
}

//Material News Item - Article
type materialArticle struct {
	Title              string `json:"title"`
	Author             string `json:"author"`
	Digest             string `json:"digest"`
	Content            string `json:"content"`
	ContentSourceURL   string `json:"content_source_url"`
	ThumbMediaID       string `json:"thumb_media_id"`
	ShowCoverPic       int    `json:"show_cover_pic"`
	URL                string `json:"url"`
	ThumbURL           string `json:"thumb_url"`
	NeedOpenComment    int    `json:"need_open_comment"`
	OnlyFansCanComment int    `json:"only_fans_can_comment"`
}

type materialNewsItem struct {
	MediaID    string `json:"media_id"`
	UpdateTime int    `json:"update_time"`
	Content    struct {
		CreateTime int               `json:"create_time"`
		UpdateTime int               `json:"update_time"`
		NewsItem   []materialArticle `json:"news_item"`
	} `json:"content"`
}

type materialNewsList struct {
	TotalCount int                `json:"total_count"`
	ItemCount  int                `json:"item_count"`
	Items      []materialNewsItem `json:"item"`
}

//
//-- function definition start
//
func getNewList() (jstr string) {
	jstr = getMediaList("news")
	return
}

func getVoiceList() (jstr string) {
	jstr = getMediaList("voice")
	return
}

func getVideoList() (jstr string) {
	jstr = getMediaList("video")
	return
}

func getImageList() (jstr string) {
	jstr = getMediaList("image")
	return
}

func getMediaList(mediaType string) (jstr string) {
	var q = materialQuery{mediaType, 0, 20}
	jsonStr, _ := json.Marshal(q)
	u := url4MaterialList()
	req, err := http.NewRequest("POST", u, bytes.NewBuffer(jsonStr))
	log.Println(u)
	if err != nil {
		return ""
	}

	client := &http.Client{}
	r, err := client.Do(req)
	if err != nil {
		return ""
	}
	defer r.Body.Close()
	b, _ := ioutil.ReadAll(r.Body)
	jstr = string(b)
	return
}

func url4MaterialList() string {
	atk, _ := GetAccessToken()
	u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=%s", atk)
	return u
}

func url4MaterialCount() string {
	atk, _ := GetAccessToken()
	u := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=%s", atk)
	return u
}

func getMaterialCount() (materialCount, error) {
	mc := materialCount{}
	url := url4MaterialCount()
	r, err := http.Get(url)
	if err != nil {
		return mc, err
	}
	defer r.Body.Close()
	b, _ := ioutil.ReadAll(r.Body)
	err = json.Unmarshal(b, &mc)
	return mc, nil
}

func unmarshalImageList(jstr string) (r materialImageList) {
	json.Unmarshal([]byte(jstr), &r)
	return
}

func unmarshalNewsList(jstr string) (r materialNewsList) {
	json.Unmarshal([]byte(jstr), &r)
	return
}
