package a.c.b;

import java.nio.ByteBuffer;
import java.util.Calendar;

public class h {
    public static final int g = 20;
    private static final int h = 0;
    private static final int i = 4;
    private static final int j = 8;
    private static final int k = 12;
    private static final int l = 13;
    private static final int m = 14;
    private static final int n = 16;
    private static final int o = 18;
    private static final int p = 18;
    private static final ThreadLocal<byte[]> q = ThreadLocal.withInitial($$Lambda$h$Yo5OotlcfY_HhuMxwWyGnE0Kddw.INSTANCE);
    private static final byte r = 6;
    private static final byte s = 24;

    /* renamed from: a  reason: collision with root package name */
    private final d f74a;
    private int b;
    private int c;
    private int d;
    private short e;
    private short f;

    private h(int i2, int i3, int i4, d dVar, short s2, short s3) {
        j.b(dVar, "cmdDescription");
        this.b = i2;
        this.c = i3;
        this.d = i4;
        this.f74a = dVar;
        this.f = s2;
        this.e = s3;
    }

    public h(int i2, d dVar) {
        j.b(dVar, "cmdDescription");
        this.b = i2;
        if (i2 >= 20) {
            this.c = n.a();
            this.d = (int) (System.currentTimeMillis() / 1000);
            this.f74a = dVar;
            this.f = 0;
            l();
            return;
        }
        throw new l("Illegal message size: " + i2);
    }

    private static d a(byte[] bArr, d dVar) {
        int i2 = bArr[12] & 1;
        o oVar = o.PHONE;
        if (i2 != oVar.a()) {
            oVar = o.CAR;
        }
        int i3 = (bArr[12] & 6) >>> 1;
        f fVar = f.PB3;
        dVar.a(oVar, i3 == fVar.a() ? fVar : f.RAW, a(bArr[12]), b(bArr[13]), (bArr[14] << 8) + bArr[15]);
        return dVar;
    }

    private static i a(byte b2) {
        int i2 = (b2 & 24) >>> 3;
        i iVar = i.REQ;
        if (i2 == iVar.a()) {
            return iVar;
        }
        i iVar2 = i.RES;
        if (i2 == iVar2.a()) {
            return iVar2;
        }
        i iVar3 = i.SEND_SYNC;
        return i2 == iVar3.a() ? iVar3 : i.SEND;
    }

    private static c b(int i2) {
        c cVar = c.NONE;
        c[] values = c.values();
        int i3 = 0;
        while (true) {
            if (i3 >= 8) {
                break;
            }
            c cVar2 = values[i3];
            if (i2 == cVar2.a()) {
                cVar = cVar2;
                break;
            }
            i3++;
        }
        if (cVar != c.NONE) {
            return cVar;
        }
        throw new l("Unknown CmdCategory: " + i2);
    }

    public static h b(byte[] bArr) {
        j.b(bArr, "rawBytes");
        if (bArr.length >= 20) {
            int a2 = b.a(bArr, 0);
            int a3 = b.a(bArr, 4);
            int a4 = b.a(bArr, 8);
            d a5 = a(bArr, new d());
            short b2 = b.b(bArr, 16);
            short b3 = b.b(bArr, 18);
            short a6 = e.a(bArr, 0, 18);
            if (b3 == a6) {
                return new h(a2, a3, a4, a5, b2, b3);
            }
            throw new l("Invalid header, checksum expect: " + Integer.toHexString(a6) + ", actual: " + Integer.toHexString(b3));
        }
        throw new l("Protocol header bytes size[" + bArr.length + "] < " + 20);
    }

    private void b(ByteBuffer byteBuffer) {
        o e2 = this.f74a.e();
        f b2 = this.f74a.b();
        c a2 = this.f74a.a();
        i c2 = this.f74a.c();
        j.a((Object) e2, "sourceDevice");
        j.a((Object) b2, "dataFormat");
        j.a((Object) b2, "dataFormat");
        j.a((Object) a2, "messageType");
        byteBuffer.put((byte) (e2.a() + (b2.a() << 1) + (c2.a() << 3)));
        byteBuffer.put((byte) a2.a());
        byteBuffer.put((byte) this.f74a.d());
    }

    private void d(byte[] bArr) {
        o e2 = this.f74a.e();
        f b2 = this.f74a.b();
        c a2 = this.f74a.a();
        i c2 = this.f74a.c();
        j.a((Object) e2, "sourceDevice");
        j.a((Object) b2, "dataFormat");
        j.a((Object) b2, "dataFormat");
        j.a((Object) a2, "messageType");
        bArr[12] = (byte) e2.a();
        bArr[12] = (byte) (bArr[12] + ((byte) (b2.a() << 1)));
        bArr[12] = (byte) (bArr[12] + ((byte) (c2.a() << 3)));
        bArr[13] = (byte) a2.a();
        bArr[14] = (byte) ((this.f74a.d() >>> 8) & 255);
        bArr[15] = (byte) (this.f74a.d() & 255);
    }

    /* access modifiers changed from: private */
    public static /* synthetic */ byte[] h() {
        return new byte[20];
    }

