/*
 * Copyright (C) 2016 team-cachebox.de
 *
 * Licensed under the : GNU General Public License (GPL);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * 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 de.longri.cachebox3;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.scenes.scene2d.Stage;
import de.longri.cachebox3.gui.stages.Splash;
import de.longri.cachebox3.gui.stages.ViewManager;
import de.longri.cachebox3.settings.Config;
import org.slf4j.LoggerFactory;
import org.slf4j.impl.LibgdxLogger;

import static org.slf4j.impl.LibgdxLogger.DEFAULT_LOG_LEVEL_KEY;

public class CacheboxMain extends ApplicationAdapter {

    static {

        System.setProperty(DEFAULT_LOG_LEVEL_KEY, CB.USED_LOG_LEVEL);
        LibgdxLogger.init();

    }

    final static org.slf4j.Logger log = LoggerFactory.getLogger(CacheboxMain.class);

    Batch batch;
    protected int FpsInfoPos = 0;
    public static float stateTime;
    private Sprite FpsInfoSprite;
    private final Matrix4 NORMAL_MATRIX = new Matrix4().toNormalMatrix();

    @Override
    public void create() {

        Gdx.graphics.setContinuousRendering(false);
        Gdx.graphics.requestRendering();


        CB.mainStage = new Splash(new Splash.LoadReady() {
            @Override
            public void ready() {

                Config.AppRaterlaunchCount.setValue(Config.AppRaterlaunchCount.getValue() + 1);
                Config.AcceptChanges();


                // Splash is ready with initialisation
                // now switch Stage to ViewManager
                synchronized (CB.mainStage) {
                    Gdx.app.postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            CB.mainStage = new ViewManager();

                            // add mainStage to input prozessor
                            CB.inputMultiplexer.addProcessor(CB.mainStage);
                        }
                    });
                }
            }
        });

        CB.windowStage = new Stage();

    }

    @Override
    public void render() {
        stateTime += Gdx.graphics.getDeltaTime();
        Gdx.gl.glClearColor(CB.backgroundColor.r, CB.backgroundColor.g, CB.backgroundColor.b, CB.backgroundColor.a);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        synchronized (CB.mainStage) {
            CB.mainStage.act();
            CB.mainStage.draw();
        }

        if(CB.windowStage!=null&&CB.windowStage.getActors().size>0){
            CB.windowStage.act();
            CB.windowStage.draw();
        }


        if (CB.isTestVersion()) {

            float FpsInfoSize = CB.getScaledFloat(4f);
            if (FpsInfoSprite != null) {
                batch = CB.mainStage.getBatch();
                batch.begin();
                Color lastColor = batch.getColor();
                batch.setColor(1.0f, 0.0f, 0.0f, 1.0f);
                batch.draw(FpsInfoSprite, FpsInfoPos, 2, FpsInfoSize, FpsInfoSize);
                batch.setColor(lastColor);
                batch.end();
            } else {
                Sprite sprite = CB.getSprite("color");
                if (sprite != null) {
                    FpsInfoSprite = new Sprite(sprite);
                    FpsInfoSprite.setColor(1.0f, 0.0f, 0.0f, 1.0f);
                    FpsInfoSprite.setSize(FpsInfoSize, FpsInfoSize);
                }
            }

            FpsInfoPos += FpsInfoSize;
            if (FpsInfoPos > 60 * FpsInfoSize) {
                FpsInfoPos = 0;
            }
        }

    }

    @Override
    public void dispose() {
        batch.dispose();
    }

    @Override
    public void pause() {
        log.debug("on pause", "close databases");
        //close databases
//        if (Database.Data != null) Database.Data.Close();
//        if (Database.Settings != null) Database.Settings.Close();
//        if (Database.FieldNotes != null) Database.FieldNotes.Close();
    }

    @Override
    public void resume() {
//        log.debug("on resume", "reopen databases");
//        //open databases
//        if (Database.Data != null) Database.Data.Open();
//        if (Database.Settings != null) Database.Settings.Open();
//        if (Database.FieldNotes != null) Database.FieldNotes.Open();
    }
}

@@ -2903,32 +2903,34 @@
 lor.a);%0A        
+//
 Gdx.gl.glClear(G
@@ -2952,24 +2952,207 @@
 FFER_BIT);%0A%0A
+        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT %7C GL20.GL_DEPTH_BUFFER_BIT %7C (Gdx.graphics.getBufferFormat().coverageSampling ?%0A                GL20.GL_COVERAGE_BUFFER_BIT_NV : 0));%0A%0A
         sync
@@ -3263,16 +3263,17 @@
       if
+ 
 (CB.wind
@@ -3283,16 +3283,20 @@
 tage
+ 
 !=
+ 
 null
+ 
 &&
+ 
 CB.w
@@ -3326,11 +3326,14 @@
 size
-%3E
+ %3E 
 0)
+ 
 %7B%0A  

change glClear value