func (f *AckFrame) LargestAcked() protocol.PacketNumber {
	return f.AckRanges[0].Largest
}
LargestAcked is the largest acked packet number
package wire
import (
	"bytes"
	"errors"
	"sort"
	"time"
	"github.com/lucas-clemente/quic-go/internal/protocol"
	"github.com/lucas-clemente/quic-go/internal/utils"
)
var errInvalidAckRanges = errors.New("AckFrame: ACK frame contains invalid ACK ranges")
type AckFrame struct {
	AckRanges []AckRange  
	DelayTime time.Duration
}
func parseAckFrame(r *bytes.Reader, ackDelayExponent uint8, version protocol.VersionNumber) (*AckFrame, error) {
	typeByte, err := r.ReadByte()
	if err != nil {
		return nil, err
	}
	ecn := typeByte&0x1 > 0
	frame := &AckFrame{}
	la, err := utils.ReadVarInt(r)
	if err != nil {
		return nil, err
	}
	largestAcked := protocol.PacketNumber(la)
	delay, err := utils.ReadVarInt(r)
	if err != nil {
		return nil, err
	}
	frame.DelayTime = time.Duration(delay*1<<ackDelayExponent) * time.Microsecond
	numBlocks, err := utils.ReadVarInt(r)
	if err != nil {
		return nil, err
	}
	ab, err := utils.ReadVarInt(r)
	if err != nil {
		return nil, err
	}
	ackBlock := protocol.PacketNumber(ab)
	if ackBlock > largestAcked {
		return nil, errors.New("invalid first ACK range")
	}
	smallest := largestAcked - ackBlock
	frame.AckRanges = append(frame.AckRanges, AckRange{Smallest: smallest, Largest: largestAcked})
	for i := uint64(0); i < numBlocks; i++ {
		g, err := utils.ReadVarInt(r)
		if err != nil {
			return nil, err
		}
		gap := protocol.PacketNumber(g)
		if smallest < gap+2 {
			return nil, errInvalidAckRanges
		}
		largest := smallest - gap - 2
		ab, err := utils.ReadVarInt(r)
		if err != nil {
			return nil, err
		}
		ackBlock := protocol.PacketNumber(ab)
		if ackBlock > largest {
			return nil, errInvalidAckRanges
		}
		smallest = largest - ackBlock
		frame.AckRanges = append(frame.AckRanges, AckRange{Smallest: smallest, Largest: largest})
	}
	if !frame.validateAckRanges() {
		return nil, errInvalidAckRanges
	}
	if ecn {
		for i := 0; i < 3; i++ {
			if _, err := utils.ReadVarInt(r); err != nil {
				return nil, err
			}
		}
	}
	return frame, nil
}
func (f *AckFrame) Write(b *bytes.Buffer, version protocol.VersionNumber) error {
	b.WriteByte(0x2)
	utils.WriteVarInt(b, uint64(f.LargestAcked()))
	utils.WriteVarInt(b, encodeAckDelay(f.DelayTime))
	numRanges := f.numEncodableAckRanges()
	utils.WriteVarInt(b, uint64(numRanges-1))
	_, firstRange := f.encodeAckRange(0)
	utils.WriteVarInt(b, firstRange)
	for i := 1; i < numRanges; i++ {
		gap, len := f.encodeAckRange(i)
		utils.WriteVarInt(b, gap)
		utils.WriteVarInt(b, len)
	}
	return nil
}
func (f *AckFrame) Length(version protocol.VersionNumber) protocol.ByteCount {
	largestAcked := f.AckRanges[0].Largest
	numRanges := f.numEncodableAckRanges()
	length := 1 + utils.VarIntLen(uint64(largestAcked)) + utils.VarIntLen(encodeAckDelay(f.DelayTime))
	length += utils.VarIntLen(uint64(numRanges - 1))
	lowestInFirstRange := f.AckRanges[0].Smallest
	length += utils.VarIntLen(uint64(largestAcked - lowestInFirstRange))
	for i := 1; i < numRanges; i++ {
		gap, len := f.encodeAckRange(i)
		length += utils.VarIntLen(gap)
		length += utils.VarIntLen(len)
	}
	return length
}
func (f *AckFrame) numEncodableAckRanges() int {
	length := 1 + utils.VarIntLen(uint64(f.LargestAcked())) + utils.VarIntLen(encodeAckDelay(f.DelayTime))
	length += 2  
	for i := 1; i < len(f.AckRanges); i++ {
		gap, len := f.encodeAckRange(i)
		rangeLen := utils.VarIntLen(gap) + utils.VarIntLen(len)
		if length+rangeLen > protocol.MaxAckFrameSize {
			return i - 1
		}
		length += rangeLen
	}
	return len(f.AckRanges)
}
func (f *AckFrame) encodeAckRange(i int) (uint64  , uint64  ) {
	if i == 0 {
		return 0, uint64(f.AckRanges[0].Largest - f.AckRanges[0].Smallest)
	}
	return uint64(f.AckRanges[i-1].Smallest - f.AckRanges[i].Largest - 2),
		uint64(f.AckRanges[i].Largest - f.AckRanges[i].Smallest)
}
func (f *AckFrame) HasMissingRanges() bool {
	return len(f.AckRanges) > 1
}
func (f *AckFrame) validateAckRanges() bool {
	if len(f.AckRanges) == 0 {
		return false
	}
	for _, ackRange := range f.AckRanges {
		if ackRange.Smallest > ackRange.Largest {
			return false
		}
	}
	for i, ackRange := range f.AckRanges {
		if i == 0 {
			continue
		}
		lastAckRange := f.AckRanges[i-1]
		if lastAckRange.Smallest <= ackRange.Smallest {
			return false
		}
		if lastAckRange.Smallest <= ackRange.Largest+1 {
			return false
		}
	}
	return true
}