package at.acdp.urweb.fhpp;

import java.util.StringJoiner;

public class Status {
    //SCON
    public boolean opm1;
    public boolean opm2;
    public boolean fct;
    public boolean rdyen;
    public boolean fault;
    public boolean warn;
    public boolean open;
    public boolean enabled;

    //SPOS
    public boolean ref; /** antrieb referenzieren */
    public boolean still; /** stillstandsüberwachung */
    public boolean dev; /** schleppfehler */
    public boolean mov; /** achse bewegt sich */
    public boolean teach; /** Quittung teachen */
    public boolean mc; /** motion complete*/
    public boolean ack; /** quittung start*/
    public boolean halt; /** halt*/

    //SDIR
    public boolean func; /** funktion wird ausgeführt */
    public int fgrp; /** rückmeldung funktionsgruppe */
    public int fnum; /** rückmeldung funktionsmodus */
    public int com; /** rückmeldung regelmodus */
    public boolean abs;
    public int istMoment;
    public int istPosition;

    @Override
    public String toString() {
        return new StringJoiner("<br>", Status.class.getSimpleName() + "[", "]")
                .add("opm1=" + opm1)
                .add("opm2=" + opm2)
                .add("fct=" + fct)
                .add("rdyen=" + rdyen)
                .add("fault=" + fault)
                .add("warn=" + warn)
                .add("open=" + open)
                .add("enabled=" + enabled)
                .add("ref=" + ref)
                .add("still=" + still)
                .add("dev=" + dev)
                .add("mov=" + mov)
                .add("teach=" + teach)
                .add("mc=" + mc)
                .add("ack=" + ack)
                .add("halt=" + halt)
                .add("func=" + func)
                .add("fgrp=" + fgrp)
                .add("fnum=" + fnum)
                .add("com=" + com)
                .add("abs=" + abs)
                .add(("istMoment=" + istMoment))
                .add(("istPosition" + istPosition))
                .toString();
    }

    public static String byteArrayToHex(byte[] a) {
        StringBuilder sb = new StringBuilder(a.length * 2);
        for(byte b: a)
            sb.append(String.format("%02x", b));
        return sb.toString();
    }


    public void read(byte[] status) {
        byte scon=status[0];
        opm1 = (scon & (1 << 7))>0;
        opm2 = (scon & (1 << 6))>0;
        fct = (scon & (1 << 5))>0;
        rdyen = (scon & (1 << 4))>0;
        fault = (scon & (1 << 3))>0;
        warn = (scon & (1 << 2))>0;
        open = (scon & (1 << 1))>0;
        enabled = (scon & (1 << 0))>0;

        byte spos=status[1];
        ref = (spos & (1 << 7)) > 0;
        still = (spos & (1 << 6)) > 0;
        dev = (spos & (1 << 5)) > 0;
        mov = (spos & (1 << 4)) > 0;
        teach = (spos & (1 << 3)) > 0;
        mc = (spos & (1 << 2)) > 0;
        ack = (spos & (1 << 1)) > 0;
        halt = (spos & (1 << 0)) > 0;

        byte sdir=status[2];
        func = (sdir & (1 << 7)) > 0;
        fgrp = (sdir >> 5) & 0x3;
        fnum = (sdir >> 3) & 0x3;
        com = (sdir >> 1) & 0x3;
        abs = (sdir & (1 << 0)) > 0;

        istMoment = status[3];
        istPosition= ((status[4] &0xFF) << 24) | ((status[5] &0xFF) << 16) | ((status[6] &0xFF) << 8) | ((status[7] &0xFF));

    }
}