    public static h i() {
        return new h(20, new d(v.a(), f.RAW, i.SEND, c.CONTROL, 0));
    }

    public static byte[] j() {
        return q.get();
    }

    private void l() {
        c(j());
    }

    public h a() {
        return new h(d(), f(), g(), c(), e(), this.e);
    }

    public h a(int i2, f fVar, i iVar, c cVar, int i3) {
        return a(i2, 0, k.c(), fVar, iVar, cVar, i3);
    }

    public h a(int i2, h hVar, int i3) {
        this.b = i2 + 20;
        this.c = hVar.c;
        this.d = (int) (System.currentTimeMillis() / 1000);
        d c2 = hVar.c();
        this.f74a.a(k.c(), c2.b(), i.RES, c2.a(), i3);
        this.f = hVar.f;
        l();
        return this;
    }

    public h a(int i2, short s2, o oVar, f fVar, i iVar, c cVar, int i3) {
        this.b = i2;
        this.c = n.a();
        this.d = (int) (System.currentTimeMillis() / 1000);
        this.f74a.a(oVar, fVar, iVar, cVar, i3);
        this.f = s2;
        l();
        return this;
    }

    public h a(h hVar) {
        this.b = hVar.b;
        this.c = hVar.c;
        this.d = hVar.d;
        d c2 = hVar.c();
        this.f74a.a(c2.e(), c2.b(), c2.c(), c2.a(), c2.d());
        this.f = hVar.f;
        return this;
    }

    public final void a(int i2) {
        if (i2 >= 20) {
            this.b = i2;
            return;
        }
        throw new l("Update message length error: " + i2);
    }

    public void a(i iVar) {
        c().a(iVar);
        l();
    }

    public final void a(ByteBuffer byteBuffer) {
        j.b(byteBuffer, "byteBuf");
        if (byteBuffer.remaining() >= 20) {
            byteBuffer.putInt(this.b);
            byteBuffer.putInt(this.c);
            byteBuffer.putInt(this.d);
            b(byteBuffer);
            byteBuffer.putShort(this.f);
            short a2 = e.a(byteBuffer, 0, 18);
            this.e = a2;
            byteBuffer.putShort(a2);
            return;
        }
        throw new l("Can not write header to ByteBuffer");
    }

    public final void a(short s2) {
        this.f = s2;
        l();
    }

    public void a(byte[] bArr) {
        j.b(bArr, "rawBytes");
        if (bArr.length >= 20) {
            int a2 = b.a(bArr, 0);
            int a3 = b.a(bArr, 4);
            int a4 = b.a(bArr, 8);
            short b2 = b.b(bArr, 16);
            short b3 = b.b(bArr, 18);
            short a5 = e.a(bArr, 0, 18);
            if (b3 == a5) {
                a(bArr, c());
                this.b = a2;
                this.c = a3;
                this.d = a4;
                this.f = b2;
                return;
            }
            throw new l("Invalid header, checksum expect: " + Integer.toHexString(a5) + ", actual: " + Integer.toHexString(b3));
        }
        throw new l("Protocol header bytes size[" + bArr.length + "] < " + 20);
    }

    public final String b() {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(((long) this.d) * 1000);
        return "--------------------------------\n" + "length: " + this.b + ", seqId: " + this.c + ", checksum: " + Integer.toHexString(this.e & 65535) + '\n' + "timestamp(s): " + this.d + " (" + instance.getTime() + ")\ncmd: " + this.f74a + '\n' + "--------------------------------\n";
    }

    public final d c() {
        return this.f74a;
    }

    public final void c(int i2) {
        if (i2 >= 0) {
            this.b = i2 + 20;
            l();
            return;
        }
        throw new l("Message body length must not less than 0");
    }

    public final void c(byte[] bArr) {
        j.b(bArr, "headerBytes");
        if (bArr.length >= 20) {
            b.a(bArr, 0, this.b);
            b.a(bArr, 4, this.c);
            b.a(bArr, 8, this.d);
            d(bArr);
            b.a(bArr, 16, this.f);
            short a2 = e.a(bArr, 0, 18);
            this.e = a2;
            b.a(bArr, 18, a2);
            return;
        }
        throw new l("Can not write bytes to headerBytes");
    }

    public final int d() {
        return this.b;
    }

    public final void d(int i2) {
        if (i2 >= 20) {
            this.b = i2;
            l();
            return;
        }
        throw new l("Update message length error: " + i2);
    }

    public short e() {
        return this.f;
    }

    public final void e(int i2) {
        if (i2 < 0) {
            throw new l("seq id must greater than 0");
        } else if (this.f74a.c() == i.RES) {
            this.c = i2;
            l();
        } else {
            throw new l(this.f74a.c() + " can not update seq id");
        }
    }

    public final int f() {
        return this.c;
    }

    public final int g() {
        return this.d;
    }

    public final byte[] k() {
        byte[] bArr = q.get();
        c(bArr);
        return bArr;
    }

    public final void m() {
        this.d = (int) (System.currentTimeMillis() / 1000);
        l();
    }

    public String toString() {
        return b();
    }
}
