package internal

import (
	"server/base"

	"github.com/name5566/leaf/gate"
	"github.com/name5566/leaf/module"
)

const (
	ConnStatusUnauth = iota // 未认证
	ConnStatusAuthed        // 已认证
)

// 未认证连接管理
type PendingConn struct {
	Agent      gate.Agent
	CreateTime int64
	Status     int
}

var (
	// 未认证连接池
	pendingConns = make(map[gate.Agent]*PendingConn)
	// 已认证用户
	authedUsers = make(map[string]gate.Agent) // key: userID
)

var (
	skeleton = base.NewSkeleton()
	ChanRPC  = skeleton.ChanRPCServer
)

type Module struct {
	*module.Skeleton
}

func (m *Module) OnInit() {
	m.Skeleton = skeleton
}

func (m *Module) OnDestroy() {

}

func GetAgentByUserID(userID string) gate.Agent {
	return authedUsers[userID]
}

func GetUserIDByAgent(agent gate.Agent) string {
	for k, v := range authedUsers {
		if v == agent {
			return k
		}
	}
	return ""
}
