/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2011 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/

#include "embeddedWebNavigatorLifeSpanHandler.h"
#include "embeddedWebNavigatorClient.h"
#include "embeddedWebNavigatorPopupInformations.h"

namespace Scol
{
  namespace EmbeddedWebNavigator
  {

WebNavigatorLifeSpanHandler::WebNavigatorLifeSpanHandler(CefRefPtr<WebNavigatorClient>& parentWebNavigatorClientInstance, const ScolWindowHandle& scolMainWindow) : WebNavigatorHandler(parentWebNavigatorClientInstance, scolMainWindow)
{
}

WebNavigatorLifeSpanHandler::WebNavigatorLifeSpanHandler() : WebNavigatorHandler(CefRefPtr<WebNavigatorClient>(), 0)
{
  // Forbidden ctor.
}

WebNavigatorLifeSpanHandler::~WebNavigatorLifeSpanHandler()
{
}

bool WebNavigatorLifeSpanHandler::OnBeforePopup(CefRefPtr<CefBrowser> parentBrowser, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, const CefString& url, CefRefPtr<CefClient>& client, CefBrowserSettings& settings)
{
  // Store popup information, and call a scol reflexive function, to let the scol developper to create another web navigator or not.
  PopupInformations* popupInformations = new PopupInformations(parentWebNavigator, popupFeatures.x, popupFeatures.y, popupFeatures.width, popupFeatures.height, url);

  // Send event
  SendMessage(scolMainWindowHandle, WEB_NAVIGATOR_LIFESPAN_ON_POPUP_WINMSG, (int)popupInformations, 0);

  // Allways cancel popup creation, if we want it, then it's up to the scol developper to create a new web navigator with the given informations.
  return true;
}

void WebNavigatorLifeSpanHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
  // Thread locking
  AutoLock lock_scope(this);

  // Register the newly created browser in our WebNavigatorClient object.
  parentWebNavigatorClient->_NotifyBrowserCreated(browser);
}

bool WebNavigatorLifeSpanHandler::RunModal(CefRefPtr<CefBrowser> browser)
{
  // Send event
  SendMessage(scolMainWindowHandle, WEB_NAVIGATOR_LIFESPAN_RUN_MODAL_WINMSG, (int)parentWebNavigator, 0);

  // TODO Manage the response
  return false;
}

bool WebNavigatorLifeSpanHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
  // WARNING! DO NOT SEND MESSAGE TO SCOL HERE
  // Because when the scol main thread will handle the reflexive call, the WebNavigator and Cef::Browser object will already have been destroyed!
  return false;
}

void WebNavigatorLifeSpanHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
  // Unregister the browser from our WebNavigatorClient object.
  parentWebNavigatorClient->_NotifyBrowserDestroyed(browser);

  // Send event
  SendMessage(scolMainWindowHandle, WEB_NAVIGATOR_LIFESPAN_ON_BEFORE_CLOSE_WINMSG, (int)parentWebNavigator, 0);
}

  }
}