package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
)

//menu is a json definition
var menu = []byte(`
{
	 "button":[
      {
           "name":"会员信息",
           "sub_button":[
            {	
               "type":"click",
               "name":"我的会员资料",
			   "key": "MEMBER_MYPROFILE"
            },
            {	
               "type":"click",
               "name":"积分和账单",
			   "key": "MEMBER_CREDITS"
            },
			
            {
               "type":"click",
               "name":"搜索其他会员信息",
               "key":"MEMBER_SEARCH"
            },
            {
               "type":"click",
               "name":"广播给所有会员",
               "key":"MEMBER_BROADCAST"
            },
            {	
               "type": "view",
               "name":"校友会主页",
			   "url":"https://www.hitxy.org.au/"
            }
            
			]
       },
      {
           "name":"组织活动",
           "sub_button":[
            {	
               "type":"click",
               "name":"我来发起一次活动",
			   "key": "EVENT_STARTONE"
            },
            {	
               "type":"pic_photo_or_album",
               "name":"参与直播活动",
			   "key": "EVENT_CONTRIBUTEPHOTO"
            },
            {	
               "type":"scancode_waitmsg",
               "name":"签到当前活动",
			   "key": "EVENT_CHECKIN"
            },
			
            {	
               "type":"click",
               "name":"参与幸运抽奖",
			   "key": "EVENT_LUCKYDRAW"
            },
                        
            {
               "type":"click",
               "name":"即将举办的活动",
               "key":"EVENTS_UPCOMING"
            }]
      },
      {
           "name":"职业发展",
           "sub_button":[
            {	
               "type":"click",
               "name":"澳洲工作机会",
			   "key": "CAREERDEV_JOBS"
            },
            {	
               "type":"click",
               "name":"融入澳洲社区",
			   "key": "CARDEV_INDUSTRY"
            },
            {	
               "type":"click",
               "name":"OutReach Program",
			   "key": "CARDEV_OUTREACH"
            },
            {	
               "type":"click",
               "name":"母校发展机会",
			   "key": "CARDEV_HIT"
            },
			
            {
               "type":"click",
               "name":"全球发展机会",
               "key":"CARDEV_GLOBAL"
            }]
       }
	   ]
}
`)

//CreateCustomMenu   bulid a menu for wechat windows
//    3 main manu max  (< 4 mandarin words)
//    5 sub items max  (< 7 mandarin words)
//success return nil
//error return err
func CreateCustomMenu(m []byte) error {
	atoken, err := GetAccessToken()
	if err != nil {
		return err
	}
	url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", atoken)
	if len(m) == 0 {
		m = menu //using default menu
	}
	return jsonPostRequest(url, m)

}

//CreateDefaultMenu build a menu for wechat window
//
func CreateDefaultMenu() error {
	var b []byte
	return CreateCustomMenu(b)
}

//jsonPostRequest perform http post request with json body
//   url is the url to post to
//   jsonBody is the payload in http post request
func jsonPostRequest(url string, jsonBody []byte) error {
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
	if err != nil {
		return err
	}
	req.Header.Set("X-Custom-Header", "Patrick Sun")
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)

	if err != nil {
		return err
	}
	defer resp.Body.Close()

	fmt.Println("response Status:", resp.Status)
	fmt.Println("response Headers:", resp.Header)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("response Body:", string(body))

	return nil
}

func GetMenu() {

}
