package org.wikipedia;

import org.json.JSONException;
import org.json.JSONObject;
import org.wikipedia.bridge.CommunicationBridge;
import org.wikipedia.bridge.StyleBundle;

public class NightModeHandler {
    private final CommunicationBridge bridge;
    private final WikipediaApp app;

    /**
     * @param bridge The bridge used to communicate with the WebView
     */
    public NightModeHandler(CommunicationBridge bridge) {
        this.bridge = bridge;
        this.app = WikipediaApp.getInstance();
    }

    /**
     * Returns the JSON used as payload for JS messages to turn night mode on/off
     *
     * @param hasPageLoaded Specify is the page has already been loaded or not.
     *                      If it has been loaded, inline style inversion is done by JS.
     *                      If not, it is handled by the transforms.
     * @return JSON used as payload for JS messages to turn night mode on or off
     */
    private JSONObject getPayload(boolean hasPageLoaded) {
        JSONObject payload = new JSONObject();
        try {
            payload.put("hasPageLoaded", hasPageLoaded);
            payload.put("nightStyleBundle",
                    StyleBundle.getAvailableBundle(StyleBundle.BUNDLE_NIGHT_MODE).toJSON());
        } catch (JSONException e) {
            // This shouldn't happen
            throw new RuntimeException(e);
        }
        return payload;
    }

    /**
     * Turn on Night Mode
     * @param hasPageLoaded Specify is the page has already been loaded or not.
     */
    public void turnOn(boolean hasPageLoaded) {
        bridge.sendMessage("toggleNightMode", getPayload(hasPageLoaded));
    }

    /**
     * Turn off Night Mode
     * @param hasPageLoaded Specify is the page has already been loaded or not.
     */
    public void turnOff(boolean hasPageLoaded) {
        bridge.sendMessage("toggleNightMode", getPayload(hasPageLoaded));
    }
}

@@ -248,44 +248,8 @@
 dge;
-%0A    private final WikipediaApp app;
 %0A%0A  
@@ -423,55 +423,8 @@
 ge;%0A
-        this.app = WikipediaApp.getInstance();%0A
     

remove unused field from NightModeHandler