package main

import (
	"log"
	"time"

	"git.lxtend.com/qqbot/config"
	"git.lxtend.com/qqbot/service/exec"
	"git.lxtend.com/qqbot/sqlite3"
	_ "git.lxtend.com/qqbot/works"
	wsclient "git.lxtend.com/qqbot/ws_client"
	"github.com/gin-gonic/gin"
)

func main() {
	// 创建 WebSocket 客户端
	defer func() {
		if err := recover(); err != nil {
			log.Print("Recovered in main:", err)
		}
	}()
	defer exec.DockerContainer.RemoveContainer()
	gin.SetMode(gin.ReleaseMode)
	sqlite3.InitDB()
	go func() {
		const reconnectDelay = 5 * time.Second
		for {
			client, err := wsclient.NewWebSocketClient("ws", config.ConfigManager.GetConfig().NapcatWsSrv, "")
			if err != nil {
				log.Printf("WebSocket连接失败: %v, %v 后重试", err, reconnectDelay)
				time.Sleep(reconnectDelay)
				continue
			}

			// 等待连接关闭
			<-client.Done
			client.Close()

			log.Printf("WebSocket连接断开, %v 后重连", reconnectDelay)
			time.Sleep(reconnectDelay)
		}
	}()
	startRouter()
	for {
		time.Sleep(1000 * time.Second)
	}
}
