/*
    Robot Interface Remote Client
    (C) 2006 Jason Hunt
    nulluser@gmail.com
    
    file: main.cpp
*/

#include <windows.h>
#include <stdio.h>

#include "main.h"
#include "window.h"
#include "display.h"
#include "network.h"
#include "input.h"
#include "image.h"
#include "robot.h"
#include "config.h"
#include "timer.h"
#include "joystick.h"


HWND system_hwnd = NULL;                        // Keep track of application handle

unsigned int frames = 0;                        // Numbers of frames so far in this second
unsigned int fps = 0;                           // Frames per second


/* Runs when program startsis created */
void system_startup()
{
    load_config("config.txt");

    joystick_start();
    robot_start();

    network_start();
 
    SetTimer(system_hwnd, ID_SECOND, 1000, NULL);
    SetTimer(system_hwnd, ID_UPDATE, 10, NULL);  // Update robot 4 times a second
}
/* End of system_startup */


/* Runs when window is closed */
void system_shutdown(void)
{
   // robot_reset();

    image_stop();
    
    network_stop();
    
    WSACleanup();

    PostQuitMessage(0);   
}
/* End of system_shutdown */



/* Deal with the system timer */
void timer_message(HWND hwnd, WPARAM wParam)
{
    if (wParam == ID_SECOND)
    {
        fps = frames;
        frames = 0;
      
        network_check();            // Make sure network is still conneted
                                          
        return;
    } 
    
    if (wParam == ID_UPDATE)
    {
        request_sensor_data();
        joystick_check();
        check_input();
        robot_update();
        update_display(hwnd);
        InvalidateRect(hwnd, NULL, 0);  
        return;
    }    
}   
/* End of timer message */


/* Ehe message handler */
LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                 
    {
        case WM_CREATE:     system_hwnd = hwnd;;    break;
        case WM_TIMER:      timer_message(hwnd, wParam); break;
        case WM_DESTROY:    system_shutdown(); break;
        case WM_KEYDOWN:    key_down(wParam, lParam); break;
        case WM_KEYUP:      key_up(wParam); break;

        case WM_PAINT:      do_paint(hwnd);  break;


        case WM_KILLFOCUS: input_lose_focus(); break;


        case WM_MOUSEMOVE:  process_mouse(lParam); break;
   
        case WM_SIZE:
            display_buffer_resize(hwnd, lParam);
            return(0);
            
        case WM_NETEVENT: network_message(wParam, lParam); break;            
            
        default: return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return(0);
}
/* End of winproc */


/* Winmain */
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // Make the main window
    if (setup_window(hinstance, nCmdShow)) return(1);

    system_startup();

    MSG messages;

    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return(messages.wParam);
}
/* End of winmain */



