// Processes messages for the main window.
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  NativeWindow* window = NativeWindow::GetWindow(hwnd);
  CefRefPtr<CefBrowser> browser;
  if (window) {
    browser = window->GetBrowser();
  }

  switch (message) {
    case WM_CREATE: {
      RECT rect;
      GetClientRect(hwnd, &rect);
      Cef::AddWebView(hwnd, rect, url_, browserSettings);
      return 0;
    }
    case WM_PAINT: {
      PAINTSTRUCT ps;
      HDC hdc;
      hdc = BeginPaint(hwnd, &ps);
      EndPaint(hwnd, &ps);
      return 0;
    }
    case WM_SETFOCUS:
      if (browser.get()) {
        PostMessage(browser->GetWindowHandle(), WM_SETFOCUS, wParam, NULL);
        return 0;
      }
    case WM_SIZE: {
      window->UpdatePosition();
      if (browser.get()) {
        RECT rect;
        GetClientRect(hwnd, &rect);
        HDWP hdwp = BeginDeferWindowPos(1);
        hdwp = DeferWindowPos(hdwp, browser->GetWindowHandle(), NULL,
                              rect.left, rect.top, rect.right - rect.left,
                              rect.bottom - rect.top, SWP_NOZORDER);
        EndDeferWindowPos(hdwp);
        if (emitFullscreen) {
          emitFullscreen = false;
          window->Emit("fullscreen");
        } else {
          window->Emit("resize", (int)LOWORD(lParam), (int)HIWORD(lParam));
        }
      }
      break;
    }
    case WM_WINDOWPOSCHANGING: {
      WINDOWPOS *position;
      position = (WINDOWPOS*)lParam;
      if (position->flags & SWP_STATECHANGED) {
        if (IsIconic(window->handle_)) {
          window->Emit("minimize");
        } else if (IsZoomed(window->handle_)) {
          window->Emit("maximize");
        } else {
          window->Emit("restore");
        }
      }
      break;
    }
    case WM_MOVE:
      window->UpdatePosition();
      if (browser.get()) {
        window->Emit("move", (int)LOWORD(lParam), (int)HIWORD(lParam));
      }
      break;
    case WM_NCHITTEST: {
      LRESULT result;
      if (DwmDefWindowProc != NULL) {
        if (DwmDefWindowProc(hwnd, message, wParam, lParam, &result)) {
          return result;
        }
      }
      break;
    }
    case WM_ERASEBKGND:
      if (browser.get()) {
        return 0;
      }
      break;
    case WM_CLOSE:
      if (browser.get()) {
        browser->ParentWindowWillClose();
      }
      break;
    //case WM_DESTROY:
    //  PostQuitMessage(0);

  }

  return DefWindowProc(hwnd, message, wParam, lParam);
}
// Processes messages for the main window.
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {

  // Callback for the main window
  switch (message) {
    case WM_CREATE: {
      NativeWindow* window = ClientHandler::GetWindow(hwnd);
      RECT rect;
      GetClientRect(hwnd, &rect);
      Cef::AddWebView(hwnd, rect, url_, browserSettings);
      return 0;
    }
    case WM_PAINT: {
      PAINTSTRUCT ps;
      HDC hdc;
      hdc = BeginPaint(hwnd, &ps);
      EndPaint(hwnd, &ps);
      return 0;
    }
    /*case WM_SETFOCUS:
      if (g_handler.get() && g_handler->GetBrowserHwnd()) {
        // Pass focus to the browser window
        PostMessage(g_handler->GetBrowserHwnd(), WM_SETFOCUS, wParam, NULL);
      }
      return 0;*/

    case WM_SIZE: {
      NativeWindow* window = ClientHandler::GetWindow(hwnd);
      if (window->GetBrowser()) {
        RECT r;
        GetClientRect(hwnd, &r);
        HDWP hdwp = BeginDeferWindowPos(1);
        hdwp = DeferWindowPos(hdwp, window->GetBrowser()->GetWindowHandle(), NULL, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER);
        EndDeferWindowPos(hdwp);
      }
      window->UpdatePosition();
      break;
    }
    case WM_MOVE:
      ClientHandler::GetWindow(hwnd)->UpdatePosition();
      break;
    case WM_ERASEBKGND:
      return 1;
    // case WM_ERASEBKGND: {
    //   NativeWindow* window = ClientHandler::GetWindow(hwnd);
    //   if (window->GetBrowser()) {
    //     // Dont erase the background if the browser window has been loaded
    //     // (this avoids flashing)
    //     return 0;
    //   }
    case WM_CLOSE: {
      NativeWindow* window = ClientHandler::GetWindow(hwnd);
      if (window->GetBrowser()) {
        window->GetBrowser()->ParentWindowWillClose();
      }
    }
    break;
    //case WM_DESTROY:
    //  PostQuitMessage(0);

  }

  return DefWindowProc(hwnd, message, wParam, lParam);
}