package socket

import (
	"context"
	"log"
	"sync/atomic"
	"time"

	"blazing/logic/service/player"

	"github.com/gogf/gf/v2/os/glog"
	"github.com/panjf2000/gnet/v2"
	"github.com/panjf2000/gnet/v2/pkg/logging"
)

func (s *Server) Boot() error {
	//	go s.bootws()
	err := gnet.Run(s, s.network+"://"+s.addr,
		gnet.WithMulticore(true),
		gnet.WithTicker(true),
		// gnet.WithReusePort(true),
		// gnet.WithReuseAddr(true),
		gnet.WithSocketRecvBuffer(s.bufferSize))
	if err != nil {
		return err
	}
	// err := gnet.Run(s, s.network+"://"+s.addr, gnet.WithMulticore(s.multicore))
	glog.Debug(context.Background(), "server exits with error: %v", err)
	// logging.Infof("server exits with error: %v", err)
	return nil
}

func (s *Server) Stop() error {
	_ = s.eng.Stop(context.Background())
	s.workerPool.Release()

	return nil
}

func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
	atomic.AddInt64(&s.connected, -1)
	//logging.Infof("conn[%v] disconnected", c.RemoteAddr().String())
	v, ok := c.Context().(*player.ClientData)
	if !ok {
		return

	}

	if v.Player != nil {

		v.Player.Save() //保存玩家数据
	}

	//}
	//关闭连接
	return
}
func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
	logging.Infof("[connected-count=%v]", atomic.LoadInt64(&s.connected))
	return 10 * time.Second, gnet.None
}
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
	s.eng = eng

	logging.Infof("syslog server is listening on %s\n", s.addr)

	return gnet.None
}

func (s *Server) OnOpen(conn gnet.Conn) (out []byte, action gnet.Action) {
	if conn.Context() == nil {
		conn.SetContext(player.NewClientData()) //注入data
	}

	atomic.AddInt64(&s.connected, 1)

	return nil, gnet.None
}

func (s *Server) OnTraffic(c gnet.Conn) (action gnet.Action) {
	if s.network != "tcp" {
		return gnet.Close
	}

	ws := c.Context().(*player.ClientData).Wsmsg
	if ws.Tcp { //升级失败时候防止缓冲区溢出
		s.handleTcp(c)
		return gnet.None
	}

	tt, len1 := ws.ReadBufferBytes(c)
	if tt == gnet.Close {

		return gnet.Close
	}

	ok, action := ws.Upgrade(c)
	if !ok { //升级失败,说明是tcp连接
		s.handleTcp(c)
		return gnet.None
	} else {
		//	fmt.Println(ws.Buf.Bytes())
		c.Read(make([]byte, len1))
	}

	if ws.Buf.Len() <= 0 {
		return gnet.None
	}
	messages, err := ws.Decode(c)
	if err != nil {
		return gnet.Close
	}
	if messages == nil {
		return
	}

	for _, msg := range messages {
		//client := conn.RemoteAddr().String()
		_ = s.workerPool.Submit(func() {
			s.parser(c, msg.Payload)
		})
	}

	return gnet.None
}

func (s *Server) handleTcp(conn gnet.Conn) (action gnet.Action) {

	for {
		if !s.discorse {
			handle(conn)
		}
		data, err := s.codec.Decode(conn)
		if err != nil {
			break
		}

		//client := conn.RemoteAddr().String()
		_ = s.workerPool.Submit(func() { //TODO 这里可能存在顺序执行问题,待修复
			s.parser(conn, data)
		})

		return gnet.None
	}

	if conn.InboundBuffered() > 0 {
		if err := conn.Wake(nil); err != nil { // wake up the connection manually to avoid missing the leftover data
			logging.Errorf("failed to wake up the connection, %v", err)
			return gnet.Close
		}
	}

	return gnet.None

}

func (s *Server) parser(c gnet.Conn, line []byte) {
	//todo 这里待实现注入player实体
	s.handler.Handle(c, line)
}

// CROSS_DOMAIN 定义跨域策略文件内容
const CROSS_DOMAIN = "<?xml version=\"1.0\"?><!DOCTYPE cross-domain-policy><cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\x00"

// TEXT 定义跨域请求的文本格式
const TEXT = "<policy-file-request/>\x00"

func handle(c gnet.Conn) {
	clientdata := c.Context().(*player.ClientData)
	if clientdata.IsCrossDomain {
		return

	}
	// 读取数据并检查是否为跨域请求
	data, err := c.Peek(len(TEXT))
	if err != nil {
		log.Printf("Error reading cross-domain request: %v", err)
		return
	}

	if string(data) == TEXT { //判断是否是跨域请求
		log.Printf("Received cross-domain request from %s", c.RemoteAddr())
		// 处理跨域请求
		c.Write([]byte(CROSS_DOMAIN))
		c.Discard(len(TEXT))

		clientdata.IsCrossDomain = true
		return
	}

	//return
}
