/*
 * Copyright (c) 2006-2008 MiniMe.
 * Code released under The MIT / X Window System License.   Full licence text can be found in license.txt
 */
package minime.ui;

import javax.microedition.lcdui.Graphics;

import minime.Composite;
import minime.Drawable;
import minime.Portability;
import minime.Rectangle;
import minime.core.Component;
import minime.core.EventDispatcher;
import minime.core.EventListener;
import minime.core.FocusManager;
import minime.core.Runtime;

/**
 * The View class is used to construct a view for application, it can hold many
 * drawables, can listen event and handle it.The contents displayed and their
 * interaction with the user are defined by subclasses.
 * 
 * @author yishu
 * 
 */
public abstract class View implements EventListener
{

    protected Composite viewComposite;

    // display area is space View allocates to child class to draw their own
    // stuff
    protected Rectangle displayArea;

    public View()
    {
        // create view composit set to device screen size (no need to clip)
        viewComposite = new Composite();
        viewComposite.setLeft(Runtime.getInstance().getLeft());
        viewComposite.setTop(Runtime.getInstance().getTop());
        viewComposite.setWidth(Runtime.getInstance().getDisplayWidth());
        viewComposite.setHeight(Runtime.getInstance().getDisplayHeight());
        viewComposite.setBgColor(Portability.TRANSPARENT) ;
        displayArea = new Rectangle(0, 0, viewComposite.getWidth(), viewComposite.getHeight());
    }

    protected void setBgColor(int color)
    {
    	viewComposite.setBgColor(color) ;
    }
    
    public int getBgColor()
    {
    	return viewComposite.getBgColor() ;
    }
    /**
     * When loading the view to screen and become the current view,it call this
     * method to load some dynamic resource, for example, add some drawables
     * into view and so on.
     * 
     */
    public abstract void activate();

    /**
     * When unloading the view from screen and don't display the view, it will
     * call this method to remove resources, for example, remove some drawables
     * from the view.
     * 
     * \TODO deactivate should remove all component from the view (should it be final ?)
     */
    public abstract void deactivate();

    /**
     * Layout the drawables in the view.Returns the displayable area rectangle.
     * 
     * @return the displayable area rectangle.
     */
    public Rectangle layout()
    {
        displayArea.x = 0 ; //viewComposite.getLeft();
        displayArea.y = 0 ; //viewComposite.getTop();
        displayArea.w = viewComposite.getWidth();
        displayArea.h = viewComposite.getHeight();
        return getDisplayArea();
    }

    /**
     * Returns the view position on screen.
     * 
     * @return the view position
     */
    public Rectangle getPosition()
    {
        Rectangle r = new Rectangle(viewComposite.getLeft(), viewComposite.getTop(), viewComposite.getWidth(), viewComposite.getHeight());
        return r;
    }

    /**
     * Returns the displayable area rectangle.
     * 
     * @return the displayable area rectangle.
     */
    public Rectangle getDisplayArea()
    {
        return displayArea;
    }

    /**
     * Sets the displayable area.
     * 
     * @param x
     *            the x coordinate of the displayable area.
     * @param y
     *            the y coordinate of the displayable area.
     * @param w
     *            the width of the displayable area.
     * @param h
     *            the height of the displayable area.
     */
    public void setDisplayArea(int x, int y, int w, int h)
    {
        displayArea = new Rectangle(x, y, w, h);
    }

    /**
     * Gets the view top.
     * 
     * @return the view top.
     */
    public int getTop()
    {
        return viewComposite.getTop();
    }

    /**
     * Gets the view left.
     * 
     * @return the view left.
     */
    public int getLeft()
    {
        return viewComposite.getLeft();
    }

    /**
     * Gets the view height.
     * 
     * @return the view height.
     */
    public int getHeight()
    {
        return viewComposite.getHeight();
    }

    /**
     * Gets the view width.
     * 
     * @return the view width.
     */
    public int getWidth()
    {
        return viewComposite.getWidth();
    }
    
    /**
     * Add a new drawable element to the View.
     * 
     * @param drawable, drawable to be added.   The drawable will be activated before 
     * being added
     */
    public void addToView(Drawable drawable)
    {
        drawable.activate();
        viewComposite.addDrawable(drawable);
    }

    /**
     * Remove a drawable element from the View.
     * 
     * @param drawable, drawable to be removed
     */
    public void removeFromView(Drawable drawable)
    {
        drawable.deactivate();
        viewComposite.removeDrawable(drawable);
    }

    /**
     * Render/paint the view
     * 
     * Render each element store in the view.
     * 
     * @param gc
     */
    public void render(Graphics gc)
    {
        viewComposite.render(gc);
    }
    
    public void gameTick()
    {}
}
