//go:build windows

package yu_hpsocket

import (
	"sync"
	"syscall"
)

func init() {
	server_callback_OnPrepareListen_ptr = syscall.NewCallback(server_callback_OnPrepareListen)
	server_callback_OnAccept_ptr = syscall.NewCallback(server_callback_OnAccept)
	server_callback_OnHandShake_ptr = syscall.NewCallback(server_callback_OnHandShake)
	server_callback_OnSend_ptr = syscall.NewCallback(server_callback_OnSend)
	server_callback_OnReceive_ptr = syscall.NewCallback(server_callback_OnReceive)
	server_callback_OnClose_ptr = syscall.NewCallback(server_callback_OnClose)
	server_callback_OnShutdown_ptr = syscall.NewCallback(server_callback_OnShutdown)
}

var (
	server_callback_OnPrepareListen_ptr uintptr
	server_callback_OnAccept_ptr        uintptr
	server_callback_OnHandShake_ptr     uintptr
	server_callback_OnSend_ptr          uintptr
	server_callback_OnReceive_ptr       uintptr
	server_callback_OnClose_ptr         uintptr
	server_callback_OnShutdown_ptr      uintptr
)

func server_callback_OnPrepareListen(Sender, Listen uintptr) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onPrepareListen(t, Sender, Listen)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onPrepareListen(t, Sender, Listen)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onPrepareListen(t, Sender, Listen)
	}
	return HR_OK
}

func server_callback_OnAccept(Sender, ConnID, Client uintptr) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onAccept(t, Sender, ConnID, Client)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onAccept(t, Sender, ConnID, Client)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onAccept(t, Sender, ConnID, Client)
	}
	return HR_OK
}

func server_callback_OnHandShake(Sender, ConnID uintptr) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onHandShake(t, Sender, ConnID)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onHandShake(t, Sender, ConnID)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onHandShake(t, Sender, ConnID)
	}
	return HR_OK
}

func server_callback_OnSend(Sender, ConnID, Data uintptr, Length uintptr) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onSend(t, Sender, ConnID, Data, Length)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onSend(t, Sender, ConnID, Data, Length)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onSend(t, Sender, ConnID, Data, Length)
	}
	return HR_OK
}

func server_callback_OnReceive(Sender, ConnID, Data uintptr, Length uintptr) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onReceive(t, Sender, ConnID, Data, Length)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onReceive(t, Sender, ConnID, Data, Length)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onReceive(t, Sender, ConnID, Data, Length)
	}
	return HR_OK
}

func server_callback_OnClose(Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onClose(t, Sender, ConnID, Operation, ErrorCode)
	}
	return HR_OK
}

func server_callback_OnShutdown(Sender uintptr) HR {
	switch j_item := _server_on_get(Sender); j_item._type {
	case _server_type_TcpServer:
		t := j_item.t.(*TcpServer)
		return t.onShutdown(t, Sender)
	case _server_type_TcpPackServer:
		t := j_item.t.(*TcpPackServer)
		return t.onShutdown(t, Sender)
	case _server_type_HttpServer:
		t := j_item.t.(*HttpServer)
		return t.onShutdown(t, Sender)
	}
	return HR_OK
}

// ---------------------------------------------------------------------------------------------------------------------

var _server_on_lock sync.Mutex
var _server_on = make(map[uintptr]any)

const (
	_server_type_Server = iota
	_server_type_TcpServer
	_server_type_TcpPackServer
	_server_type_HttpServer
)

type _server_item struct {
	_type byte
	t     any
}

func _server_on_set(Sender uintptr, v *_server_item) {
	_server_on_lock.Lock()
	defer _server_on_lock.Unlock()
	_server_on[Sender] = v
}

func _server_on_get(Sender uintptr) *_server_item {
	_server_on_lock.Lock()
	defer _server_on_lock.Unlock()
	if j_v, j_is := _server_on[Sender]; j_is {
		return j_v.(*_server_item)
	}
	return nil
}

func _server_on_del(Sender uintptr) {
	_server_on_lock.Lock()
	defer _server_on_lock.Unlock()
	delete(_server_on, Sender)
}
