package org.kne.cloud.network.ipv6;

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

public class KLALBOAMHopByHopTLV extends IPv6HopByHopTLV {
    
    // 标志位掩码定义 - 移除了DetNet相关标志
    public static final int FLAG_RECORD_PASSPORT = 0x01;        // 第1位: 记录Passport
    public static final int FLAG_POSTCARD_TO_SOURCE = 0x02;     // 第2位: 向源地址回传Postcard
    public static final int FLAG_POSTCARD_TO_PREV_HOP = 0x04;   // 第3位: 向上一跳回传Postcard
    
    // 保留其他位用于未来扩展 (0x08, 0x10, 0x20, 0x40, 0x80)

    public KLALBOAMHopByHopTLV(ByteBuffer klalbHeader) {
        super(klalbHeader);
    }

    @Override
    public String toString() {
        return "KLALBOAMHopByHopTLV [getVersion()=" + getVersion() + ", getFlags()=" + getFlags()
                + ", getInitialHops()=" + getInitialHops() + ", getUUID()=" + getUUID() 
                + ", isRecordPassport()=" + isRecordPassport()
                + ", isPostcardToSource()=" + isPostcardToSource()
                + ", isPostcardToPreviousHop()=" + isPostcardToPreviousHop()
                + "]";
    }

    public KLALBOAMHopByHopTLV(int version, int flags, int initialHops, UUID uuid,long timestamp) {
        super(IPv6HopByHopTLV.KLALB_OAM);
        setVersion(version);
        setFlags(flags);
        setInitialHops(initialHops);
        getData().put(3, (byte) 0);
        getData().put(4, (byte) 0);
        getData().put(5, (byte) 0);
        setUUID(uuid);
        setTimestamp(timestamp);
        getData().limit(30);
    }

    /**
     * 新的构造方法，通过boolean参数设置OAM测量相关的标志位
     * 
     * @param version 版本号
     * @param initialHops 初始跳数
     * @param recordPassport 是否启用记录Passport
     * @param postcardToSource 是否启用向源地址回传Postcard
     * @param postcardToPreviousHop 是否启用向上一跳回传Postcard
     * @param flowuuid 流UUID
     * @param sequence 序列号
     */
    public KLALBOAMHopByHopTLV(int version, int initialHops, boolean recordPassport, boolean postcardToSource,
                              boolean postcardToPreviousHop, UUID uuid   ,long timestamp) {
        super(IPv6HopByHopTLV.KLALB_OAM);
        
        setVersion(version);
        setInitialHops(initialHops);
        
        // 根据boolean参数计算flags值
        int flags = 0;
        if (recordPassport) flags |= FLAG_RECORD_PASSPORT;
        if (postcardToSource) flags |= FLAG_POSTCARD_TO_SOURCE;
        if (postcardToPreviousHop) flags |= FLAG_POSTCARD_TO_PREV_HOP;
        
        setFlags(flags);
        
        // 设置保留字段为0
        getData().put(3, (byte) 0);
        getData().put(4, (byte) 0);
        getData().put(5, (byte) 0);
        
        setUUID(uuid);
        setTimestamp(timestamp);
        getData().limit(30);
    }

     
   

	public int getVersion() {
        return getData().get(0) & 0xff;
    }

    public void setVersion(int version) {
        getData().put(0, (byte) version);
    }

    public int getFlags() {
        return getData().get(1) & 0xff;
    }

    public void setFlags(int flags) {
        getData().put(1, (byte) flags);
    }

    public int getInitialHops() {
        return getData().get(2) & 0xff;
    }

    public void setInitialHops(int initialHops) {
        getData().put(2, (byte) initialHops);
    }

    public void setUUID(UUID uuid) {
        getData().putLong(6, uuid.getMostSignificantBits());
        getData().putLong(14, uuid.getLeastSignificantBits());
    }

    public UUID getUUID() {
        return new UUID(getData().getLong(6), getData().getLong(14));
    }

    public long getTimestamp() {
        return getData().getLong(22);
    }

    public void setTimestamp(long sequence) {
        getData().putLong(22, sequence);
    }
    
    // 第1位: 记录Passport
    public boolean isRecordPassport() {
        return (getFlags() & FLAG_RECORD_PASSPORT) != 0;
    }

    public void setRecordPassport(boolean enabled) {
        int flags = getFlags();
        if (enabled) {
            flags |= FLAG_RECORD_PASSPORT;
        } else {
            flags &= ~FLAG_RECORD_PASSPORT;
        }
        setFlags(flags);
    }

    // 第2位: 向源地址回传Postcard
    public boolean isPostcardToSource() {
        return (getFlags() & FLAG_POSTCARD_TO_SOURCE) != 0;
    }

    public void setPostcardToSource(boolean enabled) {
        int flags = getFlags();
        if (enabled) {
            flags |= FLAG_POSTCARD_TO_SOURCE;
        } else {
            flags &= ~FLAG_POSTCARD_TO_SOURCE;
        }
        setFlags(flags);
    }

    // 第3位: 向上一跳回传Postcard
    public boolean isPostcardToPreviousHop() {
        return (getFlags() & FLAG_POSTCARD_TO_PREV_HOP) != 0;
    }

    public void setPostcardToPreviousHop(boolean enabled) {
        int flags = getFlags();
        if (enabled) {
            flags |= FLAG_POSTCARD_TO_PREV_HOP;
        } else {
            flags &= ~FLAG_POSTCARD_TO_PREV_HOP;
        }
        setFlags(flags);
    }

    @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);
    }

}