package com.bytedance.router;

import X.0HH;
import X.8uQ;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.router.dynamic.RouterConfig;
import com.bytedance.router.dynamic.ServerParam;
import com.bytedance.router.mapping.ExternalSmartRouter$$Mapping;
import com.bytedance.router.mapping.SmartRouter$$Mapping;
import com.bytedance.router.util.Logger;
import com.bytedance.router.util.Util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class RouteMapper {
    public IConfigUpdateListener mConfigUpdateListener;
    public Context mContext;
    public Map<String, List<String>> mExternalRouteInterceptorMap = new HashMap();
    public Map<String, String> mExternalRouteMap = new HashMap(this.mExternalSmartRouter$$Mapping.getMapSize() + 1, 1.0f);
    public ExternalSmartRouter$$Mapping mExternalSmartRouter$$Mapping = new ExternalSmartRouter$$Mapping();
    public IMappingInitializer mIMappingInitializer;
    public AtomicBoolean mIsInited = new AtomicBoolean(false);
    public Map<String, String> mOriginExternalMap;
    public Map<String, String> mOriginMap;
    public Map<String, List<String>> mRouteInterceptorMap = new HashMap();
    public Map<String, String> mRouteMap = new HashMap(this.mSmartRouter$$Mapping.getMapSize() + 1, 1.0f);
    public final Object mRouteMapLock = new Object();
    public ServerParam mServerParam;
    public SmartRouter$$Mapping mSmartRouter$$Mapping = new SmartRouter$$Mapping();
    public List<String> mTestUrlList = new ArrayList();

    public interface IConfigUpdateListener {
        static {
            Covode.recordClassIndex(37160);
        }

        void onConfigChanged(RouterConfig routerConfig);
    }

    static {
        Covode.recordClassIndex(37157);
    }

    public void updateNetRouteConfig() {
    }

    public Map<String, String> getExternalRouteMap() {
        return this.mExternalRouteMap;
    }

    public Map<String, String> getRouteMap() {
        return this.mRouteMap;
    }

    public ServerParam getServerParam() {
        return this.mServerParam;
    }

    public List<String> getTestUrlList() {
        return this.mTestUrlList;
    }

    public void requestRouteConfig() {
        ServerParam serverParam = this.mServerParam;
        if (serverParam == null || !serverParam.isAvailable()) {
            Logger.e("RouteMapper#requestRouteConfig serverParam is null or unavailable,You must use SmartRouter#init(Context, ServerParam) before !!!");
        } else {
            AsyncTask.execute(new Runnable() {
                static {
                    Covode.recordClassIndex(37159);
                }

                public void run() {
                    RouteMapper.this.updateNetRouteConfig();
                }
            });
        }
    }

    private void initRouteMap() {
        MethodCollector.i(10564);
        synchronized (this.mRouteMapLock) {
            try {
                if (!this.mIsInited.get()) {
                    this.mSmartRouter$$Mapping.init(this.mRouteMap);
                    this.mExternalSmartRouter$$Mapping.init(this.mExternalRouteMap);
                    this.mSmartRouter$$Mapping.initInterceptorMap(this.mRouteInterceptorMap);
                    this.mExternalSmartRouter$$Mapping.initInterceptorMap(this.mExternalRouteInterceptorMap);
                    this.mIsInited.set(true);
                }
            } finally {
                MethodCollector.o(10564);
            }
        }
    }

    public void loadLocalRouteConfig() {
        String string = 8uQ.LIZ(this.mContext, "smartrouter_conf", 0).getString("smartrouter_config", "");
        if (!TextUtils.isEmpty(string)) {
            RouterConfig load = RouterConfig.load(string);
            if (this.mConfigUpdateListener != null && load != null && !RouterConfig.isExpired(this.mContext, load)) {
                this.mConfigUpdateListener.onConfigChanged(load);
                if (Logger.isDebug()) {
                    Logger.d("Load local routerConfig: ".concat(String.valueOf(string)));
                }
            }
        }
    }

    public void setCustomInitializer(IMappingInitializer iMappingInitializer) {
        this.mIMappingInitializer = iMappingInitializer;
    }

    public void init(Context context) {
        init(context, (ServerParam) null, (IConfigUpdateListener) null);
    }

    private void initDynamicRoutes(ServerParam serverParam) {
        this.mServerParam = serverParam;
        if (serverParam == null || !serverParam.isAvailable()) {
            Logger.e("RouteMapper#initDynamicRoutes serverParam is null or unavailable!!!");
        } else {
            AsyncTask.execute(new Runnable() {
                static {
                    Covode.recordClassIndex(37158);
                }

                public void run() {
                    RouteMapper.this.loadLocalRouteConfig();
                    RouteMapper.this.updateNetRouteConfig();
                }
            });
        }
    }

    public void updateMapping(Map<String, String> map) {
        MethodCollector.i(9791);
        synchronized (this.mRouteMapLock) {
            try {
                if (this.mOriginMap == null) {
                    HashMap hashMap = new HashMap();
                    this.mOriginMap = hashMap;
                    hashMap.putAll(this.mRouteMap);
                    this.mRouteMap.putAll(map);
                } else {
                    HashMap hashMap2 = new HashMap();
                    hashMap2.putAll(this.mOriginMap);
                    hashMap2.putAll(map);
                    this.mRouteMap = hashMap2;
                }
            } finally {
                MethodCollector.o(9791);
            }
        }
    }

    public boolean installModuleMapping(String str) {
        MethodCollector.i(10023);
        if (TextUtils.isEmpty(str)) {
            MethodCollector.o(10023);
            return false;
        }
        try {
            IMappingInitializer newInstance = Class.forName(0HH.LIZ("com.bytedance.router.mapping.SmartRouter$$Mapping$$%s", new Object[]{str.replace(".", "_")})).newInstance();
            if (newInstance instanceof IMappingInitializer) {
                synchronized (this.mRouteMapLock) {
                    newInstance.init(this.mRouteMap);
                    newInstance.initInterceptorMap(this.mRouteInterceptorMap);
                }
                MethodCollector.o(10023);
                return true;
            }
        } catch (ClassNotFoundException e) {
            0HH.LIZ(e);
        } catch (InstantiationException e2) {
            0HH.LIZ(e2);
        } catch (IllegalAccessException e3) {
            0HH.LIZ(e3);
        } catch (Throwable th) {
            MethodCollector.o(10023);
            throw th;
        }
        MethodCollector.o(10023);
        return false;
    }

    public boolean loadModuleMapping(String str) {
        MethodCollector.i(10301);
        if (TextUtils.isEmpty(str)) {
            MethodCollector.o(10301);
            return false;
        }
        try {
            IMappingInitializer newInstance = Class.forName(0HH.LIZ("com.bytedance.router.generator.mapping.SmartrouterMapping$$%s", new Object[]{str})).newInstance();
            if (newInstance.getClass() == SmartRouter$$Mapping.class) {
                synchronized (this.mRouteMapLock) {
                    newInstance.init(this.mRouteMap);
                    newInstance.initInterceptorMap(this.mRouteInterceptorMap);
                }
                MethodCollector.o(10301);
                return true;
            }
            if (newInstance.getClass() == ExternalSmartRouter$$Mapping.class) {
                synchronized (this.mRouteMapLock) {
                    newInstance.init(this.mExternalRouteMap);
                    newInstance.initInterceptorMap(this.mExternalRouteInterceptorMap);
                }
                MethodCollector.o(10301);
                return true;
            }
            MethodCollector.o(10301);
            return false;
        } catch (ClassNotFoundException e) {
            0HH.LIZ(e);
        } catch (InstantiationException e2) {
            0HH.LIZ(e2);
        } catch (IllegalAccessException e3) {
            0HH.LIZ(e3);
        } catch (Throwable th) {
            MethodCollector.o(10301);
            throw th;
        }
    }

    public List<String> getInterceptorList(String str, boolean z) {
        if (z) {
            if (this.mExternalRouteInterceptorMap == null) {
                return null;
            }
            return this.mExternalRouteInterceptorMap.get(Util.getRealRouteUrl(str));
        } else if (this.mRouteInterceptorMap == null) {
            return null;
        } else {
            return this.mRouteInterceptorMap.get(Util.getRealRouteUrl(str));
        }
    }

    private String getTargetClassFromUri(String str, Map<String, String> map) {
        String realRouteUrl = Util.getRealRouteUrl(str);
        String str2 = map.get(realRouteUrl);
        if (TextUtils.isEmpty(str2) && realRouteUrl.endsWith("/") && realRouteUrl.length() > 1) {
            realRouteUrl = realRouteUrl.substring(0, realRouteUrl.length() - 1);
            str2 = map.get(realRouteUrl);
        }
        if (TextUtils.isEmpty(str2)) {
            realRouteUrl = Util.getRouteUrl(str);
            str2 = map.get(realRouteUrl);
        }
        if (!TextUtils.isEmpty(str2) || !realRouteUrl.endsWith("/") || realRouteUrl.length() <= 1) {
            return str2;
        }
        return map.get(realRouteUrl.substring(0, realRouteUrl.length() - 1));
    }

    public String getTargetClass(String str, boolean z) {
        String targetClassFromUri;
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        if (z) {
            targetClassFromUri = getTargetClassFromUri(str, this.mExternalRouteMap);
        } else {
            targetClassFromUri = getTargetClassFromUri(str, this.mRouteMap);
        }
        if (TextUtils.isEmpty(targetClassFromUri) && !this.mIsInited.get()) {
            initRouteMap();
            if (z) {
                targetClassFromUri = getTargetClassFromUri(str, this.mExternalRouteMap);
            } else {
                targetClassFromUri = getTargetClassFromUri(str, this.mRouteMap);
            }
        }
        Logger.d("RouteMapper#getTargetClass url: " + str + "  |  targetClass: " + targetClassFromUri + "  |  isExternal: " + z);
        return targetClassFromUri;
    }

    public void init(Context context, ServerParam serverParam, IConfigUpdateListener iConfigUpdateListener) {
        Logger.d("RouteMapper#init RouteMapper");
        this.mContext = context;
        this.mConfigUpdateListener = iConfigUpdateListener;
        if (!this.mIsInited.get()) {
            initRouteMap();
        }
        Logger.d(0HH.LIZ("RouteMapper#loadMappingByRouter routes: %d .", new Object[]{Integer.valueOf(this.mRouteMap.size())}));
        Logger.d(0HH.LIZ("RouteMapper#loadMappingByRouter routes: %d .", new Object[]{Integer.valueOf(this.mExternalRouteMap.size())}));
        if (serverParam != null) {
            initDynamicRoutes(serverParam);
        }
    }
}
