/*
 * Copyright (C) 2007 Pleyo.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Pleyo nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PLEYO AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL PLEYO OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#include "kjs/wk_config.h"
#include "BALConfiguration.h"
#include "BIKeyboardEvent.h"
#include "BIMouseEvent.h"
#include "ChromeClientBal.h"
#include "ContextMenuClientBal.h"
#include "CString.h"
#include "Document.h"
#include "EditorClientBal.h"
#include "EventHandler.h"
#include "FrameBal.h"
#include "FrameLoaderClientBal.h"
#include "FrameLoader.h"
#include "FrameLoadRequest.h"
#include "FrameView.h"
#include "Page.h"
#include "RenderTreeAsText.h"
#include "Settings.h"
#include "TextStream.h"
#include "WindowBal.h"
#ifdef __OWB_WEB_UI__
#include "ChromeClientWebUI.h"
#include "FrameLoaderClientWebUI.h"
#endif
#include "BTLogHelper.h"

using WebCore::EventHandler;
using WebCore::Frame;
using WebCore::FrameBal;
using WebCore::FrameLoadRequest;
using WebCore::FrameLoaderClientBal;
using WebCore::Page;

namespace BAL {

WindowBal::WindowBal(
const uint16_t x,
const uint16_t y,
const uint16_t width,
const uint16_t height)
    : m_x(x)
    , m_y(y)
    , m_width(width)
    , m_height(height)
{
#ifdef __OWB_WEB_UI__
    chromeClient = new WebCore::ChromeClientWebUI();
    frameLoaderClient = new WebCore::FrameLoaderClientWebUI();
#else
    chromeClient = new WebCore::ChromeClientBal();
	frameLoaderClient = new WebCore::FrameLoaderClientBal();
#endif
	contextMenuClient = new WebCore::ContextMenuClientBal();
	editorClient = new WebCore::EditorClientBal();
	dragClientBal = new WebCore::DragClientBal();
	inspectorClientBal = new WebCore::InspectorClientBal();

    Page* page = new Page(chromeClient, contextMenuClient, editorClient,
    		dragClientBal, inspectorClientBal);

    page->settings()->setLoadsImagesAutomatically(true);
    page->settings()->setMinimumFontSize(5);
    page->settings()->setMinimumLogicalFontSize(5);
    page->settings()->setShouldPrintBackgrounds(true);
#ifdef __OWB_JS__
    page->settings()->setJavaScriptEnabled(true);
#else
    page->settings()->setJavaScriptEnabled(false);
#endif
    page->settings()->setPluginsEnabled(true);
    page->settings()->setDefaultFixedFontSize(14);
    page->settings()->setDefaultFontSize(14);

    editorClient->setPage(page);

    m_mainFrame = new FrameBal(page, 0, frameLoaderClient, this);
    frameLoaderClient->setFrame(m_mainFrame.get());
    chromeClient->setFrame(m_mainFrame.get());
    page->chrome()->setWindowRect(FloatRect(x, y, width, height));
    m_mainFrame->view()->resize(width, height);
    m_mainFrame->view()->move(x, y);

	 m_dc = createBIGraphicsContext();

}

WindowBal::~WindowBal()
{
    delete m_mainFrame->page();
    //hack to free frame

	m_mainFrame = 0;
    // how do we release WebCore::allPages ?
	delete chromeClient;
	delete contextMenuClient;
	delete editorClient;
	delete inspectorClientBal;

	delete m_dc;
    /* frameLoaderClient e dragClientBal já são deletados em outro ponto */
}

void WindowBal::handleEvent(BAL::BIEvent *event)
{
    return;
    // Window events are handled in WindowManager
}

bool WindowBal::canGoBackOrForward(int distance)
{
    return m_mainFrame->loader()->canGoBackOrForward(distance);
}

void WindowBal::goBackOrForward(int distance)
{
    m_mainFrame->loader()->goBackOrForward(distance);
}

void WindowBal::setURL(const KURL& url)
{
    WebCore::FrameLoader* fL = m_mainFrame->loader();

    if (url.protocol().isEmpty() && !url.isLocalFile())
    {
        KURL urlWithProtocol(url.url().prepend("http://"));
        m_url = urlWithProtocol;
    }
    else
    {
        m_url = url;
    }

    fL->load(m_url);
}

void WindowBal::setURL(const KURL& url, const KURL& css)
{
    WebCore::FrameLoader* fL = m_mainFrame->loader();

    if (css.protocol().isEmpty() && !css.isLocalFile())
    {
        KURL cssWithProtocol(css.url().prepend("http://"));
        m_css = cssWithProtocol;
    }
    else
    {
        m_css = css;
    }

    fL->setExternalCSS(m_css);
    setURL(url);
}

void WindowBal::stop()
{
    m_mainFrame->loader()->stopForUserCancel();
}

const KURL& WindowBal::URL()
{
    return m_url;
}

const BTWidget* WindowBal::widget() const
{
    return m_mainFrame->view();
}

void WindowBal::setFrameLoaderClient(WebCore::FrameLoaderClientBal* frameLoaderClient)
{
    delete m_mainFrame->page();

#ifdef __OWB_WEB_UI__
    WebCore::ChromeClientBal* chromeClient = new WebCore::ChromeClientWebUI();
#else
    WebCore::ChromeClientBal* chromeClient = new WebCore::ChromeClientBal();
#endif
    WebCore::ContextMenuClientBal* contextMenuClient = new WebCore::ContextMenuClientBal();
    WebCore::EditorClientBal* editorClient = new WebCore::EditorClientBal();
    Page* page = new Page(chromeClient, contextMenuClient, editorClient,
                    new WebCore::DragClientBal(), new WebCore::InspectorClientBal());
    editorClient->setPage(page);

    m_mainFrame = new FrameBal(page, 0, frameLoaderClient, this);
    frameLoaderClient->setFrame(m_mainFrame.get());
    chromeClient->setFrame(m_mainFrame.get());
    page->chrome()->setWindowRect(FloatRect(m_x, m_y, m_width, m_height));
    m_mainFrame->view()->resize(m_width, m_height);
    m_mainFrame->view()->move(m_x, m_y);
}



WebCore::Frame* WindowBal::mainFrame(void)
{
   return  m_mainFrame.get();
}



}
