package com.bytedance.router;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import androidx.fragment.app.Fragment;
import com.bytedance.covode.number.Covode;
import com.bytedance.router.RouteIntent;
import com.bytedance.router.monitor.RouterMonitor;
import com.bytedance.router.util.Logger;
import com.bytedance.router.util.Util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

public class SmartRoute {
    public int enterAnim = -1;
    public int exitAnim = -1;
    public Bundle mAnimationBundle;
    public OpenResultCallback mCallback;
    public Context mContext;
    public Uri mData;
    public Boolean mExternalType = false;
    public Intent mExtraParams;
    public Fragment mFragment = null;
    public String mUrl = "";

    static {
        Covode.recordClassIndex(37165);
    }

    public SmartRoute withParam(String str, boolean z) {
        this.mExtraParams.putExtra(str, z);
        return this;
    }

    public SmartRoute withParam(String str, boolean[] zArr) {
        this.mExtraParams.putExtra(str, zArr);
        return this;
    }

    public SmartRoute withParam(String str, byte[] bArr) {
        this.mExtraParams.putExtra(str, bArr);
        return this;
    }

    public SmartRoute withParam(String str, char[] cArr) {
        this.mExtraParams.putExtra(str, cArr);
        return this;
    }

    public SmartRoute withParam(String str, CharSequence charSequence) {
        this.mExtraParams.putExtra(str, charSequence);
        return this;
    }

    public SmartRoute withParam(String str, CharSequence[] charSequenceArr) {
        this.mExtraParams.putExtra(str, charSequenceArr);
        return this;
    }

    public SmartRoute withParam(String str, double[] dArr) {
        this.mExtraParams.putExtra(str, dArr);
        return this;
    }

    public SmartRoute withParam(String str, float[] fArr) {
        this.mExtraParams.putExtra(str, fArr);
        return this;
    }

    public SmartRoute withParam(String str, int[] iArr) {
        this.mExtraParams.putExtra(str, iArr);
        return this;
    }

    public SmartRoute withParam(String str, long[] jArr) {
        this.mExtraParams.putExtra(str, jArr);
        return this;
    }

    public SmartRoute withParam(String str, Parcelable parcelable) {
        this.mExtraParams.putExtra(str, parcelable);
        return this;
    }

    public SmartRoute withParam(String str, Parcelable[] parcelableArr) {
        this.mExtraParams.putExtra(str, parcelableArr);
        return this;
    }

    public SmartRoute withParam(String str, Serializable serializable) {
        this.mExtraParams.putExtra(str, serializable);
        return this;
    }

    public SmartRoute withParam(String str, short s) {
        this.mExtraParams.putExtra(str, s);
        return this;
    }

    public SmartRoute withParam(String str, short[] sArr) {
        this.mExtraParams.putExtra(str, sArr);
        return this;
    }

    public SmartRoute withParam(String str, String str2) {
        this.mExtraParams.putExtra(str, str2);
        return this;
    }

    public SmartRoute withParam(String str, String[] strArr) {
        this.mExtraParams.putExtra(str, strArr);
        return this;
    }

    public RouteIntent buildRouteIntent() {
        RouteIntent.Builder builder = new RouteIntent.Builder();
        builder.withUrl(this.mUrl);
        builder.withParam(this.mExtraParams);
        builder.addFlags(this.mExtraParams.getFlags());
        builder.withAnimation(this.enterAnim, this.exitAnim);
        builder.withBundleAnimation(this.mAnimationBundle);
        builder.withCallback(this.mCallback);
        builder.withData(this.mData);
        builder.withExternalType(this.mExternalType.booleanValue());
        return builder.build();
    }

    public Intent buildIntent() {
        String startRoute = RouterMonitor.INSTANCE.startRoute(this.mUrl);
        RouteManager.getInstance().setTempResultCallback(this.mCallback);
        if (TextUtils.isEmpty(this.mUrl) || !Util.isLegalUrl(this.mUrl)) {
            Logger.e("SmartRoute#buildIntent error!!!");
            RouteManager.getInstance().getRouteCallbackProxy().onFail(this.mUrl, "SmartRoute#buildIntent error!");
            return null;
        }
        RouteIntent buildRouteIntent = buildRouteIntent();
        buildRouteIntent.setRouteId(startRoute);
        Intent buildIntent = RouteManager.getInstance().buildIntent(this.mContext, buildRouteIntent);
        RouterMonitor.INSTANCE.reportSuccess(startRoute);
        return buildIntent;
    }

    public void open() {
        String startRoute = RouterMonitor.INSTANCE.startRoute(this.mUrl);
        RouterMonitor.INSTANCE.collectParams(startRoute, INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(this.mExtraParams));
        RouteManager.getInstance().setTempResultCallback(this.mCallback);
        if (this.mContext == null || TextUtils.isEmpty(this.mUrl) || !Util.isLegalUrl(this.mUrl)) {
            Logger.e("SmartRoute#open error");
            RouterMonitor.INSTANCE.reportFail(startRoute, "SmartRoute#open error");
            RouteManager.getInstance().getRouteCallbackProxy().onFail(this.mUrl, "SmartRoute#open error");
            return;
        }
        RouteIntent buildRouteIntent = buildRouteIntent();
        buildRouteIntent.setRouteId(startRoute);
        RouteManager.getInstance().open(this.mContext, buildRouteIntent);
    }

    public SmartRoute withBundleAnimation(Bundle bundle) {
        this.mAnimationBundle = bundle;
        return this;
    }

