package a.c.d.g;

import a.c.b.b0.a;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucarsink.sink.natives.SinkNative;
import java.nio.charset.StandardCharsets;
import org.json.JSONException;
import org.json.JSONObject;

public class b {
    private static final String h = "UibcManager";
    private static final String i = "uibc_session_key";
    private static final String j = "type";
    private static final String k = "action";
    private static final String l = "keycode";
    private static final String m = "metaState";
    private static final String n = "width";
    private static final String o = "height";
    private static final String p = "count";
    private static final int q = 1;
    private static final int r = 2;

    /* renamed from: a  reason: collision with root package name */
    private volatile boolean f132a = false;
    private a b;
    private String c;
    private int d;
    private int e;
    private int f;
    private int g;

    private int a(int i2) {
        return (int) (((((float) i2) * 1.0f) / ((float) this.f)) * ((float) this.d));
    }

    private int b(int i2) {
        return (int) (((((float) i2) * 1.0f) / ((float) this.g)) * ((float) this.e));
    }

    private boolean b() {
        return this.b != null;
    }

    public String a() {
        return this.c;
    }

    public void a(String str) {
        this.c = str;
    }

    public void a(boolean z) {
        a aVar;
        if (z) {
            a.a.a.b.c(h, "uibc encryption enabled");
            aVar = new a(i);
        } else {
            a.a.a.b.c(h, "uibc encryption disabled");
            aVar = null;
        }
        this.b = aVar;
        this.f132a = true;
    }

    public boolean a(int i2, int i3, int[] iArr, int[] iArr2, int[] iArr3) {
        if (iArr.length != i3 || iArr2.length != i3 || iArr3.length != i3) {
            a.a.a.b.b(h, "sendTouchEvent failed: count error!");
            return false;
        } else if (!this.f132a) {
            a.a.a.b.b(h, "sendTouchEvent failed: encryption is not settled");
            return false;
        } else if (this.d <= 0 || this.e <= 0) {
            a.a.a.b.b(h, "sendTouchEvent failed: source size haven't been initialized!");
            return false;
        } else {
            a.a.a.b.a(h, "sendTouchEvent, action: " + i2);
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("type", 1);
                jSONObject.put(k, i2);
                jSONObject.put(n, this.f);
                jSONObject.put(o, this.g);
                jSONObject.put(p, i3);
                for (int i4 = 0; i4 < i3; i4++) {
                    jSONObject.put("trackID" + i4, iArr[i4]);
                    int a2 = a(iArr2[i4]);
                    int b2 = b(iArr3[i4]);
                    jSONObject.put("x" + i4, a2);
                    jSONObject.put("y" + i4, b2);
                    a.a.a.b.a(h, "origin point: (" + iArr2[i4] + ", " + iArr3[i4] + "), display size: (" + this.f + ", " + this.g + "), source size: (" + this.d + ", " + this.e + "), mapped point: (" + a2 + ", " + b2 + ")");
                }
                byte[] bytes = jSONObject.toString().getBytes(StandardCharsets.UTF_8);
                if (b()) {
                    bytes = this.b.b(bytes);
                }
                return SinkNative.onUibcEvent(this.c, bytes);
            } catch (JSONException e2) {
                a.a.a.b.b(h, "sendTouchEvent JSONException", e2);
                return false;
            }
        }
    }

    public boolean a(UCarCommon.KeyEventActionType keyEventActionType, UCarCommon.KeyCodeType keyCodeType, int i2) {
        if (keyEventActionType == null || keyCodeType == null || keyEventActionType == UCarCommon.KeyEventActionType.KEY_EVENT_ACTION_UNDEFINED || keyCodeType == UCarCommon.KeyCodeType.KEY_CODE_UNDEFINED) {
            a.a.a.b.b(h, " sendKeyEvent() args error");
            return false;
        } else if (!this.f132a) {
            a.a.a.b.b(h, "sendTouchEvent failed: encryption is not settled");
            return false;
        } else {
            a.a.a.b.a(h, "sendKeyEvent");
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("type", 2);
                jSONObject.put(k, keyEventActionType.getValue());
                jSONObject.put(l, keyCodeType.getValue());
                jSONObject.put(m, i2);
                a.a.a.b.a(h, "on key event: " + jSONObject);
                byte[] bytes = jSONObject.toString().getBytes(StandardCharsets.UTF_8);
                if (b()) {
                    bytes = this.b.b(bytes);
                }
                return SinkNative.onUibcEvent(this.c, bytes);
            } catch (JSONException e2) {
                a.a.a.b.b(h, "sendKeyEvent JSONException", e2);
                return false;
            }
        }
    }

    public void c(int i2) {
        this.g = i2;
    }

    public void d(int i2) {
        this.f = i2;
    }

    public void e(int i2) {
        this.e = i2;
    }

    public void f(int i2) {
        this.d = i2;
    }
}
