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

/**
 * The Event class is responsible for creating a event, this event has specified
 * type, and has two int parameters and an object parameter for event, also an
 * int time parameter for executing the event.
 * 
 * @author yishu
 * 
 */
public class Event
{
    // The event type
    /** The key event, paramA is game action, paramB is keyCode */
    public static final int KEY_EVENT = 0x1000;

    /** The application event */
    public static final int APP_EVENT = 0x1001;

    /** SoftBar event, paramA is sb action, paramB is sb keyCode */
    public static final int SB_EVENT = 0x1002;

    /**
     * The key down event, after the key pressed down, it will touch off the
     * event, paramA is game action.
     */
    public static final int KEY_DOWN_EVENT = 0x1003;

    /**
     * The key up event, after the key pressed, when it is up, it touches off
     * key up event, paramA is game action.
     */
    public static final int KEY_UP_EVENT = 0x1004;


    /** The circleMenu event to notify which item is selected */
    public static final int CIRCLEMENU_EVENT = 0x1006;

    /** The menu event to notify which item is selected */
    public static final int MENU_EVENT = 0x1007;

    /** The menu event to notify when selection is made */
    public static final int MENU_SELECTION_EVENT = 0x1008;

    /**
     * The timer off event, when a pointed time task finishes, it can touch off
     * this event.
     */
    public static final int TIMER_OFF_EVENT = 0x1009;

    /** The menu event to notify when selection is made */
    public static final int POPUP_EVENT = 0x100A;
    
    /**
     * Event fire from ChoiceGroup when a radio button is selected.
     */
    public static final int RADIO_SELECTED_EVENT = 0x100C;

    /**
     * Event fire from ChoiceGroup when a check box is marked.
     */
    public static final int BOX_MARKED_EVENT = 0x100D;

    /**
     * Event fire from ChoiceGroup when a check box is unmarked.
     */
    public static final int BOX_UNMARKED_EVENT = 0x100E;
    public static final int NET_EVENT = 0x200A;

    /** The card event, paramA is action status */
    public static final int CARD_OPERATION_SUCCESSFUL = 0x3000 ; 
    public static final int CARD_OPERATION_ERROR = 0x3001 ; 
    public static final int CARD_SCRIPT_EVENT = 0x3002 ; 

    /** The event parameter for application event */
    public static final int APP_START = 0x0100;
    public static final int APP_STOP = 0x0101;
    public static final int APP_PAUSE = 0x0102;
    public static final int APP_RESUME = 0x0103;
    public static final int APP_DESTROY = 0x0104;

    /** The event parameter for network event */
    public static final int RELOAD_RESULT_OK = 0x0206;
    public static final int RELOAD_RESULT_ERROR = 0x0207;
    public static final int RELOAD_PROGRESS = 0x0208;
    public static final int RELOAD_CANCELED = 0x0209;

    public static final int NET_STATUS_OPERATION_COMPLETED = 0x020A;
    public static final int NET_STATUS_ERROR = 0x020B;
    public static final int NET_STATUS_TIMEOUT = 0x020C;

    public static final int STARTUP_EVENT=0x020D;
    public static final int CARD_EVENT=0x020E;
    public static final int PASSWORD_LEFT_TIMES_EVENT=0x020F;
    //timer event
    public static final int TIMER_EVENT=0x0310;
    public static final int TIMER_FINISH=0x0311;
    /** The event type */
    public int type;
    /** the time for event is to be executed. */
    public long when;

    public int paramA;
    public int paramB;
    public Object payload;

    /**
     * Constructs an event instance with given event type, event paramters and
     * time the event is to be executed.
     * 
     * @param type
     *            the event type
     * @param paramA
     *            the first int parameter for event
     * @param paramB
     *            the second int parameter for event
     * @param payload
     *            an object parameter for event.
     * @param when
     *            the time for event is to be executed.
     */
    private Event(int type, int paramA, int paramB, Object payload, long when)
    {
        this.type = type;
        this.paramA = paramA;
        this.paramB = paramB;
        this.payload = payload;
        this.when = when;
    }

    /**
     * Creates a new event instance with given specified type, two int
     * parameters and an object.
     * 
     * @param type
     *            the event type
     * @param paramA
     *            the first int parameter for event
     * @param paramB
     *            the second int parameter for event
     * @param payload
     *            an object parameter for event.
     * @return the created event.
     */
    public static Event createEvent(int type, int paramA, int paramB, Object payload)
    {
        return createEvent(type, paramA, paramB, payload, 0);
    }

    /**
     * Creates a new event instance with given specified type and two int
     * parameters.
     * 
     * @param type
     *            the event type
     * @param paramA
     *            the first int parameter for event
     * @param paramB
     *            the second int parameter for event
     * @return the created event.
     */
    public static Event createEvent(int type, int paramA, int paramB)
    {
        return createEvent(type, paramA, paramB, null, 0);
    }

    /**
     * Creates a new event instance with given specified type, two int
     * parameters ,an object parameter and the delay time for executing the
     * event after that specified delay.
     * 
     * @param type
     *            the event type
     * @param paramA
     *            the first int parameter for event
     * @param paramB
     *            the second int parameter for event
     * @param payload
     *            an object parameter for event.
     * @param delay
     *            delay in milliseconds before task is to be executed.
     * @return the created event.
     */
    public static Event createEvent(int type, int paramA, int paramB, Object payload, long delay)
    {
        // TODO: get the object from a pool
        return new Event(type, paramA, paramB, payload, delay + System.currentTimeMillis());
    }

    /**
     * Returns the event information as a string.
     */
    public String toString()
    {
        String print = null;
        switch (type)
        {
            case KEY_EVENT:
                print = "KEY_EVENT :" + paramA + ":" + paramB;
                break;
            case KEY_DOWN_EVENT:
                print = "KEY_DOWN_EVENT :" + paramA + ":" + paramB;
                break;
            case KEY_UP_EVENT:
                print = "KEY_UP_EVENT :" + paramA + ":" + paramB;
                break;
            case SB_EVENT:
                print = "SB_EVENT :" + paramA + ":" + paramB;
                break;
            case MENU_EVENT:
                print = "MENU_EVENT :" + paramA + ":" + paramB;
                break;
            case MENU_SELECTION_EVENT:
                print = "MENU_SELECTION_EVENT :" + paramA + ":" + paramB;
                break;
            case APP_EVENT:
                print = "APP_EVENT: ";
                switch (paramA)
                {
                    case APP_START:
                        print = print + "APP_START";
                        break;
                    case APP_STOP:
                        print = print + "APP_STOP";
                        break;
                    case APP_PAUSE:
                        print = print + "APP_PAUSE";
                        break;
                    case APP_RESUME:
                        print = print + "APP_RESUME";
                        break;
                    case APP_DESTROY:
                        print = print + "APP_DESTROY";
                        break;
                }
                break;
        }
        return print;
    }
}
