package com.bytedance.router;

import android.content.Context;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.router.RouteIntent;
import com.bytedance.router.interceptor.IInterceptor;
import com.bytedance.router.interceptor.IInterceptorProvider;
import com.bytedance.router.interceptor.RewriteManager;
import com.bytedance.router.mapping.SmartRouterInterceptor;
import com.bytedance.router.monitor.RouterMonitor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class InterceptManager {
    public IInterceptorProvider interceptorProvider;
    public Map<String, IInterceptor> mAssignInterceptors = new HashMap();
    public volatile boolean mInterceptorProviderIsInited;
    public List<IInterceptor> mInterceptors = new LinkedList();
    public AtomicBoolean mIsInited = new AtomicBoolean(false);
    public List<IInterceptor> mLastInterceptors = new LinkedList();
    public RewriteManager mRewriteManager;
    public SmartRouterInterceptor mSmartRouterInterceptor;

    static {
        Covode.recordClassIndex(37142);
    }

    public RewriteManager getRewriteManager() {
        return this.mRewriteManager;
    }

    public InterceptManager() {
        if (this.mRewriteManager == null) {
            this.mRewriteManager = new RewriteManager();
        }
        this.mInterceptors.add(this.mRewriteManager);
        this.mSmartRouterInterceptor = new SmartRouterInterceptor();
    }

    public void initInterceptors() {
        if (!this.mIsInited.get()) {
            ArrayList arrayList = new ArrayList();
            this.mSmartRouterInterceptor.initInterceptors(arrayList);
            this.mInterceptors.addAll(arrayList);
            HashMap hashMap = new HashMap();
            this.mSmartRouterInterceptor.initAssignInterceptors(hashMap);
            this.mAssignInterceptors.putAll(hashMap);
        }
        this.mIsInited.set(true);
    }

    public void addInterceptorProvider(IInterceptorProvider iInterceptorProvider) {
        if (iInterceptorProvider != null) {
            this.interceptorProvider = iInterceptorProvider;
        }
    }

    public void addInterceptor(IInterceptor iInterceptor) {
        MethodCollector.i(8092);
        if (iInterceptor == null) {
            MethodCollector.o(8092);
            return;
        }
        synchronized (this) {
            try {
                this.mInterceptors.add(iInterceptor);
            } finally {
                MethodCollector.o(8092);
            }
        }
    }

    public void addLastInterceptor(IInterceptor iInterceptor) {
        MethodCollector.i(8302);
        if (iInterceptor == null) {
            MethodCollector.o(8302);
            return;
        }
        synchronized (this) {
            try {
                this.mLastInterceptors.add(iInterceptor);
            } finally {
                MethodCollector.o(8302);
            }
        }
    }

    public boolean whetherIntercept(String str) {
        MethodCollector.i(4518);
        if (TextUtils.isEmpty(str)) {
            MethodCollector.o(4518);
            return false;
        }
        synchronized (this) {
            try {
                if (!this.mInterceptorProviderIsInited) {
                    IInterceptorProvider iInterceptorProvider = this.interceptorProvider;
                    if (iInterceptorProvider != null) {
                        this.mInterceptors.addAll(iInterceptorProvider.getInterceptors());
                    }
                    this.mInterceptorProviderIsInited = true;
                }
                RouteIntent build = new RouteIntent.Builder(str).build();
                for (IInterceptor matchInterceptRules : this.mInterceptors) {
                    if (matchInterceptRules.matchInterceptRules(build)) {
                        return true;
                    }
                }
                for (IInterceptor matchInterceptRules2 : this.mLastInterceptors) {
                    if (matchInterceptRules2.matchInterceptRules(build)) {
                        MethodCollector.o(4518);
                        return true;
                    }
                }
                MethodCollector.o(4518);
                return false;
            } finally {
                MethodCollector.o(4518);
            }
        }
    }

    public void addAssignInterceptor(String str, IInterceptor iInterceptor) {
        MethodCollector.i(8304);
        if (TextUtils.isEmpty(str) || iInterceptor == null) {
            MethodCollector.o(8304);
            return;
        }
        synchronized (this) {
            try {
                this.mAssignInterceptors.put(str, iInterceptor);
            } finally {
                MethodCollector.o(8304);
            }
        }
    }

    public boolean processRouteIntent(Context context, RouteIntent routeIntent) {
        boolean z;
        boolean z2;
        IInterceptor next;
        MethodCollector.i(4360);
        if (routeIntent == null) {
            MethodCollector.o(4360);
            return true;
        }
        synchronized (this) {
            try {
                if (!this.mInterceptorProviderIsInited) {
                    IInterceptorProvider iInterceptorProvider = this.interceptorProvider;
                    if (iInterceptorProvider != null) {
                        this.mInterceptors.addAll(iInterceptorProvider.getInterceptors());
                    }
                    this.mInterceptorProviderIsInited = true;
                }
                Iterator<IInterceptor> it = this.mInterceptors.iterator();
                do {
                    z = false;
                    if (it.hasNext()) {
                        next = it.next();
                        RouterMonitor.INSTANCE.startMeasureDuration(routeIntent.getRouteId(), next.getClass().getSimpleName());
                        if (next.matchInterceptRules(routeIntent)) {
                            z = next.onInterceptRoute(context, routeIntent);
                            RouterMonitor.INSTANCE.reportIfUrlChanged(routeIntent.getRouteId(), routeIntent.getUrl(), next.getClass().getSimpleName());
                        }
                        RouterMonitor.INSTANCE.stopMeasureDuration(routeIntent.getRouteId(), next.getClass().getSimpleName());
                    } else {
                        for (IInterceptor next2 : this.mLastInterceptors) {
                            RouterMonitor.INSTANCE.startMeasureDuration(routeIntent.getRouteId(), next2.getClass().getSimpleName());
                            if (next2.matchInterceptRules(routeIntent)) {
                                z2 = next2.onInterceptRoute(context, routeIntent);
                                RouterMonitor.INSTANCE.reportIfUrlChanged(routeIntent.getRouteId(), routeIntent.getUrl(), next2.getClass().getSimpleName());
                            } else {
                                z2 = false;
                            }
                            RouterMonitor.INSTANCE.stopMeasureDuration(routeIntent.getRouteId(), next2.getClass().getSimpleName());
                            if (z2) {
                                RouterMonitor.INSTANCE.reportIntercepted(routeIntent.getRouteId(), next2.getClass().getSimpleName());
                                MethodCollector.o(4360);
                                return true;
                            }
                        }
                        MethodCollector.o(4360);
                        return false;
                    }
                } while (!z);
                RouterMonitor.INSTANCE.reportIntercepted(routeIntent.getRouteId(), next.getClass().getSimpleName());
                return true;
            } finally {
                MethodCollector.o(4360);
            }
        }
    }

    public boolean processAssignInterceptor(Context context, List<String> list, RouteIntent routeIntent) {
        MethodCollector.i(4441);
        if (routeIntent == null) {
            MethodCollector.o(4441);
            return true;
        }
        synchronized (this) {
            try {
                for (String str : list) {
                    IInterceptor iInterceptor = this.mAssignInterceptors.get(str);
                    if (iInterceptor != null) {
                        RouterMonitor.INSTANCE.startMeasureDuration(routeIntent.getRouteId(), iInterceptor.getClass().getSimpleName());
                        if (iInterceptor.onInterceptRoute(context, routeIntent)) {
                            RouterMonitor.INSTANCE.stopMeasureDuration(routeIntent.getRouteId(), iInterceptor.getClass().getSimpleName());
                            RouterMonitor.INSTANCE.reportIntercepted(routeIntent.getRouteId(), iInterceptor.getClass().getSimpleName());
                            return true;
                        }
                        RouterMonitor.INSTANCE.stopMeasureDuration(routeIntent.getRouteId(), iInterceptor.getClass().getSimpleName());
                    }
                }
                MethodCollector.o(4441);
                return false;
            } finally {
                MethodCollector.o(4441);
            }
        }
    }
}
