// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package sctp

import (
	
	
	
)

/*
chunkSelectiveAck represents an SCTP Chunk of type SACK

This chunk is sent to the peer endpoint to acknowledge received DATA
chunks and to inform the peer endpoint of gaps in the received
subsequences of DATA chunks as represented by their TSNs.
0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   Type = 3    |Chunk  Flags   |      Chunk Length             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                      Cumulative TSN Ack                       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Advertised Receiver Window Credit (a_rwnd)           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Number of Gap Ack Blocks = N  |  Number of Duplicate TSNs = X |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Gap Ack Block #1 Start       |   Gap Ack Block #1 End        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/                                                               /
\                              ...                              \
/                                                               /
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   Gap Ack Block #N Start      |  Gap Ack Block #N End         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Duplicate TSN 1                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/                                                               /
\                              ...                              \
/                                                               /
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                       Duplicate TSN X                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

type gapAckBlock struct {
	start uint16
	end   uint16
}

// Selective ack chunk errors.
var (
	ErrChunkTypeNotSack           = errors.New("ChunkType is not of type SACK")
	ErrSackSizeNotLargeEnoughInfo = errors.New("SACK Chunk size is not large enough to contain header")
	ErrSackSizeNotMatchPredicted  = errors.New("SACK Chunk size does not match predicted amount from header values")
)

// String makes gapAckBlock printable.
func ( gapAckBlock) () string {
	return fmt.Sprintf("%d - %d", .start, .end)
}

type chunkSelectiveAck struct {
	chunkHeader
	cumulativeTSNAck               uint32
	advertisedReceiverWindowCredit uint32
	gapAckBlocks                   []gapAckBlock
	duplicateTSN                   []uint32
}

const (
	selectiveAckHeaderSize = 12
)

func ( *chunkSelectiveAck) ( []byte) error {
	if  := .chunkHeader.unmarshal();  != nil {
		return 
	}

	if .typ != ctSack {
		return fmt.Errorf("%w: actually is %s", ErrChunkTypeNotSack, .typ.String())
	}

	if len(.raw) < selectiveAckHeaderSize {
		return fmt.Errorf("%w: %v remaining, needs %v bytes", ErrSackSizeNotLargeEnoughInfo,
			len(.raw), selectiveAckHeaderSize)
	}

	.cumulativeTSNAck = binary.BigEndian.Uint32(.raw[0:])
	.advertisedReceiverWindowCredit = binary.BigEndian.Uint32(.raw[4:])
	.gapAckBlocks = make([]gapAckBlock, binary.BigEndian.Uint16(.raw[8:]))
	.duplicateTSN = make([]uint32, binary.BigEndian.Uint16(.raw[10:]))

	if len(.raw) != selectiveAckHeaderSize+(4*len(.gapAckBlocks)+(4*len(.duplicateTSN))) {
		return ErrSackSizeNotMatchPredicted
	}

	 := selectiveAckHeaderSize
	for  := range .gapAckBlocks {
		.gapAckBlocks[].start = binary.BigEndian.Uint16(.raw[:])
		.gapAckBlocks[].end = binary.BigEndian.Uint16(.raw[+2:])
		 += 4
	}
	for  := range .duplicateTSN {
		.duplicateTSN[] = binary.BigEndian.Uint32(.raw[:])
		 += 4
	}

	return nil
}

func ( *chunkSelectiveAck) () ([]byte, error) {
	 := make([]byte, selectiveAckHeaderSize+(4*len(.gapAckBlocks)+(4*len(.duplicateTSN))))
	binary.BigEndian.PutUint32([0:], .cumulativeTSNAck)
	binary.BigEndian.PutUint32([4:], .advertisedReceiverWindowCredit)
	binary.BigEndian.PutUint16([8:], uint16(len(.gapAckBlocks)))  //nolint:gosec // G115
	binary.BigEndian.PutUint16([10:], uint16(len(.duplicateTSN))) //nolint:gosec // G115
	 := selectiveAckHeaderSize
	for ,  := range .gapAckBlocks {
		binary.BigEndian.PutUint16([:], .start)
		binary.BigEndian.PutUint16([+2:], .end)
		 += 4
	}
	for ,  := range .duplicateTSN {
		binary.BigEndian.PutUint32([:], )
		 += 4
	}

	.chunkHeader.typ = ctSack
	.chunkHeader.raw = 

	return .chunkHeader.marshal()
}

func ( *chunkSelectiveAck) () ( bool,  error) {
	return false, nil
}

// String makes chunkSelectiveAck printable.
func ( *chunkSelectiveAck) () string {
	 := fmt.Sprintf("SACK cumTsnAck=%d arwnd=%d dupTsn=%d",
		.cumulativeTSNAck,
		.advertisedReceiverWindowCredit,
		.duplicateTSN)

	for ,  := range .gapAckBlocks {
		 = fmt.Sprintf("%s\n gap ack: %s", , )
	}

	return 
}