// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef toolx_Windows_session
#define toolx_Windows_session

#include <windows.h>
#include <windowsx.h>

#include <ostream>
#include <cstdlib>

namespace toolx {
namespace Windows {

class session {
public:
  session(std::ostream& a_out):m_out(a_out){}
  virtual ~session() {}
protected:
  session(const session& a_from):m_out(a_from.m_out){}
  session& operator=(const session&){return *this;}
public:
  std::ostream& out() const {return m_out;}

  bool is_valid() const {return true;}

  bool steer() {
    while(true) {
      MSG event;
      BOOL status = ::GetMessage(&event,NULL,0,0);
      if(status == -1) { // This may happen (dixit Microsoft doc).
        break;
      } else if(status == 0) { //WM_QUIT
        break;
      } else {
        ::TranslateMessage(&event);
        ::DispatchMessage (&event);
      }
    }
    return true;
  }

  bool sync() {
    MSG event;
    while(::PeekMessage(&event,NULL,0,0,PM_REMOVE)) {
      if(event.message==WM_QUIT) return false;
      ::TranslateMessage(&event);
      ::DispatchMessage(&event);
    }
    return true;
  }

  void show_window(HWND a_hwnd){
    ::SetForegroundWindow(a_hwnd);
    ::ShowWindow(a_hwnd,SW_SHOWDEFAULT);
    ::UpdateWindow(a_hwnd);
    ::DrawMenuBar(a_hwnd);
  }

  void set_size(HWND a_hwnd,unsigned int a_width,unsigned int a_height) {
    ::MoveWindow(a_hwnd,0,0,a_width,a_height,TRUE);
  }

  //void post(HWND a_hwnd,LPARAM a_msg){
  //  ::PostMessage(a_hwnd,WM_USER,(WPARAM)0,a_msg);
  //}
public:
  bool tmp_dir(std::string& a_dir) {
    char* env = ::getenv("TEMP");
    if(!env) {a_dir.clear();return false;}
    a_dir = env;
    return true;
  }
protected:
  std::ostream& m_out;
};

}}


#endif
