package org.wikipedia.analytics;

import androidx.annotation.NonNull;

import org.json.JSONObject;
import org.wikipedia.WikipediaApp;
import org.wikipedia.dataclient.WikiSite;

import java.util.concurrent.TimeUnit;

public class ToCInteractionFunnel extends Funnel {
    private static final String SCHEMA_NAME = "MobileWikiAppToCInteraction";
    private static final int REV_ID = 19044853;

    private final int pageId;
    private final int numSections;

    private int numPeeks;
    private int numOpens;
    private int numSectionClicks;

    private long lastPeekMillis;
    private int totalPeekSec;

    private long lastScrollStartMillis;
    private int totalOpenedSec;

    public ToCInteractionFunnel(WikipediaApp app, WikiSite wiki, int pageId, int numSections) {
        super(app, SCHEMA_NAME, REV_ID, Funnel.SAMPLE_LOG_ALL, wiki);
        this.pageId = pageId;
        this.numSections = numSections;
    }

    @Override
    protected JSONObject preprocessData(@NonNull JSONObject eventData) {
        preprocessData(eventData, "page_id", pageId);
        preprocessData(eventData, "num_sections", numSections);
        return super.preprocessData(eventData);
    }

    @Override protected void preprocessSessionToken(@NonNull JSONObject eventData) { }

    public void peek() {
        numPeeks++;
        lastPeekMillis = System.currentTimeMillis();
    }

    public void hide() {
        if (lastPeekMillis == 0) {
            return;
        }
        totalPeekSec += (System.currentTimeMillis() - lastPeekMillis) / TimeUnit.SECONDS.toMillis(1);
        lastPeekMillis = 0;
    }

    public void scrollStart() {
        numOpens++;
        lastScrollStartMillis = System.currentTimeMillis();
    }

    public void scrollStop() {
        if (lastScrollStartMillis == 0) {
            return;
        }
        totalOpenedSec += (System.currentTimeMillis() - lastScrollStartMillis) / TimeUnit.SECONDS.toMillis(1);
        lastScrollStartMillis = 0;
    }


    public void logClick() {
        numSectionClicks++;
    }

    public void log() {
        hide();
        scrollStop();
        if (numSections == 0 || totalPeekSec == 0) {
            return;
        }
        log(
                "num_peeks", numPeeks,
                "num_opens", numOpens,
                "num_section_clicks", numSectionClicks,
                "total_peek_sec", totalPeekSec,
                "total_open_sec", totalOpenedSec
        );
    }
}

@@ -456,34 +456,8 @@
 s;%0A%0A
-    private int numPeeks;%0A
     
@@ -474,24 +474,24 @@
 t numOpens;%0A
+
     private 
@@ -1245,113 +1245,8 @@
  %7D%0A%0A
-    public void peek() %7B%0A        numPeeks++;%0A        lastPeekMillis = System.currentTimeMillis();%0A    %7D%0A%0A
     
@@ -2099,16 +2099,9 @@
 s%22, 
-numPeeks
+0
 ,%0A  

Remove funnel peek() method