package a.b.a.h;

import a.c.b.p;
import a.c.b.s;
import a.c.b.y;
import a.c.b.z.e;
import a.c.b.z.f;
import android.content.Context;
import android.util.Log;
import com.ucar.databus.proto.UCarProto;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class d {
    private static final String g = "UCarAuthService";

    /* renamed from: a  reason: collision with root package name */
    private boolean f28a = false;
    private final a.b.a.g.a b;
    private final c c;
    private final a.c.b.b0.a d = new a.c.b.b0.a();
    private final c e;
    private int f;

    public class a extends a.b.a.g.a {
        public a(a.c.b.z.b bVar, boolean z, boolean z2) {
            super(bVar, z, z2);
        }

        @Override // a.c.b.z.g.e, a.c.b.z.g.c, a.b.a.g.a
        public void j() {
            super.j();
            d.this.f28a = true;
        }

        @Override // a.c.b.z.g.e, a.c.b.z.g.c
        public void k() {
            d.this.f28a = false;
        }
    }

    public class b implements e {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ UCarProto.AuthResponse f29a;

        public b(UCarProto.AuthResponse authResponse) {
            this.f29a = authResponse;
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            if (!bool.booleanValue()) {
                return;
            }
            if (this.f29a.getResult() == 0) {
                d.this.c.a();
            } else if (this.f29a.getResult() == 2 || this.f29a.getResult() == 1 || this.f29a.getResult() == 3) {
                d.this.c.b();
            } else {
                Log.e(d.g, "wrong negotiation protocol version received");
            }
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(d.g, "Send auth response error.", exc);
        }
    }

    public interface c {
        void a();

        void a(String str, int i);

        void a(String str, String str2, String str3, int i);

        void b();

        int onSelectWorkMode(int i);
    }

    public d(Context context, c cVar, int i, int i2, String str) {
        this.e = new c(context);
        this.c = cVar;
        a aVar = new a(a.c.b.z.b.AUTH, true, false);
        this.b = aVar;
        aVar.a(new f.a(i, i2, str) {
            /* class a.b.a.h.$$Lambda$d$xSmc6Ry_U9DyaEi4W1YRqFn8Hrc */
            public final /* synthetic */ int f$1;
            public final /* synthetic */ int f$2;
            public final /* synthetic */ String f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            @Override // a.c.b.z.f.a
            public final void a(s sVar) {
                d.lambda$xSmc6Ry_U9DyaEi4W1YRqFn8Hrc(d.this, this.f$1, this.f$2, this.f$3, sVar);
            }
        });
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(int i, int i2, String str, s sVar) throws InterruptedException {
        int i3;
        c cVar;
        if (p.c(sVar)) {
            UCarProto.AuthRequest f2 = p.f(sVar);
            String str2 = new String(f2.getId().toByteArray(), StandardCharsets.UTF_8);
            if (f2.hasProductName()) {
                this.c.a(str2, f2.getModel(), f2.getProductName(), f2.getVersion());
            } else {
                this.c.a(str2, f2.getModel(), null, f2.getVersion());
            }
            if (f2.hasWorkModes()) {
                cVar = this.c;
                i3 = f2.getWorkModes();
            } else {
                cVar = this.c;
                i3 = 1;
            }
            this.f = cVar.onSelectWorkMode(y.a(i3).b(i));
            UCarProto.AuthResponse a2 = this.e.a(f2, this.f, i2, str);
            this.b.a(p.a(a2, sVar.m()), (e) new b(a2));
        } else if (p.b(sVar)) {
            String str3 = new String(this.d.a(p.e(sVar).getCipher().toByteArray()), StandardCharsets.UTF_8);
            String[] split = str3.split(":");
            if (split.length > 0) {
                this.c.a(split[0], this.f);
            } else {
                Log.e(g, "phone's authentication confirmation message is ill-formatted, msg=" + str3);
            }
        }
    }

    public void a() {
        a.a.a.b.a(g, "stop auth service");
        this.b.i();
        this.f28a = false;
    }

    public void a(String str) {
        this.e.e(str.getBytes(StandardCharsets.UTF_8));
    }

    public void b(String str) {
        try {
            a.a.a.b.a(g, "start auth service, binding=" + this.f28a);
            if (!this.f28a) {
                this.b.a(0, str);
            }
        } catch (IOException e2) {
            Log.e(g, "Start auth channel error.", e2);
        }
    }
}
