package peer

import (
	"net/netip"
	"vppn/m"
)

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

type controlMsg[T any] struct {
	SrcIP   byte
	SrcAddr netip.AddrPort
	Packet  T
}

func parseControlMsg(srcIP byte, srcAddr netip.AddrPort, buf []byte) (any, error) {
	switch buf[0] {

	case packetTypeInit:
		packet, err := parsePacketInit(buf)
		return controlMsg[packetInit]{
			SrcIP:   srcIP,
			SrcAddr: srcAddr,
			Packet:  packet,
		}, err

	case packetTypeSyn:
		packet, err := parsePacketSyn(buf)
		return controlMsg[packetSyn]{
			SrcIP:   srcIP,
			SrcAddr: srcAddr,
			Packet:  packet,
		}, err

	case packetTypeAck:
		packet, err := parsePacketAck(buf)
		return controlMsg[packetAck]{
			SrcIP:   srcIP,
			SrcAddr: srcAddr,
			Packet:  packet,
		}, err

	case packetTypeProbe:
		packet, err := parsePacketProbe(buf)
		return controlMsg[packetProbe]{
			SrcIP:   srcIP,
			SrcAddr: srcAddr,
			Packet:  packet,
		}, err

	default:
		return nil, errUnknownPacketType
	}
}

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

type peerUpdateMsg struct {
	Peer *m.Peer
}

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

type pingTimerMsg struct{}
