package org.kne.cloud.network.srv6;

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

public class DetNetSRv6TLV extends IPv6SegmentRoutingTLV {
    
    // 标志位掩码定义
    public static final int FLAG_GUARANTEED_DELIVERY = 0x01;    // 第1位: 承诺送达
    public static final int FLAG_AVOID_REORDERING = 0x02;       // 第2位: 避免乱序
    public static final int FLAG_RESOURCE_RESERVED = 0x04;      // 第3位: 资源预留
    public static final int FLAG_EXPLICIT_ROUTE = 0x08;         // 第4位: 显式路由
    
    // 保留其他位用于未来扩展 (0x10, 0x20, 0x40, 0x80)

    public DetNetSRv6TLV(ByteBuffer buffer) {
        super(buffer);
    }

    /**
     * 构造方法
     */
    public DetNetSRv6TLV(boolean guaranteedDelivery, boolean avoidReordering, 
                        boolean resourceReserved, boolean explicitRoute) {
        super(IPv6SegmentRoutingTLV.DETNET); // 使用建议的TLV类型编号
        
        int flags = 0;
        if (guaranteedDelivery) flags |= FLAG_GUARANTEED_DELIVERY;
        if (avoidReordering) flags |= FLAG_AVOID_REORDERING;
        if (resourceReserved) flags |= FLAG_RESOURCE_RESERVED;
        if (explicitRoute) flags |= FLAG_EXPLICIT_ROUTE;
        
        setFlags(flags);
        
        // DetNet TLV数据部分：1字节标志位 + 5字节保留
        getData().put(1, (byte) 0); // 保留
        getData().put(2, (byte) 0); // 保留  
        getData().put(3, (byte) 0); // 保留
        getData().put(4, (byte) 0); // 保留  
        getData().put(5, (byte) 0); // 保留
        
        getData().limit(6); // 总数据长度4字节
    }

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

    public void setFlags(int flags) {
        getData().put(0, (byte) flags);
    }
    // 标志位getter/setter方法
    public boolean isGuaranteedDelivery() {
        return (getData().get(0) & FLAG_GUARANTEED_DELIVERY) != 0;
    }

    public void setGuaranteedDelivery(boolean enabled) {
        byte flags = getData().get(0);
        if (enabled) {
            flags |= FLAG_GUARANTEED_DELIVERY;
        } else {
            flags &= ~FLAG_GUARANTEED_DELIVERY;
        }
        getData().put(0, flags);
    }

    public boolean isAvoidReordering() {
        return (getData().get(0) & FLAG_AVOID_REORDERING) != 0;
    }

    public void setAvoidReordering(boolean enabled) {
        byte flags = getData().get(0);
        if (enabled) {
            flags |= FLAG_AVOID_REORDERING;
        } else {
            flags &= ~FLAG_AVOID_REORDERING;
        }
        getData().put(0, flags);
    }

    public boolean isResourceReserved() {
        return (getData().get(0) & FLAG_RESOURCE_RESERVED) != 0;
    }

    public void setResourceReserved(boolean enabled) {
        byte flags = getData().get(0);
        if (enabled) {
            flags |= FLAG_RESOURCE_RESERVED;
        } else {
            flags &= ~FLAG_RESOURCE_RESERVED;
        }
        getData().put(0, flags);
    }

    public boolean isExplicitRoute() {
        return (getData().get(0) & FLAG_EXPLICIT_ROUTE) != 0;
    }

    public void setExplicitRoute(boolean enabled) {
        byte flags = getData().get(0);
        if (enabled) {
            flags |= FLAG_EXPLICIT_ROUTE;
        } else {
            flags &= ~FLAG_EXPLICIT_ROUTE;
        }
        getData().put(0, flags);
    }

    @Override
    public String toString() {
        return "DetNetSRv6TLV [" +
               "guaranteedDelivery=" + isGuaranteedDelivery() +
               ", avoidReordering=" + isAvoidReordering() +
               ", resourceReserved=" + isResourceReserved() +
               ", explicitRoute=" + isExplicitRoute() + "]";
    }

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