/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.focus.observer;

import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.util.Log;

import org.mozilla.focus.architecture.NonNullObserver;
import org.mozilla.focus.session.Session;
import org.mozilla.focus.telemetry.TelemetryWrapper;

public class AverageLoadTimeObserver extends NonNullObserver<Boolean> {

    private static final String LOG_TAG = "AverageLoadTimeObserver";
    private long startLoadTime = 0;
    private boolean loadStarted = false;

    private Session session;

    public AverageLoadTimeObserver(@NonNull Session session) {
        this.session = session;
    }

    @Override
    protected void onValueChanged(Boolean loading) {
        if (loading) {
            if (!loadStarted) {
                startLoadTime = SystemClock.elapsedRealtime();
                Log.i(LOG_TAG, "zerdatime " + startLoadTime +
                        " - page load start");
                loadStarted = true;
            }
        } else {
            if (loadStarted) {
                Log.i(LOG_TAG, "Loaded page at " + session.getUrl().getValue());
                long endTime = SystemClock.elapsedRealtime();
                Log.i(LOG_TAG, "zerdatime " + endTime +
                        " - page load stop");
                Log.i(LOG_TAG, (endTime - startLoadTime) + " - elapsed load");
                TelemetryWrapper.addLoadToAverage(endTime - startLoadTime);
                loadStarted = false;
            }
        }
    }
}

@@ -336,16 +336,54 @@
 l.Log;%0A%0A
+import org.mozilla.focus.BuildConfig;%0A
 import o
@@ -429,16 +429,16 @@
 server;%0A
-
 import o
@@ -1206,16 +1206,260 @@
  = true;
+%0A%0A                // Add Nimbledroid start bookend for loading website%0A                Log.i(%22NimbleDroidV1%22, %22Scenario.begin %22 +%0A                        BuildConfig.FLAVOR + %22_%22 +%0A                        session.getUrl().getValue() + %22_load%22);
 %0A       
@@ -1913,16 +1913,16 @@
 dTime);%0A
-
         
@@ -1949,16 +1949,258 @@
 = false;
+%0A%0A                // Add Nimbledroid start bookend for loading website%0A                Log.i(%22NimbleDroidV1%22, %22Scenario.end %22 +%0A                        BuildConfig.FLAVOR + %22_%22 +%0A                        session.getUrl().getValue() + %22_load%22);
 %0A       

Add nimbledroid bookend for website loading