package com.seewo.remote.e.j;

import android.util.Log;
import com.seewo.remote.g.w;
import java.util.LinkedList;
import java.util.List;

/* JADX INFO: compiled from: CommonCmdParser.java */
/* JADX INFO: loaded from: classes.dex */
public class d implements c {
    private final byte[] a;
    private final byte[] b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final f.a.r.a<byte[]> f265c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private List<Byte> f266d;

    /* JADX INFO: compiled from: CommonCmdParser.java */
    public static class b {
        byte[] a = new byte[0];
        byte[] b = new byte[0];

        public d a() {
            if (this.b.length != 0) {
                return new d(this);
            }
            throw new e();
        }

        public b b(byte[] bArr) {
            this.a = bArr;
            Log.v("CommonCommandParser", "set head:" + w.a(bArr));
            return this;
        }

        public b c(byte[] bArr) {
            this.b = bArr;
            return this;
        }
    }

    private void c() {
        this.f266d.clear();
    }

    private boolean d() {
        int size = this.f266d.size();
        if (this.f266d.size() < this.b.length) {
            return false;
        }
        int i2 = 0;
        while (true) {
            byte[] bArr = this.b;
            if (i2 >= bArr.length) {
                return true;
            }
            if (this.f266d.get((size - bArr.length) + i2).byteValue() != this.b[i2]) {
                return false;
            }
            i2++;
        }
    }

    @Override // com.seewo.remote.e.j.c
    public f.a.d<byte[]> a() {
        return this.f265c;
    }

    @Override // com.seewo.remote.e.j.c
    public void b(byte b2) {
        boolean z;
        boolean z2;
        this.f266d.add(Byte.valueOf(b2));
        if (d()) {
            int size = this.f266d.size();
            if (size < this.b.length + this.a.length) {
                Log.v("CommonCommandParser", "length error, clear the command");
                c();
                return;
            }
            e.c.e.f.e.g("CommonCommandParser", "before cmd:" + this.f266d + ", head:" + w.a(this.a));
            int length = this.a.length;
            int i2 = 0;
            int i3 = 0;
            while (true) {
                z = true;
                if (i2 >= size) {
                    i2 = i3;
                    z = false;
                    break;
                }
                if (size - i2 > length) {
                    int i4 = 0;
                    while (true) {
                        if (i4 >= length) {
                            z2 = true;
                            break;
                        } else {
                            if (this.f266d.get(i2 + i4).byteValue() != this.a[i4]) {
                                z2 = false;
                                break;
                            }
                            i4++;
                        }
                    }
                    if (z2) {
                        break;
                    }
                }
                i3 = i2;
                i2++;
            }
            Log.v("CommonCommandParser", "headPosition:" + i2);
            if (!z) {
                Log.v("CommonCommandParser", "head is error, clear the command");
                c();
                return;
            }
            if (i2 != 0) {
                List<Byte> listSubList = this.f266d.subList(i2, size);
                this.f266d = listSubList;
                size = listSubList.size();
            }
            e.c.e.f.e.g("CommonCommandParser", "after cmd:" + this.f266d);
            byte[] bArr = new byte[size];
            for (int i5 = 0; i5 < size; i5++) {
                bArr[i5] = this.f266d.get(i5).byteValue();
            }
            c();
            e.c.e.f.e.g("CommonCommandParser", "next: " + w.a(bArr));
            this.f265c.d(bArr);
        }
    }

    private d(b bVar) {
        this.f265c = f.a.r.a.x();
        this.f266d = new LinkedList();
        this.a = bVar.a;
        this.b = bVar.b;
    }
}
