package server

import (
	"context"
	"dlt645-server/protocol"
	"fmt"
	"github.com/gogf/gf/container/gmap"
	"github.com/gogf/gf/net/gtcp"
	"github.com/gogf/gf/os/glog"
	gatewayV2 "sparrow-sdk/v2"
	"sync"
)

type Server struct {
	closeChan chan struct{}
	server    *gtcp.Server
	ctx       context.Context
	addr      string
	port      int
	clients   *gmap.HashMap
	grMu      sync.Mutex
	grWG      sync.WaitGroup
	grRunning bool
	message   protocol.Message
	gateWay   *gatewayV2.Gateway
}

func NewServer(ctx context.Context, addr string, port int, gw *gatewayV2.Gateway) *Server {
	return &Server{
		closeChan: make(chan struct{}),
		ctx:       ctx,
		addr:      addr,
		port:      port,
		gateWay:   gw,
	}
}

func (s *Server) Start() error {
	glog.Printf("服务端启动[%s:%d]", s.addr, s.port)
	server := gtcp.NewServer(fmt.Sprintf("%s:%d", s.addr, s.port), s.handleConnect)
	s.server = server
	s.grMu.Lock()
	s.grRunning = true
	s.grMu.Unlock()
	return s.server.Run()
}

func (s *Server) Stop() {
	s.server.Close()
}

func (s *Server) handleConnect(conn *gtcp.Conn) {
	s.startGoRoutine(func() {
		s.createClient(conn)
		s.grWG.Done()
	})
}

func (s *Server) startGoRoutine(f func()) {
	s.grMu.Lock()
	if s.grRunning {
		s.grWG.Add(1)
		go f()
	}
	s.grMu.Unlock()
}

func (s *Server) createClient(conn *gtcp.Conn) *Client {
	c := &Client{
		srv:  s,
		done: make(chan struct{}),
		nc:   conn,
		closeHandler: func(id string, c *Client) {
			glog.Debugf("客户端断开:%s", id)
		},
	}
	glog.Info("client connect created")
	s.startGoRoutine(func() {
		c.ReadLoop()
	})
	return c
}

func (s *Server) removeClient(gatewayId uint16) {
	s.clients.Remove(gatewayId)
}

func (s *Server) ReportStatus(subId string, data interface{}) error {
	return s.gateWay.ReportStatus(subId, "status", data)
}
