package org.kne.cloud.network.ipv6;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class KLALBPassportHopByHopTLV extends IPv6HopByHopTLV {
    
    public KLALBPassportHopByHopTLV(ByteBuffer buffer) {
        super(buffer);
    }

    /**
     * 构造方法 - 创建新的护照时间戳记录
     * 
     * @param packetSize 包长度（2字节）
     * @param timestampOffset 时间戳偏移量（4字节），相对于流开始时间
     */
    public KLALBPassportHopByHopTLV(int packetSize, int timestampOffset) {
        super(IPv6HopByHopTLV.KLALB_PASSPORT); // 护照记录类型
        
        setPacketSize(packetSize);
        setTimestampOffset(timestampOffset);
        getData().limit(6); // 固定6字节数据
    }

    @Override
    public String toString() {
        return "KLALBPassportHopByHopTLV [packetLength=" + getPacketSize() + 
               ", timestampOffset=" + getTimestampOffset() + "]";
    }

    /**
     * 设置包长度（2字节，大端序）
     * 范围: 0-65535字节
     */
    public void setPacketSize(int packetSize) {
        if (packetSize < 0 || packetSize > 65535) {
            throw new IllegalArgumentException("Packet length must be between 0 and 65535");
        }
        getData().put(0, (byte) ((packetSize >> 8) & 0xFF));
        getData().put(1, (byte) (packetSize & 0xFF));
    }

    /**
     * 获取包长度（2字节，大端序）
     */
    public int getPacketSize() {
        return ((getData().get(0) & 0xFF) << 8) | (getData().get(1) & 0xFF);
    }

    /**
     * 设置时间戳偏移量（4字节，有符号整数，大端序）
     */
    public void setTimestampOffset(int timestampOffset) {
        getData().putInt(2, timestampOffset);
    }

    /**
     * 获取时间戳偏移量（4字节，有符号整数，大端序）
     */
    public int getTimestampOffset() {
        return getData().getInt(2);
    }

    @Override
    public void writeToChannel(WritableByteChannel dto) throws IOException {
        super.writeToChannel(dto);
    }

    @Override
    public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
        super.readFromChannel(din, length);
    }

	public long getTimestampOffsetLong() {
		return ((long)getTimestampOffset())<<4;
	}
}