package de.frajul.particlelab.main;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import de.frajul.particlelab.entities.particles.ParticleReader;
import de.frajul.particlelab.entities.particles.ParticleSystem;
import de.frajul.particlelab.rendering.GameRenderer;
import de.frajul.particlelab.rendering.Rendering;

/**
 * Created by Julian on 02.08.2016.
 */
public class MainActivity extends Activity {

    private MyGlSurfaceView glSurfaceView;
    private ParticleSystem particleSystem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            GameLog.d("OnCreate");
            super.onCreate(savedInstanceState);
            super.requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            if (!hasGLES20())
                throw new Exception("OpenGL ES 2.0 not supported");

            particleSystem = new ParticleSystem(this);
            Rendering rendering = new Rendering(this, particleSystem);
            glSurfaceView = new MyGlSurfaceView(this, new GameRenderer(this,rendering));
            super.setContentView(glSurfaceView);
        } catch (Exception e) {
            onException(e);
        }
    }

    public void onException(Exception e) {
        GameLog.e(e);
        super.finish();
    }

    @Override
    protected void onPause() {
        GameLog.d("OnPause");
        glSurfaceView.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        GameLog.d("OnResume");
        glSurfaceView.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        GameLog.d("OnDestroy");
        super.onDestroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    private boolean hasGLES20() {
        ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ConfigurationInfo info = am.getDeviceConfigurationInfo();
        return info.reqGlEsVersion >= 0x20000;
    }

}
