package main

import (
	"log"
	"time"

	"tygit.tuyoo.com/gocomponents/galog"
)

func main() {
	log.Println("log ga asset server every 5 seconds.")
	go logGA()
	go logAsset()
	logServer()
}

func logGA() {
	projectID := "20433"
	clientID := "Android_5.0_tyGuest,weixinPay,tyAccount.alipay.0-hall20433.tuyoo.sdktest"
	logger, err := galog.NewServerGALogger(&galog.ServerGALogOptions{
		ProjectID:      projectID,
		ClientID:       clientID,
		LogDir:         "ga_log",
		LogType:        galog.LogTypeTrack,
		EnableAsync:    true,
		AsyncQueueSize: 1000,
	})
	if err != nil {
		log.Fatal(err)
	}
	t := time.NewTicker(5 * time.Second)

	for tt := range t.C {
		log.Println("logGA")
		logger.
			GetEntry("sdk_s_login_succ").
			SetDeviceID("device001").
			SetUserID("10086").
			SetProperties(map[string]interface{}{
				"ip_address":  "127.0.0.1",
				"proj_app_id": "10010",
				"uuid":        "4951d472-2c46-4fe5-9c4f-c35b6fb53f67",
				"ts":          tt.UnixNano(),
			}).
			Flush()
	}
}

func logAsset() {
	projectID := "28"
	clientID := "Android_4.827_tyGuest,nearme.nearme.0-hall28.oppo.bydzz"
	logger, err := galog.NewGALogger(".", projectID, clientID, galog.LogTypeAsset)
	if err != nil {
		log.Fatal(err)
	}
	t := time.NewTicker(5 * time.Second)

	asset := galog.AssetProperties{}
	asset.SetAssetID("13101").
		SetAssetName("\u9501\u5b9a").
		SetAssetType("6").
		SetAssetFinal("2").
		SetAssetAssociated("3").
		SetAssetStartTime("0").
		SetAssetTimeLimit("0").
		SetAssetSource("").
		SetKV("uuid", "uuid-v4")

	for tt := range t.C {
		log.Println("logAssert")
		_ = tt
		logger.
			GetEntry("asset_increase").
			SetDeviceID("").
			SetUserID("10086").
			SetProperties(asset).
			Flush()
	}
}

func logServer() {
	slogger, _ := galog.NewServerLogger(&galog.ServerLogOptions{
		LogDir:         "logs",
		EnableAsync:    true,
		AsyncQueueSize: 1000,
	})

	t := time.NewTicker(5 * time.Second)

	for tt := range t.C {
		log.Println("logServer")
		_ = tt

		msgMap := map[string]interface{}{
			"CreateTime": time.Now().UnixNano() / int64(time.Microsecond),
			"Host":       "host",
			"AppId":      "10010",
			"UserId":     "12345",
			"Level":      "Notice",
			"Entry":      "Login",
			"Func":       "HandleLogin",
			"TraceMsg":   "HandlerWSFriApplyList|HandlerWSFriApplyList",
			"Params":     "{\"isoCode\":\"CN\",\"P0\":[3,\"Total: 3, End: 0, \"],\"pf\":\"wx\",\"appVer\":\"1.0\",\"sdkVer\":\"1.0\",\"ip\":\"58.247.195.158\",\"clientId\":\"7abd64bf-3fb6-4fef-a25e-e6562b7fb857\",\"timeZone\":\"Asia/Shanghai\",\"loginMark\":\"\",\"st\":1472}",
		}
		slogger.BizErr(msgMap)
	}
}
