/** @file GalioWaferCollection.h
 *
 * Copyright (C) Espial Limited 2014 - Company confidential - All rights reserved.
 *
 */

#ifndef GalioWaferCollection_h
#define GalioWaferCollection_h

#if ENABLE(RMAI)

#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/text/AtomicString.h>
#include <wtf/Vector.h>

#include "GalioBrowser.h"
#include "GalioConfiguration.h"
#include "MOM.h"
#include "GalioUtility.h"
#include "Document.h"

namespace WebCore {

class GalioWafer;
class Frame;
class Document;

class GalioWaferCollection : public RefCounted<GalioWaferCollection> {
public:
    static GalioWaferCollection* instance();

    GalioBrowser* browser() const;
    GalioConfiguration* config() const;
    //MOM* mom() const;
    GalioUtility* utility() const;

    GalioWafer* activeWafer() const;
    GalioWafer* topmostVisible() const;
    void show(GalioWafer* wafer);
    void hide(GalioWafer* wafer);
    void makeActive(GalioWafer* wafer);
    void makeInactive(GalioWafer* wafer);

    Frame* frame() const { return m_frame; }
    Document* document() const { return m_document; }

    GalioWafer* findOwnerWafer(const Document* document) const;
    void createApplicationTree(Frame* frame);
    bool dispatchSystemEvent(PassRefPtr<Event> event, PassRefPtr<Node> target);

    int nextZIndex() { return ++m_zIndex; }
    bool handleKeyEvent(const PlatformKeyboardEvent& kevt, Frame* frame);
    
protected:
    GalioWafer* link(Vector<GalioWafer*>& list, GalioWafer* wafer);
    bool delink(Vector<GalioWafer*>& list, GalioWafer* wafer);
    bool dispatchSystemEventToActiveList(PassRefPtr<Event> prpEvent, Document* exceptDoc, GalioWafer*);

private:
    GalioWaferCollection();

    mutable RefPtr<GalioBrowser> m_browser;
    mutable RefPtr<GalioConfiguration> m_config;
    mutable RefPtr<MOM> m_mom;
    mutable RefPtr<GalioUtility> m_utility;
    Vector<GalioWafer*> m_activeList;
    Vector<GalioWafer*> m_showList;
    Document* m_document;
    Frame* m_frame;
    int m_zIndex;
};

} // namespace WebCore

#endif // ENABLE(RMAI)

#endif // GalioWaferCollection_h
