package weixinapi

import (
	"crypto/md5"
	"fmt"
	"git.rosy.net.cn/baseapi/platformapi"
	"io/ioutil"
	"net/http"
	"testing"

	"git.rosy.net.cn/baseapi/utils"
)

func TestCBRetrieveToken(t *testing.T) {
	result, err := api.CBRetrieveToken()
	if err != nil || result.ExpiresIn != 7200 {
		t.Fatal(err.Error())
	}
	sugarLogger.Debug(result)
}

func TestCBMessageTemplateSend(t *testing.T) {
	// "oYN_usk0AeGc_C6VEZfmFQP5VHMQ": 1, // 周小扬
	// "oYN_ust9hXKEvEv0X6Mq6nlAWs_E": 1, // me
	// "oYN_usvnObzrPweIgHTad9-uMf78": 1, // 老赵
	err := api.CBMessageTemplateSend("oYN_usjzxL9LBzh_hjf0-E_LGmvs", "b8-tLyWwAmK-1tEU1eGqp_YAAqQtSzoVDZkHuyUe9lk", "", map[string]interface{}{
		"appid": "wx2bb99eb5d2c9b82c",
		//"pagepath": "pages/order-manager/main",
	}, map[string]interface{}{
		"first": map[string]interface{}{
			"value": "这是一个测试头",
			"color": "#0191EA",
		},
		"keyword1": map[string]interface{}{
			"value": "美团#1",
			"color": "#173177",
		},
		"keyword2": map[string]interface{}{
			"value": "好菜鲜生(清水塘生鲜农贸市场2店)",
			"color": "#173177",
		},
		//"orderId": "orderId",
		"keyword3": map[string]interface{}{
			//"value": "吴廷琦吴吴廷琦吴吴廷琦吴吴廷琦吴吴廷琦吴吴廷琦吴 18727362534,0987",
			"value": "1234567891234567891234567891234 18727362534,0987",
			"color": "#173177",
		},
		"keyword4": map[string]interface{}{
			"value": "2023:05:05 14:30:18",
			"color": "#173177",
		},
		//"keyword5": map[string]interface{}{
		//	"value": "立即配送",
		//	"color": "#173177",
		//},

		//"customerPhone": map[string]interface{}{
		//	"value": "1234567891234567891234567891234",
		//	"color": "#173177",
		//},
	})
	if err != nil {
		t.Fatal(err.Error())
	}
}

func TestCBUpdateRemark(t *testing.T) {
	remark := "一二三四五六七八九十"
	t.Log(len(remark))
	err := api.CBUpdateRemark("oYN_ust9hXKEvEv0X6Mq6nlAWs_E", remark)
	if err != nil {
		t.Log(err)
	}
}

func TestCBGetUserInfo(t *testing.T) {
	userInfo, err := api.CBGetUserInfo("ox1y15LCS3ytipsTjEQ4_nXrRtkw")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(userInfo, false))
}

func TestCBGetTicketInfo(t *testing.T) {
	userInfo, err := api.CBGetTicketInfo()
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(userInfo, false))
}

func TestCBBatchgetMaterial(t *testing.T) {
	userInfo, err := api.CBBatchgetMaterial(MaterialTypeThumb, 0, 1)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(userInfo, false))
}

func TestCBUploadImg(t *testing.T) {
	data, _, _ := DownloadFileByURL("https://image.jxc4.com/noGoodsImg.jpg")
	result, err := api.CBUploadImg(data, "noGoodsImg.jpg", "image/jpeg")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestCBCBUploadThumb(t *testing.T) {
	data, _, _ := DownloadFileByURL("https://image.jxc4.com/noGoodsImg.jpg")
	result, _, err := api.CBUploadThumb(data, "noGoodsImg.jpg", "image/jpeg")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func DownloadFileByURL(fileURL string) (bodyData []byte, fileMD5 string, err error) {
	response, err := http.Get(fileURL)
	if err == nil {
		defer response.Body.Close()
		if response.StatusCode == http.StatusOK {
			if bodyData, err = ioutil.ReadAll(response.Body); err == nil {
				fileMD5 = fmt.Sprintf("%X", md5.Sum(bodyData))
			}
		} else {
			err = platformapi.ErrHTTPCodeIsNot200
		}
	}
	return bodyData, fileMD5, err
}

func TestCBAddNews(t *testing.T) {
	result, err := api.CBAddNews(&CBAddNewsParam{
		Title:              "测试素材",
		ThumbMediaID:       "cILZtVn68ncUY79JUE3tWFOGvL3GytjbKLOKiGbLK1I",
		Author:             "西西",
		Digest:             "测试摘要",
		ShowCoverPic:       1,
		Content:            "测试内容：<p><b>测试段落</b></p>",
		ContentSourceUrl:   "",
		NeedOpenComment:    0,
		OnlyFansCanComment: 0,
	})
	if err != nil {
		t.Fatal(err)
	}
	//cILZtVn68ncUY79JUE3tWL4YE2er7eHJJPV_BGRieHE
	t.Log(utils.Format4Output(result, false))
}

func TestCBMassSend(t *testing.T) {
	err := api.CBMassSend([]string{"oYN_usv1RPvrSxCvo1WsbwI8lZa0", "oYN_ushJ8Ma2kppYRlCJKSZxtzMk"}, "cILZtVn68ncUY79JUE3tWL4YE2er7eHJJPV_BGRieHE")
	if err != nil {
		t.Fatal(err)
	}
	//t.Log(utils.Format4Output(result, false))
}

func TestTode(t *testing.T) {
	ccc := 3 &^ 1
	fmt.Println(ccc)
}
