package com.gyee.edge.protocol.iec60870.infoelement;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * Represents a section ready qualifier (SRQ) information element.
 */
public class IeSectionReadyQualifier extends InformationElement {

    private final int value;
    private final boolean sectionNotReady;

    public IeSectionReadyQualifier(int value, boolean sectionNotReady) {
        this.value = value;
        this.sectionNotReady = sectionNotReady;
    }

    static IeSectionReadyQualifier decode(DataInputStream is) throws IOException {
        int b1 = is.readUnsignedByte();
        int value = b1 & 0x7f;
        boolean sectionNotReady = ((b1 & 0x80) == 0x80);
        return new IeSectionReadyQualifier(value, sectionNotReady);
    }

    @Override
    int encode(byte[] buffer, int i) {
        buffer[i] = (byte) value;
        if (sectionNotReady) {
            buffer[i] |= 0x80;
        }
        return 1;
    }

    public int getValue() {
        return value;
    }

    public boolean isSectionNotReady() {
        return sectionNotReady;
    }

    @Override
    public String toString() {
        return "Section ready qualifier: " + value + ", section not ready: " + sectionNotReady;
    }
}
