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

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

/**
 * Represents a file ready qualifier (FRQ) information element.
 */
public class IeFileReadyQualifier extends InformationElement {

    private final int value;
    private final boolean negativeConfirm;

    public IeFileReadyQualifier(int value, boolean negativeConfirm) {
        this.value = value;
        this.negativeConfirm = negativeConfirm;
    }

    static InformationElement decode(DataInputStream is) throws IOException {
        int b1 = is.readUnsignedByte();
        int value = b1 & 0x7f;
        boolean negativeConfirm = ((b1 & 0x80) == 0x80);

        return new IeFileReadyQualifier(value, negativeConfirm);
    }

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

    public int getValue() {
        return value;
    }

    public boolean isNegativeConfirm() {
        return negativeConfirm;
    }

    @Override
    public String toString() {
        return "File ready qualifier: " + value + ", negative confirm: " + negativeConfirm;
    }

}
