package main

import (
	"github.com/golang/protobuf/proto"
	"log"
	message "server/src/msg"
	"time"
)

func main() {
	// 创建WebSocket客户端
	client := &WSClient{}

	// 连接到服务器
	if err := client.Connect(); err != nil {
		log.Fatalf("Connection error: %v", err)
	}
	defer client.Close()

	// 启动心跳协程
	go client.StartHeartbeat(5 * time.Second)

	// 发送登录请求
	loginResp, err := client.Login(
		"device-12345",
		"securepassword",
		1, // Android
		"1.0.0",
		"Samsung Galaxy S21",
	)

	if err != nil {
		log.Fatalf("Login failed: %v", err)
	}

	log.Println("Login response:")
	PrettyPrint(loginResp)

	// 保持连接并处理其他消息
	for {
		pkt, err := client.ReceivePacket()
		if err != nil {
			log.Printf("Error receiving packet: %v", err)
			break
		}

		log.Printf("Received packet: CmdMain=%d, CmdSub=%d", pkt.CmdMain, pkt.CmdSub)

		// 根据命令类型处理不同消息
		switch {
		case pkt.CmdMain == message.HEART && pkt.CmdSub == message.SC_RESPONSE: // 心跳响应
			heartbeatResp := &message.SC_Heartbeat{}
			if err := proto.Unmarshal(pkt.Data, heartbeatResp); err != nil {
				log.Printf("Failed to unmarshal heartbeat response: %v", err)
			} else {
				log.Println("Heartbeat response received")
			}

		// 添加其他消息处理逻辑...

		default:
			log.Printf("Unhandled command: %d.%d", pkt.CmdMain, pkt.CmdSub)
		}
	}
}
