/*
File Name: WindowManager.h
Created By: Andy Odle
Date: Oct 13, 2009

Description: 
    
    
*/

#ifndef WindowManager_h
#define WindowManager_h

#include <ctime>

// Xlib Headers.
#include <X11/Xlib.h>

// Opengl Headers.
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "./source/PlatformSpecific/inc/ErrorReport.h"
#include "./source/PlatformSpecific/inc/DeviceInput.h"
#include "./source/MVC/Controller.h"
#include "./source/inc/TimeGOC.h"
#include "./source/inc/InputEngine.h"

class WindowManager : public Controller
{
    
    public:

        static WindowManager *winManager_i();
        void CreateWindow();
        void ApplicationLoop();
    
        void AtachView(View *view_p);
        void DetachView(View *view_p);
        void NotifyViews(Model *model_p);

        int GetWidth(){return width_m;}
        int GetHeight(){return height_m;}
        bool GetActive(){return windowIsActive_m;}
    
    private:

        WindowManager();
        ~WindowManager();
        WindowManager(WindowManager const &windowManager_p){};
        WindowManager &operator=(WindowManager const &windowManager_p){};
    
        bool InitializeOpenGL();
        void ResizeOpenGLViewPort(int width_p, int height_p);
        void CleanUpWindow();
        void MessagePump(TimeGOC *timeGOC_p, InputEngine *input_p);
    
        // Frames Per Second (FPS) Calculation funnctions.
        void StartFPSCalculation(time_t totalElapsedSeconds_p);
        void EndFPSCalculation();
    
    private:

        // X11 Window Variables
        int argc_m;
        char **argv_m;
        Display *display_m;
        Window window_m;
        Colormap colorMap_m;
        XEvent xEvent_m;
        XSetWindowAttributes xSetWindowAttributes_m;
        XVisualInfo *xVisualInfo_m;
    
        int errorBase_m;
        int eventBase_m;
        int screen_m;
        GC graphicsContext_m;
        string windowTitle_m;
    
        // X11 Window Messages
        Atom wmDeleteMessage_m;
    
        // Opengl
        GLXContext glxContext_m;
    
        // Time variables.
        TimeGOC *timeGOC_m;
    
        // GOC
        bool done_m;
        bool fullScreen_m;
        int width_m;
        int height_m;
        static WindowManager *windowManager_m;
    
        ErrorReport *errorReport_m;

        // FPS variables.
        bool enableFPS_m;
        float fps_m;
        int frame_m;
        int lastTotalElapsedSeconds_m;
        time_t totalElapsedSeconds_m;

        // Game loop consistancy variables.
        int gameTicksPerSec_m;
        int gameTicksToSkip_m;
        int gameMaxFramesToSkip_m;
        unsigned int gameNextTick_m;
        int slowFrameCount_m;

        // Keyboard variables.
        bool windowIsActive_m;
        Pressed pressedKeyOrButton_m;
        InputEngine *inputEngine_m;
};

#endif
