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

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

/**
 * Represents an acknowledge file or section qualifier (AFQ) information element.
 */
public class IeAckFileOrSectionQualifier extends InformationElement {

    private final int action;
    private final int notice;

    public IeAckFileOrSectionQualifier(int action, int notice) {
        this.action = action;
        this.notice = notice;
    }

    static IeAckFileOrSectionQualifier decode(DataInputStream is) throws IOException {

        int b1 = is.readUnsignedByte();
        int action = b1 & 0x0f;
        int notice = (b1 >> 4) & 0x0f;
        return new IeAckFileOrSectionQualifier(action, notice);
    }

    @Override
    int encode(byte[] buffer, int i) {
        buffer[i] = (byte) (action | (notice << 4));
        return 1;
    }

    public int getRequest() {
        return action;
    }

    public int getFreeze() {
        return notice;
    }

    @Override
    public String toString() {
        return String.format("Acknowledge file or section qualifier, action: %d, notice: %d.", action, notice);
    }
}