    public SmartRoute withCallback(OpenResultCallback openResultCallback) {
        this.mCallback = openResultCallback;
        return this;
    }

    public SmartRoute withFragment(Fragment fragment) {
        this.mFragment = fragment;
        return this;
    }

    public SmartRoute withParam(Uri uri) {
        this.mData = uri;
        return this;
    }

    public SmartRoute withUrl(String str) {
        this.mUrl = str;
        return this;
    }

    public static Bundle INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(Intent intent) {
        try {
            return intent.getExtras();
        } catch (Exception unused) {
            return null;
        }
    }

    public SmartRoute addFlags(int i) {
        this.mExtraParams.addFlags(i);
        return this;
    }

    public void open(int i) {
        open(i, (OnActivityResultCallback) null);
    }

    public SmartRoute setExternal(boolean z) {
        this.mExternalType = Boolean.valueOf(z);
        return this;
    }

    public SmartRoute withParam(Bundle bundle) {
        if (bundle != null) {
            this.mExtraParams.putExtras(bundle);
        }
        return this;
    }

    public SmartRoute withNavArg(Parcelable parcelable) {
        if (parcelable != null) {
            this.mExtraParams.putExtra("smart_key_route_arg", parcelable);
        }
        return this;
    }

    public SmartRoute(Context context) {
        this.mContext = context;
        this.mExtraParams = new Intent();
    }

    public SmartRoute withParam(Intent intent) {
        if (INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(intent) != null) {
            if (INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(this.mExtraParams) == null) {
                this.mExtraParams.putExtras(new Bundle());
            }
            INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(this.mExtraParams).putAll(INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(intent));
        }
        return this;
    }

    public SmartRoute withAnimation(int i, int i2) {
        this.enterAnim = i;
        this.exitAnim = i2;
        return this;
    }

    public SmartRoute withParamCharSequenceList(String str, ArrayList<CharSequence> arrayList) {
        this.mExtraParams.putCharSequenceArrayListExtra(str, arrayList);
        return this;
    }

    public SmartRoute withParamIntegerList(String str, ArrayList<Integer> arrayList) {
        this.mExtraParams.putIntegerArrayListExtra(str, arrayList);
        return this;
    }

    public SmartRoute withParamParcelableList(String str, ArrayList<? extends Parcelable> arrayList) {
        this.mExtraParams.putParcelableArrayListExtra(str, arrayList);
        return this;
    }

    public SmartRoute withParamStringList(String str, ArrayList<String> arrayList) {
        this.mExtraParams.putStringArrayListExtra(str, arrayList);
        return this;
    }

    public SmartRoute withParam(String str, byte b) {
        this.mExtraParams.putExtra(str, b);
        return this;
    }

    public SmartRoute withParam(String str, char c) {
        this.mExtraParams.putExtra(str, c);
        return this;
    }

    private void sliceParams2Intent(Intent intent, String str) {
        Map sliceUrlParams = Util.sliceUrlParams(str);
        if (sliceUrlParams != null && sliceUrlParams.size() != 0) {
            for (Map.Entry entry : sliceUrlParams.entrySet()) {
                intent.putExtra((String) entry.getKey(), (String) entry.getValue());
            }
        }
    }

    public SmartRoute withParam(String str, double d) {
        this.mExtraParams.putExtra(str, d);
        return this;
    }

    public void open(int i, OnActivityResultCallback onActivityResultCallback) {
        String startRoute = RouterMonitor.INSTANCE.startRoute(this.mUrl);
        RouterMonitor.INSTANCE.collectParams(startRoute, INVOKEVIRTUAL_com_bytedance_router_SmartRoute_com_ss_android_ugc_aweme_lancet_IntentLancet_getExtras(this.mExtraParams));
        if (i != Integer.MIN_VALUE) {
            RouteManager.getInstance().setTempResultCallback(this.mCallback);
            if (this.mContext == null || TextUtils.isEmpty(this.mUrl) || !Util.isLegalUrl(this.mUrl) || !(this.mContext instanceof Activity)) {
                RouteManager.getInstance().getRouteCallbackProxy().onFail(this.mUrl, "SmartRoute#open(int requestCode) error!");
                Logger.e("SmartRoute#open(int requestCode) error");
                return;
            }
            RouteIntent buildRouteIntent = buildRouteIntent();
            buildRouteIntent.setRequestCode(i);
            buildRouteIntent.setRouteId(startRoute);
            buildRouteIntent.setOnActivityResultCallback(onActivityResultCallback);
            Fragment fragment = this.mFragment;
            if (fragment != null) {
                buildRouteIntent.setFragment(fragment);
            }
            RouteManager.getInstance().open(this.mContext, buildRouteIntent);
            return;
        }
        RouterMonitor.INSTANCE.reportFail(startRoute, "requestCode must not be Integer.MIN_VALUE:-2147483648 !!!");
        throw new IllegalArgumentException("requestCode must not be Integer.MIN_VALUE:-2147483648 !!!");
    }

    public SmartRoute withParam(String str, float f) {
        this.mExtraParams.putExtra(str, f);
        return this;
    }

    public SmartRoute withParam(String str, int i) {
        this.mExtraParams.putExtra(str, i);
        return this;
    }

    public SmartRoute withParam(String str, long j) {
        this.mExtraParams.putExtra(str, j);
        return this;
    }

    public SmartRoute withParam(String str, Bundle bundle) {
        this.mExtraParams.putExtra(str, bundle);
        return this;
    }
}
