package http

import (
	"context"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"math/rand"
	"netease_control_center/globals"
	"netease_control_center/interfaces/log"
	"strconv"
	"strings"
	"testing"
	"time"
)

func TestHttpReq_Post(t *testing.T) {
	var req HttpReqFirstRun
	var res HttpRspFirstRun

	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"

	req.Mac = "thisismacaddr"
	req.BindUser = "1-22DAEB83D6721A40"

	code, detail, err := HttpPostAuto(req, &res)

	fmt.Println(code, detail, err, res)
}

func TestHttpReq_Post2(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-CA879CFC538A1BFA"

	var req HttpReqRebindUser
	var res HttpRspRebindUser

	req.AppUuid = "1-956D1BB7F32D9B79"

	code, detail, err := HttpPostAuto(req, &res)

	fmt.Println(code, detail, err, res)
}

func TestHttpReq_PostAuto(t *testing.T) {
	var req HttpReqFirstRun
	var res HttpRspFirstRun

	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"

	req.Mac = "thisismacaddr"
	req.BindUser = "1-5740EF1C1418F7D3"

	code, detail, err := HttpPostAuto(req, &res)
	fmt.Println(code, detail, err, res)
	globals.AesKey = res.AesKey
	ak, _ := base64.StdEncoding.DecodeString(res.AesKey)
	fmt.Println("AES KEY: ", ak)

	globals.Uuid = res.VboxId

	var req2 HttpReqRebindUser
	var res2 HttpRspRebindUser

	req2.AppUuid = "1-7E32E9824C91C59D"

	code, detail, err = HttpPostAuto(req2, &res2)
	fmt.Println(code, detail, err, res2)
}

func TestVersion(t *testing.T) {
	var req HttpReqFirstRun
	var res HttpRspFirstRun

	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"

	req.Mac = "thisismacaddr"
	req.BindUser = "1-5740EF1C1418F7D3"

	code, detail, err := HttpPostAuto(req, &res)
	fmt.Println(code, detail, err, res)
	globals.AesKey = res.AesKey
	ak, _ := base64.StdEncoding.DecodeString(res.AesKey)
	fmt.Println("AES KEY: ", ak)

	globals.Uuid = res.VboxId

	var req2 HttpReqNewestVersion
	var res2 HttpRspNewestVersion

	req2.Version = "0.0.2"
	globals.GitTag = req2.Version

	code, detail, err = HttpPostAuto(req2, &res2)
	fmt.Println(code, detail, err, res2)
	data, _ := json.Marshal(res2)
	fmt.Println(string(data))

	var req3 HttpReqLastNormalVersion
	var res3 HttpRspLastNormalVersion

	code, detail, err = HttpPostAuto(req3, &res3)
	fmt.Println(code, detail, err)

	var req4 HttpReqGetVersionInfo
	var res4 HttpRspGetVersionInfo

	req4.Version = "0.0.2"

	code, detail, err = HttpPostAuto(req4, &res4)
	fmt.Println(code, detail, err)
}

func TestAlarm(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-D8B870AC67EC73FF"

	var add HttpReqAddAlarm
	var addrsp HttpRspAddAlarm
	rand.Seed(time.Now().UnixNano())
	now := time.Now().Local().Add(time.Duration(rand.Intn(24)) * time.Hour)
	add.Minute = strconv.Itoa(now.Minute())
	add.Hour = strconv.Itoa(now.Hour())
	//add.LocalId = "1"
	//add.Vboxid = globals.Uuid

	//add.AlarmTime = uint64(now.Unix()) * 1000
	fmt.Println("set alarm:", now.Unix(), now.Hour(), now.Minute())
	code, detail, err := HttpPostAuto(add, &addrsp)

	fmt.Println("add alarm", code, detail, err, addrsp)

	var list HttpReqGetRefleshedAlarmList
	var listrsp HttpRspGetRefleshedAlarmList

	code, detail, err = HttpPostAuto(list, &listrsp)
	fmt.Println("get alarmlist:", code, detail, err, listrsp)
}

func TestGetAlarms(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-D8B870AC67EC73FF"

	var list HttpReqGetRefleshedAlarmList
	var listrsp HttpRspGetRefleshedAlarmList

	code, detail, err := HttpPostAuto(list, &listrsp)
	fmt.Println("get alarmlist:", code, detail, err, listrsp)
}

func TestGetNewVersion(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-D8B870AC67EC73FF"
	globals.GitTag = "0.0.9"

	var list HttpReqLastNormalVersion
	var listrsp HttpRspLastNormalVersion

	code, detail, err := HttpPostAuto(list, &listrsp)
	fmt.Println("get alarmlist:", code, detail, err, listrsp)
}

func TestDelAlarm(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-D8B870AC67EC73FF"
	var alarmlist []string

	var list HttpReqGetRefleshedAlarmList
	var listrsp HttpRspGetRefleshedAlarmList

	code, detail, err := HttpPostAuto(list, &listrsp)
	fmt.Println("get alarmlist:", code, detail, err, listrsp)
	if len(listrsp.Data) >= 2 {
		alarmlist = append(alarmlist, strconv.Itoa(int(listrsp.Data[0].Id)), strconv.Itoa(int(listrsp.Data[1].Id)))
		var req HttpReqMultiDelAlarms
		var rsp HttpRspMultiDelAlarms

		req.Ids = strings.Join(alarmlist, ",")
		fmt.Println(log.DebugLog, "Del:", req.Ids)
		code, detail, err := HttpPostAuto(req, &rsp)
		fmt.Println("del alarm:", code, detail, err, rsp)
	}
}

func TestHttpGettimestamp(t *testing.T) {
	var req HttpReqGetTimeStamp
	var rsp HttpRspGetTimeStamp
	fmt.Println(HttpPostAuto(req, &rsp))

	fmt.Println("time cal result:", HttpTestAndCalTime(context.Background()))
}

func TestHttpHotfix(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-CEC366D13981B342"
	globals.GitTag = "0.2.2"

	var list HttpReqHotfix
	var listrsp HttpRspHotfix
	list.CurVersion = "0.2.4"
	code, detail, err := HttpPostAuto(list, &listrsp)
	fmt.Println("get hotfix:", code, detail, err, listrsp)
}

func TestHttpreqscene(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-CEC366D13981B342"
	globals.GitTag = "0.2.2"

	var req HttpReqRefreshScene
	var rsp HttpRspRefreshScene

	code, detail, err := HttpPostAuto(req, &rsp)
	fmt.Println("get retcode:", code, detail, err, rsp)
	fmt.Printf("Get data:%+v\n", rsp)
}

func TestHttpGetSceneById(t *testing.T) {
	globals.BuildVersion = "1234"
	globals.BuildDate = "2345"
	globals.Uuid = "0-CEC366D13981B342"
	globals.GitTag = "0.2.2"

	var req HttpReqSceneById
	var rsp HttpRspSceneById
	req.SceneId = "5a26441f0cf2692ba7001d97"

	code, detail, err := HttpPostAuto(req, &rsp)
	fmt.Println("get scene:", code, detail, err)
	fmt.Printf("Get data:%+v\n", rsp)
}
