/*
 * Copyright (C) 2016-2019, Stichting Mapcode Foundation (http://www.mapcode.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mapcode;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.mapcode.processing.NoAppState;
import com.mapcode.processing.ProcessingApplet;
import com.mapcode.visualizer.*;
import com.tomtom.speedtools.guice.GuiceConfigurationModule;

import javax.annotation.Nonnull;


/**
 * The class App extends ProcessingApp, because it is a Processing applet. This implies the class can also be "run" from
 * within a browser. In that case, it should not live within a package, so there is no package declaration in this
 * file.
 * <p>
 * Note that this class is named 'App' and not, for example, 'Visualizer', because it needs to run as an applet, and the
 * name 'App' is hard-coded in the some places that allow for this...
 */
public class App extends ProcessingApplet<NoAppState> {

    // Target frame-rate.
    private static final int FRAMERATE = 8;

    // Injected areas.
    @Nonnull
    private final DisplayArea displayArea;
    @Nonnull
    private final MapArea mapArea;
    @Nonnull
    private final LogArea logArea;
    @Nonnull
    private final PlaybackArea playbackArea;

    /**
     * Constructor.
     */
    public App() {
        super(FRAMERATE, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT, NoAppState.DEFAULT);

        /**
         * Create a Guice injector.
         */
        final Injector injector = Guice.createInjector(
                new GuiceConfigurationModule(
                        "classpath:mapcode-visualizer.properties",
                        "classpath:mapcode-visualizer-secret.properties"),
                new VisualizerModule());

        displayArea = injector.getInstance(DisplayArea.class);
        mapArea = injector.getInstance(MapArea.class);
        logArea = injector.getInstance(LogArea.class);
        playbackArea = injector.getInstance(PlaybackArea.class);
    }

    /**
     * Main PApplet setup function, called before execution of the applet.
     */
    @Override
    public final void setup() {
        super.setup();

        // Setup windows areas.
        displayArea.setup();
        mapArea.setup();
        logArea.setup();
        playbackArea.setup();
    }
}
