package yilianyunapi

import (
	"fmt"
	"testing"
	"time"

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

	"go.uber.org/zap"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)

	// 自有应用
	//api = New("1039586024", "4885d07c2997b661102e4b6099c0bf3b")
	//api.SetToken("283f27f7dfbe4379b2db9b8f11e61b9c")

	// 开放应用
	api = New("1098307169", "d5eedb40c99e6691b1ca2ba82a363d6a")
}

func handleError(t *testing.T, err error) {
	if err != nil {
		sugarLogger.Debug(err)
		t.Fatal(err.Error())
	}
}

func TestRetrieveToken(t *testing.T) {
	token, err := api.RetrieveToken()
	handleError(t, err)
	baseapi.SugarLogger.Debug(utils.Format4Output(token, true))
}

func TestAddPrinter(t *testing.T) {
	// err := api.AddPrinter("4004600675", "fem2ukwvduik", "公司测试打印机1")
	// 4004617180, 381870509796 公司测试打印机2
	err := api.AddPrinter("4004615546", "7nxsw668yqtk", "测试打印机1")
	handleError(t, err)
}

func TestDeletePrinter(t *testing.T) {
	err := api.DeletePrinter("4004615546")
	handleError(t, err)
}

func TestPrintMsg(t *testing.T) {
	// 4004606481
	err := api.PrintMsg("4004797135", utils.GetUUID(), "<FS2>饿百取货码</FS2>")
	handleError(t, err)
}

func TestPrintMsgWithToken(t *testing.T) {
	// 4004606481
	//err := api.PrintMsgWithToken("4004555254", utils.GetUUID(), "<FS2>饿百取货码</FS2>", "0a8255eeb42445f7b8db42b1fc60ce87")
	err := api.PrintMsgWithToken("4004797135", utils.GetUUID(), "<FS2>饿百取货码</FS2>", "179770218366")
	handleError(t, err)
}

func TestGetPrintStatus(t *testing.T) {
	state, err := api.GetPrintStatus("4004802855")
	handleError(t, err)
	baseapi.SugarLogger.Debug(state)
}

func TestGetPrintStatusWithToken(t *testing.T) {
	state, err := api.GetPrintStatusWithToken("4004627655", "308344d0cb74469eb892b26eaffffca2")
	handleError(t, err)
	baseapi.SugarLogger.Debug(state)
}

func TestGetPrinterToken(t *testing.T) {
	TokenInfo, err := api.GetPrinterToken("4004797135", "179770218366", "")
	handleError(t, err)
	baseapi.SugarLogger.Debug(TokenInfo)
}

func TestCancelAll(t *testing.T) {
	err := api.CancelAll("4004617242", "03e4ec4d65264bca850c8ad87dd8fc21")
	handleError(t, err)
}

func TestPlayText(t *testing.T) {
	err := api.PlayText("4004617180", utils.GetUUID(), "我们已安排%s配送员%s负责配送。^_^", "")
	handleError(t, err)
}

func TestRefreshToken(t *testing.T) {
	tokenInfo, err := api.RefreshToken("b87e713b8aa0408e92d3877a3fb94299")
	handleError(t, err)
	t.Log(utils.Format4Output(tokenInfo, true))
}

func TestGetOrderPagingList(t *testing.T) {
	result, err := api.GetOrderPagingList("4004613792", "", 1, 50)
	handleError(t, err)
	t.Log(utils.Format4Output(result, true))
}

func TestName(t *testing.T) {
	fmt.Println(time.Now().Add(time.Duration(2592000) * time.Second).Unix())
}

func TestSetSound(t *testing.T) {
	err := api.SetSound("4004617180", "2")
	handleError(t, err)
}
