package com.tencent.mm.plugin.webview.luggage.jsapi;

import android.content.Context;
import com.tencent.luggage.d.n;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.compatible.util.f;
import com.tencent.mm.g.a.gw;
import com.tencent.mm.game.report.api.b;
import com.tencent.mm.modelstat.d;
import com.tencent.mm.plugin.downloader.model.e;
import com.tencent.mm.plugin.webview.luggage.c.c;
import com.tencent.mm.plugin.webview.ui.tools.game.i;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.at;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.ui.base.h;
import com.tencent.ytcommon.BuildConfig;
import org.json.JSONException;
import org.json.JSONObject;
import org.xwalk.core.XWalkFeature;

public class a extends bc<n> {
    public final String name() {
        return "addDownloadTaskStraight";
    }

    public final int biG() {
        return 2;
    }

    public final void b(com.tencent.luggage.d.a.a aVar) {
    }

    public final void a(Context context, String str, com.tencent.mm.plugin.webview.luggage.jsapi.bc.a aVar) {
        AppMethodBeat.i(6270);
        try {
            JSONObject jSONObject = new JSONObject(str);
            String optString = jSONObject.optString("task_url");
            long optLong = jSONObject.optLong("task_size");
            if (bo.isNullOrNil(optString)) {
                ab.e("MicroMsg.JsApiAddDownloadTaskStraight", "url is null");
                aVar.d("fail", null);
                AppMethodBeat.o(6270);
                return;
            }
            b.eBF.a(jSONObject.optString("appid"), jSONObject.optInt("scene", XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION), 9, -1, BuildConfig.FLAVOR, null);
            if (jSONObject.optBoolean("use_downloader_widget", false)) {
                i.a(context, com.tencent.mm.plugin.downloader_app.b.a.R(jSONObject), new 6(this, aVar));
                d.b(10, "LuggageGameWebViewUI_addDownloadTask", hashCode());
                AppMethodBeat.o(6270);
            } else if (!at.isNetworkConnected(context)) {
                aVar.d("fail_network_not_connected", null);
                c.runOnUiThread(new 1(this, context));
                ab.i("MicroMsg.JsApiAddDownloadTaskStraight", " fail, network not ready");
                AppMethodBeat.o(6270);
            } else if (!f.Mn()) {
                aVar.d("sdcard_not_ready", null);
                c.runOnUiThread(new 2(this, context));
                ab.i("MicroMsg.JsApiAddDownloadTaskStraight", " fail, sdcard not ready");
                AppMethodBeat.o(6270);
            } else if (optLong > 0 && !f.eW(optLong)) {
                aVar.d("has_not_enough_space", null);
                c.runOnUiThread(new 3(this, context));
                ab.i("MicroMsg.JsApiAddDownloadTaskStraight", "fail, not enough space, require size = ".concat(String.valueOf(optLong)));
                AppMethodBeat.o(6270);
            } else if (at.isWifi(context)) {
                a(jSONObject, aVar);
                AppMethodBeat.o(6270);
            } else {
                h.a(context, context.getString(2131305542), context.getString(2131305543), context.getString(2131305537), context.getString(2131296868), false, new 4(this, context, jSONObject, aVar), new 5(this, aVar), 2131690683);
                AppMethodBeat.o(6270);
            }
        } catch (JSONException e) {
            ab.e("MicroMsg.JsApiAddDownloadTaskStraight", "paras data error: " + e.getMessage());
            aVar.d("fail", null);
            AppMethodBeat.o(6270);
        }
    }

    private void a(JSONObject jSONObject, com.tencent.mm.plugin.webview.luggage.jsapi.bc.a aVar) {
        long b;
        AppMethodBeat.i(6271);
        gw gwVar = new gw();
        String optString = jSONObject.optString("task_name");
        String optString2 = jSONObject.optString("task_url");
        String optString3 = jSONObject.optString("alternative_url");
        long optLong = jSONObject.optLong("task_size");
        String optString4 = jSONObject.optString("file_md5");
        String optString5 = jSONObject.optString("extInfo");
        String optString6 = jSONObject.optString("fileType");
        String optString7 = jSONObject.optString("appid");
        String optString8 = jSONObject.optString("package_name");
        int optInt = jSONObject.optInt("scene", XWalkFeature.INTERFACE_GEO_LOCATION_PERMISSION);
        int optInt2 = jSONObject.optInt("downloader_type", 1);
        gwVar.cBC.url = optString2;
        gwVar.cBC.cvZ = optString4;
        gwVar.cBC.extInfo = optString5;
        gwVar.cBC.appId = optString7;
        gwVar.cBC.scene = optInt;
        com.tencent.mm.sdk.b.a.xxA.m(gwVar);
        com.tencent.mm.plugin.downloader.model.e.a aVar2 = new com.tencent.mm.plugin.downloader.model.e.a();
        aVar2.It(optString2);
        aVar2.Iu(optString3);
        aVar2.hA(optLong);
        aVar2.Iv(optString);
        aVar2.Iw(optString4);
        aVar2.setAppId(optString7);
        aVar2.cY(optString8);
        aVar2.ga(true);
        aVar2.tN(bo.getInt(optString6, 1));
        aVar2.setScene(optInt);
        e eVar = aVar2.kNl;
        if (optInt2 == 1) {
            b = com.tencent.mm.plugin.downloader.model.d.bij().b(eVar);
        } else {
            b = com.tencent.mm.plugin.downloader.model.d.bij().a(eVar);
        }
        d.b(10, "LuggageGameWebViewUI_addDownloadTask", hashCode());
        if (b <= 0) {
            aVar.d("fail", null);
            AppMethodBeat.o(6271);
            return;
        }
        try {
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("download_id", b);
            aVar.d(null, jSONObject2);
            AppMethodBeat.o(6271);
        } catch (JSONException e) {
            aVar.d(null, null);
            AppMethodBeat.o(6271);
        }
    }
}
