package com.rim.samples.device.LEDDemo;

import net.rim.device.api.system.LED;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.system.RealtimeClockListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

/*
 * BlackBerry applications that provide a user interface must extend 
 * UiApplication.
 */
class LEDDemo extends UiApplication implements RealtimeClockListener , FieldChangeListener
{
    private DateField _startTime, _endTime;
    private ButtonField _startStopButton;
    static private int _sLedColor[] = { 0x00ff0000 , 0x0000ff00 , 0x000000ff };
    private int _index = 0;
      
    /**
     * Entry point for application. 
     */
    public static void main(String[] args)
    {
        // Create a new instance of the application.
        LEDDemo theApp = new LEDDemo();
        
        // To make the application enter the event thread and start processing messages, 
        // we invoke the enterEventDispatcher() method.
        theApp.enterEventDispatcher();
    }
        
    public void clockUpdated()
    {
        long currentTime = System.currentTimeMillis();
        if(currentTime >= _startTime.getDate() && currentTime < _endTime.getDate())
        {
                LED.setState(LED.STATE_BLINKING);
                LED.setColorConfiguration(100, 1000, _sLedColor[_index]); 
                if(++_index > 2)
                    _index = 0;
        }
        else if(currentTime >= _endTime.getDate())
        {
            _startStopButton.setCookie(new Boolean(false));
            _startStopButton.setLabel("Start");
            removeRealtimeClockListener(this);
            LED.setState(LED.STATE_OFF);            
            Dialog.alert("Time up!");         
        }
    }
    
    private void OnStartStopBtn()
    {
        Boolean cookie = (Boolean)_startStopButton.getCookie();
        if(cookie == null || cookie.booleanValue() == false)
        {
            long currentTime = System.currentTimeMillis();
            if(currentTime >= _endTime.getDate())
            {
                Dialog.alert("The Endtime has been past!");
            }
            else if(LED.isPolychromatic(LED.LED_TYPE_STATUS))   // whether is capable of displaying multiple colors 
            {
                _startStopButton.setCookie(new Boolean(true));
                _startStopButton.setLabel("Stop");
                addRealtimeClockListener(this);                
            }
            else
            {
                Dialog.alert("Your BB'LED can't support multiple colors!");
            }
        }
        else
        {
            _startStopButton.setCookie(new Boolean(false));
            _startStopButton.setLabel("Start");
            removeRealtimeClockListener(this);
            LED.setState(LED.STATE_OFF);            
        }
    }
    
    public void fieldChanged( Field field, int context )
    {
        if( field.equals( _startStopButton )) 
        {
            OnStartStopBtn();
        }        
    }
    /**
     * <p>The default constructor. Creates all of the RIM UI components and pushes the
     * application's root screen onto the UI stack.
     */
    private LEDDemo()
    {
        // Get current time
        long currentTime = System.currentTimeMillis();
        _startTime = new DateField("Start: " , currentTime + 6000, DateField.DATE_TIME);
        _endTime = new DateField("End: " , currentTime + 3600000, DateField.DATE_TIME);
        _startStopButton = new ButtonField("Start" , Field.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
        _startStopButton.setChangeListener(this);
        SetupScreen setupUI = new SetupScreen();
        setupUI.add(_startTime);
        setupUI.add(_endTime);
        setupUI.add(_startStopButton);
        // Push the main screen instance onto the UI stack for rendering.       
        pushScreen(setupUI);
    }    
}


/**
 * Create a new screen that extends MainScreen, which provides default standard
 * behavior for BlackBerry applications.
 */
/*package*/ final class SetupScreen extends MainScreen
{
    /**
     * HelloWorldScreen constructor.
     */
    SetupScreen()
    {
        // Add a field to the title region of the screen. We use a simple LabelField 
        // here. The ELLIPSIS option truncates the label text with "..." if the text 
        // is too long for the space available.
        LabelField title = new LabelField("LED Setup" , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
        setTitle(title);

        // Add a read only text field (RichTextField) to the screen.  The RichTextField
        // is focusable by default.  In this case we provide a style to make the field
        // non-focusable.

    }
    
    /**
     * Display a dialog box to the user with "Goodbye!" when the application 
     * is closed.
     * 
     * @see net.rim.device.api.ui.Screen#close()
     */
    public void close()
    {
        // Display a farewell message before closing application.
        Dialog.alert("Goodbye!");
        System.exit(0);
        
        super.close();
    }   
}
