package com.tencent.mm.plugin.appbrand.jsapi.c;

import android.annotation.TargetApi;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.compatible.util.d;
import com.tencent.mm.plugin.appbrand.g;
import com.tencent.mm.plugin.appbrand.jsapi.a;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.vending.j.b;
import com.tencent.ytcommon.BuildConfig;
import java.util.HashMap;
import java.util.UUID;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@TargetApi(18)
public final class c extends a {
    private static final int CTRL_INDEX = 221;
    private static final String NAME = "startBeaconDiscovery";
    private a$a.a hKb;
    com.tencent.mm.plugin.appbrand.g.c hKg = null;

    public final void a(com.tencent.mm.plugin.appbrand.jsapi.c cVar, JSONObject jSONObject, int i) {
        AppMethodBeat.i(94364);
        ab.i("MicroMsg.JsApiStartBeaconDiscovery", "startBeaconDiscovery data %s", new Object[]{jSONObject});
        UUID[] z = z(jSONObject);
        if (z == null || z.length <= 0) {
            HashMap hashMap = new HashMap();
            hashMap.put("errCode", Integer.valueOf(11006));
            cVar.M(i, j("fail:invalid data", hashMap));
            AppMethodBeat.o(94364);
            return;
        }
        b F;
        String appId = cVar.getAppId();
        a$a AZ = a.AZ(cVar.getAppId());
        if (AZ == null) {
            ab.i("MicroMsg.JsApiStartBeaconDiscovery", "beaconWorker init");
            AZ = new a$a();
            a.a(appId, AZ);
        }
        if (this.hKb == null) {
            ab.i("MicroMsg.JsApiStartBeaconDiscovery", "onBeaconScanCallback init");
            this.hKb = new 1(this, cVar);
        }
        if (this.hKg == null) {
            ab.i("MicroMsg.JsApiStartBeaconDiscovery", "listener init");
            this.hKg = new 2(this, cVar);
            g.a(cVar.getAppId(), this.hKg);
        }
        AZ.hKa = z;
        AZ.hKb = this.hKb;
        ab.i("MicroMsg.BeaconManager", "BeaconWorker:%d start", new Object[]{Integer.valueOf(AZ.hashCode())});
        if (AZ.isStart()) {
            ab.i("MicroMsg.BeaconManager", "BeaconWorker:%d, already start", new Object[]{Integer.valueOf(AZ.hashCode())});
            F = com.tencent.mm.vending.j.c.F(Integer.valueOf(11003), "fail:already start");
        } else if (d.fP(18)) {
            ab.e("MicroMsg.BeaconManager", "API version is below 18!");
            F = com.tencent.mm.vending.j.c.F(Integer.valueOf(11000), "fail:not support");
        } else if (AZ.hJY == null) {
            ab.e("MicroMsg.BeaconManager", "bluetoothAdapter is null!");
            F = com.tencent.mm.vending.j.c.F(Integer.valueOf(11001), "fail:bluetooth service is unavailable");
        } else if (!AZ.hJY.isEnabled()) {
            ab.e("MicroMsg.BeaconManager", "bluetoothAdapter is null!");
            F = com.tencent.mm.vending.j.c.F(Integer.valueOf(11001), "fail:bluetooth service is unavailable");
        } else if (AZ.hJY.isDiscovering()) {
            ab.e("MicroMsg.BeaconManager", "bluetoothAdapter is Discovering!");
            F = com.tencent.mm.vending.j.c.F(Integer.valueOf(11003), "fail:already start");
        } else {
            AZ.hJZ.clear();
            ab.i("MicroMsg.BeaconManager", "startLeScan:%b", new Object[]{Boolean.valueOf(AZ.hJY.startLeScan(AZ.hKe))});
            if (AZ.hJY.startLeScan(AZ.hKe)) {
                AZ.cFy = true;
                F = com.tencent.mm.vending.j.c.F(Integer.valueOf(0), BuildConfig.FLAVOR);
            } else {
                F = com.tencent.mm.vending.j.c.F(Integer.valueOf(11005), "fail:system error");
            }
        }
        HashMap hashMap2 = new HashMap();
        hashMap2.put("errCode", F.get(0));
        cVar.M(i, j(((Integer) F.get(0)).intValue() == 0 ? "ok" : (String) F.get(1), hashMap2));
        AppMethodBeat.o(94364);
    }

    private static UUID[] z(JSONObject jSONObject) {
        AppMethodBeat.i(94365);
        UUID[] uuidArr = null;
        if (jSONObject.has("uuids")) {
            try {
                JSONArray jSONArray = new JSONArray(jSONObject.optString("uuids"));
                uuidArr = new UUID[jSONArray.length()];
                for (int i = 0; i < jSONArray.length(); i++) {
                    ab.d("MicroMsg.JsApiStartBeaconDiscovery", "uuid %s", new Object[]{jSONArray.getString(i)});
                    uuidArr[i] = UUID.fromString(r3);
                }
            } catch (JSONException e) {
                ab.e("MicroMsg.JsApiStartBeaconDiscovery", "get uuid error!");
            }
        }
        AppMethodBeat.o(94365);
        return uuidArr;
    }
}
