package com.bytedance.sdk.openadsdk.core.widget.fm;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import androidx.annotation.MainThread;
import com.bytedance.sdk.component.utils.mq;
import com.bytedance.sdk.component.utils.wey;
import com.bytedance.sdk.openadsdk.core.fhx;
import com.bytedance.sdk.openadsdk.core.gof;
import com.bytedance.sdk.openadsdk.core.model.lse;
import com.bytedance.sdk.openadsdk.irt.fm.fm;
import com.bytedance.sdk.openadsdk.utils.zan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes2.dex */
public class wsy implements mq.fm {
    float dsz;
    float duv;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    String f7339ef;
    float fhx;
    Context fm;
    boolean gof;
    long gqi;
    long irt;
    float ku;
    private final boolean mq;
    private long ol;
    float onz;
    boolean qhl;
    lse ro;
    int vt;
    private boolean wbw;
    boolean wsy;
    boolean wu;
    WebView yz;
    private final Handler nt = new mq(fhx.ro().getLooper(), this);

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    String f7340lb = "landingpage";
    int ajl = 0;
    private final String gzf = ".*\\/serp\\?sc=.*&clkt=\\d+$";
    private final String ai = ".*\\/\\?caf_results=.*&clkt=\\d+$";
    fm hlt = new fm() { // from class: com.bytedance.sdk.openadsdk.core.widget.fm.wsy.1
        @Override // com.bytedance.sdk.openadsdk.core.widget.fm.wsy.fm
        public void fm() {
            wsy wsyVar = wsy.this;
            wsyVar.wu = true;
            wsyVar.ajl();
            wsy wsyVar2 = wsy.this;
            wsyVar2.fm(2, wsyVar2.f7339ef, wsyVar2.vt);
        }
    };
    GestureDetector hi = new GestureDetector(gof.fm(), new GestureDetector.SimpleOnGestureListener() { // from class: com.bytedance.sdk.openadsdk.core.widget.fm.wsy.2
        @Override // android.view.GestureDetector.SimpleOnGestureListener, android.view.GestureDetector.OnGestureListener
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f10, float f11) {
            wsy.this.qhl = true;
            return false;
        }
    });
    int jnr = gof.yz().wey();
    Map<Integer, Long> wey = new HashMap();
    Map<Integer, Float> sds = new HashMap();
    Map<Integer, Long> maa = new HashMap();
    List<Integer> lse = new ArrayList();

    public interface fm {
        void fm();
    }

    public wsy(WebView webView, lse lseVar, Context context, boolean z10) {
        this.ro = lseVar;
        this.yz = webView;
        this.fm = context;
        this.mq = z10;
    }

    /* JADX INFO: Access modifiers changed from: private */
    @MainThread
    public void ajl() {
        if (this.sds.get(Integer.valueOf(this.vt)) != null) {
            return;
        }
        float height = this.yz.getHeight() / zan.ro(this.fm, this.yz.getContentHeight());
        if (height < 0.0f || height > 1.0f) {
            height = 0.0f;
        }
        this.sds.put(Integer.valueOf(this.vt), Float.valueOf(height));
    }

    private boolean jnr() {
        try {
            int i10 = this.vt;
            if (i10 != 2 && i10 != 3) {
                return false;
            }
            if (Pattern.matches(".*\\/serp\\?sc=.*&clkt=\\d+$", this.f7339ef)) {
                return true;
            }
            return Pattern.matches(".*\\/\\?caf_results=.*&clkt=\\d+$", this.f7339ef);
        } catch (Throwable th) {
            wey.lb("WebArbitrageBehavior", th.toString());
            return false;
        }
    }

    private boolean wsy() {
        int i10 = this.ajl + 1;
        this.ajl = i10;
        if (i10 > this.jnr) {
            return true;
        }
        return ("landingpage".equals(this.f7340lb) || "landingpage_endcard".equals(this.f7340lb) || "landingpage_split_screen".equals(this.f7340lb) || "landingpage_direct".equals(this.f7340lb) || "landingpage_split_ceiling".equals(this.f7340lb)) ? false : true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void wu() {
        WebBackForwardList webBackForwardListCopyBackForwardList = this.yz.copyBackForwardList();
        if (webBackForwardListCopyBackForwardList != null) {
            int currentIndex = webBackForwardListCopyBackForwardList.getCurrentIndex();
            this.vt = currentIndex + 1;
            if (this.mq) {
                this.vt = currentIndex + 2;
            }
        }
    }

    private void yz(String str) {
        if (wsy()) {
            return;
        }
        com.bytedance.sdk.openadsdk.yz.lb.fm(this.ro, new fm.C0096fm().fm(this.f7339ef).ro(Uri.decode(str)).fm(), this.f7340lb);
    }

    public void fm(String str) {
        this.f7340lb = str;
    }

    public fm lb() {
        return this.hlt;
    }

    public void ro(String str) {
        this.f7339ef = str;
        wu();
        this.wey.put(Integer.valueOf(this.vt), Long.valueOf(SystemClock.elapsedRealtime()));
        this.maa.put(Integer.valueOf(this.vt), Long.valueOf(SystemClock.elapsedRealtime()));
        this.wbw = jnr();
    }

    public void lb(String str) {
        if (this.wsy) {
            this.gof = true;
        }
        if (this.vt == 1 && !TextUtils.isEmpty(str) && str.contains("query=")) {
            int iIndexOf = str.indexOf("query=") + 6;
            int iIndexOf2 = str.indexOf("&", iIndexOf);
            if (iIndexOf < 0 || iIndexOf2 >= str.length() || iIndexOf2 <= iIndexOf) {
                return;
            }
            yz(str.substring(iIndexOf, iIndexOf2));
        }
    }

    public void fm() {
        ro(this.vt);
    }

    public void fm(int i10) {
        float height = (this.yz.getHeight() + i10) / zan.ro(this.fm, this.yz.getContentHeight());
        Float f10 = this.sds.get(Integer.valueOf(this.vt));
        if (height > (f10 == null ? 0.0f : f10.floatValue())) {
            this.sds.put(Integer.valueOf(this.vt), Float.valueOf(height));
        }
    }

    public void ro() {
        this.yz.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() { // from class: com.bytedance.sdk.openadsdk.core.widget.fm.wsy.3
            @Override // android.view.ViewTreeObserver.OnWindowFocusChangeListener
            public void onWindowFocusChanged(boolean z10) {
                if (!z10) {
                    wsy wsyVar = wsy.this;
                    if (!wsyVar.wu) {
                        wsyVar.ajl();
                        long jElapsedRealtime = SystemClock.elapsedRealtime();
                        if (jElapsedRealtime - wsy.this.ol >= 50) {
                            wsy wsyVar2 = wsy.this;
                            wsyVar2.fm(3, wsyVar2.f7339ef, wsyVar2.vt);
                            wsy.this.ol = jElapsedRealtime;
                            return;
                        }
                        return;
                    }
                    wsyVar.wu = false;
                }
                if (z10) {
                    wsy.this.wu();
                    wsy wsyVar3 = wsy.this;
                    wsyVar3.wey.put(Integer.valueOf(wsyVar3.vt), Long.valueOf(SystemClock.elapsedRealtime()));
                }
            }
        });
    }

    private void ro(int i10) {
        if (wsy() || this.lse.contains(Integer.valueOf(i10))) {
            return;
        }
        this.lse.add(Integer.valueOf(i10));
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        com.bytedance.sdk.openadsdk.yz.lb.ro(this.ro, new fm.C0096fm().fm(this.f7339ef).fm(this.vt).vt(jElapsedRealtime - (this.maa.get(Integer.valueOf(i10)) != null ? r6.longValue() : jElapsedRealtime)).fm(), this.f7340lb);
    }

    private void yz() {
        if (wsy()) {
            return;
        }
        com.bytedance.sdk.openadsdk.irt.fm.fm fmVarFm = new fm.C0096fm().fm(this.f7339ef).fm(this.vt).yz(this.ku).jnr(this.duv).ajl(this.fhx).wsy(this.gqi).fm();
        Message messageObtain = Message.obtain();
        messageObtain.what = 100;
        messageObtain.obj = fmVarFm;
        this.nt.sendMessageDelayed(messageObtain, 20L);
    }

    private void lb(int i10) {
        if (wsy()) {
            return;
        }
        ajl();
        this.wsy = true;
        com.bytedance.sdk.openadsdk.irt.fm.fm fmVarFm = new fm.C0096fm().fm(this.f7339ef).fm(this.vt).fm(this.ku).ro(this.duv).lb(this.gqi).lb(i10).fm();
        try {
            if (this.wbw) {
                WebView.HitTestResult hitTestResult = this.yz.getHitTestResult();
                fmVarFm.fm(hitTestResult.getExtra());
                fmVarFm.fm(hitTestResult.getType());
            }
        } catch (Throwable unused) {
        }
        Message messageObtain = Message.obtain();
        messageObtain.what = 200;
        messageObtain.obj = fmVarFm;
        this.nt.sendMessageDelayed(messageObtain, 100L);
    }

    public void fm(MotionEvent motionEvent) {
        this.hi.onTouchEvent(motionEvent);
        int action = motionEvent.getAction();
        if (action == 0) {
            this.ku = motionEvent.getX();
            this.duv = motionEvent.getY();
            this.irt = SystemClock.elapsedRealtime();
        } else if (action != 1) {
            if (action != 3) {
                return;
            }
            lb(2);
        } else {
            this.gqi = SystemClock.elapsedRealtime() - this.irt;
            if (ro(motionEvent)) {
                yz();
            } else {
                lb(1);
            }
        }
    }

    private boolean ro(MotionEvent motionEvent) {
        this.dsz = motionEvent.getX();
        float y10 = motionEvent.getY();
        this.onz = y10;
        float f10 = this.duv;
        if (y10 - f10 == 0.0f) {
            return false;
        }
        this.fhx = y10 - f10;
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(int i10, String str, int i11) {
        if (wsy()) {
            return;
        }
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        Long l10 = this.wey.get(Integer.valueOf(i11));
        long jLongValue = l10 != null ? l10.longValue() : jElapsedRealtime;
        Float f10 = this.sds.get(Integer.valueOf(i11));
        com.bytedance.sdk.openadsdk.yz.lb.lb(this.ro, new fm.C0096fm().fm(str).fm(i11).wu(jElapsedRealtime - jLongValue).ef(f10 == null ? 0.0f : f10.floatValue()).ro(i10).fm(), this.f7340lb);
    }

    @Override // com.bytedance.sdk.component.utils.mq.fm
    public void fm(Message message) {
        int i10 = message.what;
        com.bytedance.sdk.openadsdk.irt.fm.fm fmVar = (com.bytedance.sdk.openadsdk.irt.fm.fm) message.obj;
        if (i10 == 100) {
            fmVar.yz(this.qhl ? 2 : 1);
            com.bytedance.sdk.openadsdk.yz.lb.yz(this.ro, fmVar, this.f7340lb);
            this.qhl = false;
        } else if (i10 == 200) {
            if (this.gof) {
                fm(1, fmVar.lb(), fmVar.yz());
            }
            fmVar.lb(this.gof ? 1 : 0);
            com.bytedance.sdk.openadsdk.yz.lb.jnr(this.ro, fmVar, this.f7340lb);
            this.wsy = false;
            this.gof = false;
        }
    }
}
