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

Description: 
    
    
*/

#ifndef WindowManager_h
#define WindowManager_h

#define WIN32_LEAN_AND_MEAN
#define VC_LEANMEAN

#define _WIN32_WINNT 0x0501

#include "windows.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "GL/glext.h"

#include "./source/PlatformSpecific/inc/ErrorReport.h"
#include "./source/PlatformSpecific/inc/DeviceInput.h"
#include "./source/PlatformSpecific/inc/GeneralPurposeFunctions.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 CreateWindowWin();
    void ApplicationLoop();
    void SetWindowInstance(HINSTANCE &hinstance_p);
        
    int GetWidth(){return width_m;}
    int GetHeight(){return height_m;}
    bool GetActive(){return windowIsActive_m;}
    HDC GetOpenGLDeviceContext(){return glDeviceContext_m;}
     
    INT_PTR WindowMessages(HWND &wnd, UINT &msg, WPARAM &wparam, LPARAM &lparam);

    // Required controller interface.
    void AtachView(View *view_p);
    void DetachView(View *view_p);
    void NotifyViews(Model *model_p);

private:

    WindowManager();
    ~WindowManager();
    WindowManager(WindowManager const&){}
    WindowManager &operator=(WindowManager const&){}
    
    void InitializeOpenGL();
    void ResizeOpenGLViewPort(int width_p, int height_p);
    void Render();
    
    void StartFPSCalculation(time_t totalElapsedSeconds_p);
    void EndFPSCalculation();
    
private:

    // WinXP Window variables.
    string windowTitle_m;
    HWND currentWindow_m;
    MSG messageHandler_m;
    HINSTANCE hinstance_m;
    RECT windowRectangle_m;
    WNDCLASS WindowStructure_m;
    bool windowIsActive_m;
    
    // OpenGL variables.
    HGLRC glRenderingContext_m;
    HDC glDeviceContext_m;
    PIXELFORMATDESCRIPTOR pixelFormatDescriptor_m;
    
    // GOC variables.
    int width_m;
    int height_m;
    bool fullScreen_m;
    static WindowManager * windowManager_m; // Singleton Storage.
    ErrorReport *errorReport_m;
    
    // Time variables.
    TimeGOC *timeGOC_m;
    
    // FPS variables.
    bool enableFPS_m;
    float fps_m;
    int frame_m;
    int lastTotalElapsedSeconds_m;
    int totalElapsedSeconds_m;

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

    // Keyboard variables.
    bool isActive_m;
    InputEngine *inputEngine_m;
};

LRESULT CALLBACK WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam);

#endif
