//go:build windows

package yu_hpsocket

import (
	"fmt"
	yu_http "gogs.qqck.cn/s/tools/http"
	"testing"
	"time"
)

func TestHPsocketHttpServer(t *testing.T) {
	j_server := NewHttpServer()
	fmt.Println(j_server.Create(false))

	j_server.OnMessageBegin(func(t *HttpServer, Sender, ConnID uintptr) HPR {
		fmt.Println("HttpServer.OnMessageBegin", Sender, ConnID)
		return HPR_OK
	})
	j_server.OnRequestLine(func(t *HttpServer, Sender, ConnID uintptr, Method, Url string) HPR {
		fmt.Println("HttpServer.OnRequestLine", Sender, ConnID, Method, Url)
		return HPR_OK
	})
	j_server.OnHeadersComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
		fmt.Println("HttpServer.OnHeadersComplete", Sender, ConnID)
		// testHPsocketHttpServerCall(t, ConnID)
		return HPR_OK
	})
	j_server.OnBody(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HPR {
		fmt.Println("HttpServer.OnBody", Sender, ConnID, Data, Length)
		return HPR_OK
	})
	j_server.OnChunkHeader(func(t *HttpServer, Sender, ConnID uintptr, Length uintptr) HPR {
		fmt.Println("HttpServer.OnChunkHeader", Sender, ConnID, Length)
		return HPR_OK
	})
	j_server.OnChunkComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
		fmt.Println("HttpServer.OnChunkComplete", Sender, ConnID)
		return HPR_OK
	})
	j_server.OnMessageComplete(func(t *HttpServer, Sender, ConnID uintptr) HPR {
		fmt.Println("HttpServer.OnMessageComplete", Sender, ConnID)
		fmt.Println("HttpServer.GetAllHeaders", t.GetAllHeaders(ConnID))
		t.SendResponseS(ConnID, yu_http.StatusOK, "OK", []TNVPair{
			{"qwewqewqe", "Upgrade;aasdasd;wwqeqwe;"},
			{"Accept", "text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8"},
			{"Accept", "text/ht"},
		}, "nilnilnilnilnilnilnilnilnilnilnil")
		return HPR_OK
	})
	j_server.OnUpgrade(func(t *HttpServer, Sender, ConnID uintptr, UpgradeType HUT) HPR {
		fmt.Println("HttpServer.OnUpgrade", Sender, ConnID, UpgradeType)
		switch UpgradeType {
		case HUT_WEB_SOCKET:
			t.SendResponse(ConnID, yu_http.StatusSwitchingProtocols, "", []TNVPair{
				{"Connection", "Upgrade"},
				{"Upgrade", "WebSocket"},
			}, nil)
		}
		return HPR_OK
	})
	j_server.OnParseError(func(t *HttpServer, Sender, ConnID uintptr, ErrorCode int, ErrorDesc string) HPR {
		fmt.Println("HttpServer.OnParseError", Sender, ConnID, ErrorCode, ErrorDesc)
		return HPR_OK
	})
	j_server.OnWSMessageHeader(func(t *HttpServer, Sender, ConnID uintptr, Final bool, Reserved, OperationCode byte, Mask []byte, BodyLen int64) HR {
		fmt.Println("HttpServer.OnWSMessageHeader", Sender, ConnID, Final, Reserved, OperationCode, Mask, BodyLen)
		return HR_OK
	})
	j_server.OnWSMessageBody(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR {
		fmt.Println("HttpServer.OnWSMessageBody", Sender, ConnID, Data, Length)
		return HR_OK
	})
	j_server.OnWSMessageComplete(func(t *HttpServer, Sender, ConnID uintptr) HR {
		fmt.Println("HttpServer.OnWSMessageComplete", Sender, ConnID)
		return HR_OK
	})
	j_server.OnPrepareListen(func(t *HttpServer, Sender, Listen uintptr) HR {
		fmt.Println("HttpServer.OnPrepareListen", Sender, Listen)
		return HR_OK
	})
	j_server.OnAccept(func(t *HttpServer, Sender, ConnID, Client uintptr) HR {
		fmt.Println("HttpServer.OnAccept", ConnID, Client)
		return HR_OK
	})
	j_server.OnHandShake(func(t *HttpServer, Sender, ConnID uintptr) HR {
		fmt.Println("HttpServer.OnHandShake", Sender, ConnID)
		return HR_OK
	})
	j_server.OnSend(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR {
		fmt.Println("HttpServer.OnSend", Sender, ConnID, Data, Length)
		return HR_OK
	})
	j_server.OnReceive(func(t *HttpServer, Sender, ConnID, Data uintptr, Length uintptr) HR {
		fmt.Println("HttpServer.OnReceive", Sender, ConnID, Data, Length)
		return HR_OK
	})
	j_server.OnClose(func(t *HttpServer, Sender, ConnID uintptr, Operation SO, ErrorCode int) HR {
		fmt.Println("HttpServer.OnClose", Sender, ConnID, Operation, ErrorCode)
		return HR_OK
	})
	j_server.OnShutdown(func(t *HttpServer, Sender uintptr) HR {
		fmt.Println("HttpServer.OnShutdown", Sender)
		return HR_OK
	})
	fmt.Println(j_server.Start("0.0.0.0", 8001))

	fmt.Println(j_server.GetLastError(), j_server.GetLastErrorDesc())

	time.Sleep(time.Second * 100000)

}
