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

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

/**
 * Represents a qualifier of parameter of measured values (QPM) information element.
 */
public class IeQualifierOfParameterOfMeasuredValues extends InformationElement {

    private final int kindOfParameter;
    private final boolean change;
    private final boolean notInOperation;

    public IeQualifierOfParameterOfMeasuredValues(int kindOfParameter, boolean change, boolean notInOperation) {
        this.kindOfParameter = kindOfParameter;
        this.change = change;
        this.notInOperation = notInOperation;
    }

    IeQualifierOfParameterOfMeasuredValues(DataInputStream is) throws IOException {
        int b1 = (is.readByte() & 0xff);
        kindOfParameter = b1 & 0x3f;
        change = ((b1 & 0x40) == 0x40);
        notInOperation = ((b1 & 0x80) == 0x80);
    }

    @Override
    int encode(byte[] buffer, int i) {
        buffer[i] = (byte) kindOfParameter;
        if (change) {
            buffer[i] |= 0x40;
        }
        if (notInOperation) {
            buffer[i] |= 0x80;
        }
        return 1;
    }

    public int getKindOfParameter() {
        return kindOfParameter;
    }

    public boolean isChange() {
        return change;
    }

    public boolean isNotInOperation() {
        return notInOperation;
    }

    @Override
    public String toString() {
        return "Qualifier of parameter of measured values, kind of parameter: " + kindOfParameter + ", change: "
                + change + ", not in operation: " + notInOperation;
    }
}
