package com.funambol.omadmclient.android.service;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class NotificationsScreen extends Activity 
{
    private Button m_button; // to do:  change to Array o f Buttons in case of show many messages

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        Log.i(OmaDmClinetService.LOGTAG, "enter >> NotificationsScreen.onCreate()");

        super.onCreate(savedInstanceState);

        setContentView(R.layout.notifications);

        // set onClick handlers for buttons
        Button confirmButton = (Button)this.findViewById(R.id.buttonNotificationConfirm);
        if(confirmButton != null)
        {
            Log.i(OmaDmClinetService.LOGTAG, "before extras.getString(Notification)");
            //if (savedInstanceState != null)
            //{
            //String message = savedInstanceState.getString("Notification");
            //if (message != null)
                //{
                    Log.i(OmaDmClinetService.LOGTAG, "confirmButton.setText(message)");
                    confirmButton.setText(NotificationCallbackInterface.m_message);
                    confirmButton.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) 
                        {
                        // to do: synchronize
                        NotificationCallbackInterface.m_result = true;

                        synchronized(NotificationCallbackInterface.m_callBack)
                         {
                        NotificationCallbackInterface.m_callBack.notifyAll();
                          }
                        finish();
                        }
                    });
            //}
            //else
            //{
            //    Log.e(OmaDmClinetService.LOGTAG, "failed to get savedInstanceState");
            //}
        }
        else
        {
            Log.e(OmaDmClinetService.LOGTAG, "error: failed to get button from layout");
        }

        Log.i(OmaDmClinetService.LOGTAG, "leave << NotificationsScreen.onCreate()");
    }
    
    @Override
    protected void onStart() 
    {
        Log.i(OmaDmClinetService.LOGTAG, "enter >> NotificationsScreen.onStart()");

        super.onStart();

        /*
        LinearLayout layout = (LinearLayout)this.findViewById(R.layout.notifications);
    	if (layout != null)
    	{
        	
    		// create new button in runtime for show notification 
        	Button btnNotify = new Button(this);
        	if (btnNotify != null)
        	{
            	Bundle extras = getIntent().getExtras(); 
            	if(extras != null)
            	{
            		String message = extras.getString("Notification");
                	btnNotify.setText(message);
                	btnNotify.setOnClickListener(new Button.OnClickListener() 
                	{
                		public void onClick(View v) 
                		{
                			// to do: synchronize
                			NotificationCallbackInterface.m_result = true;
                			
                			synchronized(NotificationCallbackInterface.m_callBack)
                			{
                				NotificationCallbackInterface.m_callBack.notifyAll();
                			}
                			finish();
                        }
                    });
                   
                    Log.i(OmaDmClinetService.LOGTAG, "before >> layout.addView(btnNotify)");
                	layout.addView(btnNotify);   	        		
                    Log.i(OmaDmClinetService.LOGTAG, "after << layout.addView(btnNotify)");
            	}
            	else
            	{
                    Log.e(OmaDmClinetService.LOGTAG, "failed to get message for notification");
            	}
        	}
        	else
        	{
            	Log.e(OmaDmClinetService.LOGTAG, "failed to create new button in runtime");
        	}  		
    	}
    	else
    	{
        	Log.e(OmaDmClinetService.LOGTAG, "failed to get layout instance reference");
    	}
    	*/

        Log.i(OmaDmClinetService.LOGTAG, "leave << NotificationsScreen.onStart()");
    }

    protected void onStop ()
    {
        super.onStop();
        
        synchronized(NotificationCallbackInterface.m_callBack)
        {
            NotificationCallbackInterface.m_callBack.notifyAll();
        }
    }
}
