/*
 * Environment.java
 *
 * Copyright 1999 Helsinki University of Technology
 * ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
 *
 */

package tove.dcf;

import java.io.*;
import java.util.zip.*;
import java.beans.*;
import java.util.*;
import java.text.*;
import tove.dcf.common.*;

/**
 *  DCF Environment is a starting point for running DCF components.
 *  Use load or createRoot methods to create DCF environment.
 *
 *  @author   Pasi Nummisalo
 *  @version  1.0  (14.09.98)
 */

public class Environment
{
   private static ManagedFacade _facade;
   static ComponentIf _root;
   public static String _path = null;

   static SimpleDateFormat timeOfDayFormatter = new SimpleDateFormat("HHMM");
   static SimpleDateFormat dayOfWeekFormatter = new SimpleDateFormat("EE");

   /**
   * Create new root component.
   * @param fullClassName_  component's class name with package.
   * @param name_  root component's name (used also as file name with .ser file type).
   * @param manageable_  true = components can be managed from GUI.
   */

   public static void createRoot(String fullClassName_, String name_,
      boolean manageable_)
         throws ClassNotFoundException, IOException
   {
      if (manageable_ == true)
      {
         _facade = new ManagedFacade();
      }

      _root = (ComponentIf) Beans.instantiate(null, fullClassName_);
      _root.setName(name_);

      if (_facade != null)
      {
         _facade.register(_root);
      }
   }

   /**
   * Load components from disk.
   * @param name_  root component's name (file name).
   * @param manageable_  true = components can be managed from GUI.
   */

   public static void loadRoot(String name_, boolean manageable_)
      throws ClassNotFoundException, IOException
   {
      if (manageable_ == true)
      {
         _facade = new ManagedFacade();
      }

      _root = (ComponentIf) Beans.instantiate(null, name_);

      if (_facade != null)
      {
         _facade.register(_root);
      }
   }

   public static void setPath(String path_)
   {
      _path = path_;
   }

   public static void waitRequest()
      throws InterruptedException
   {
         System.out.println("dcf Server waiting for invocations");
         java.lang.Object sync = new java.lang.Object();
         synchronized (sync)
         {
            sync.wait();
         }
   }


   public static void traceSend(int key_, String message_)
   {
      try
      {
         if (_facade != null)
         {
            Object[] param = new Object[1];
            param[0] = message_;
            _facade._manager.action(key_, "remoteTraceSending", param);
            //remoteAdapter.remoteGUI.action(key, "remoteTraceSending", param);
         }
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
   }

   public static void sendPropertyChange(int key_, String name_, Object value_)
   {
      try
      {
         if (_facade != null)
         {
            _facade._manager.propertyChange(key_, name_, value_);
         }
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
   }

   public static int getHourOfDay()
   {
      return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
   }

   public static int getMinute()
   {
      return Calendar.getInstance().get(Calendar.MINUTE);
   }

   public static int getDayOfWeek()
   {
      return Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
   }

   public static int getDayOfYear()
   {
      return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
   }

   public static int getTimeOfDay()
   {
      int hour = getHourOfDay();
      int min = getMinute();
      String time = String.valueOf(hour);
      String minStr = String.valueOf(min);
      if (minStr.length() == 1)
      {
         minStr = "0" + minStr;
      }
      time = time + minStr;
      return Integer.parseInt(time);
   }

   public static String getDayOfWeekString()
   {
      return dayOfWeekFormatter.format(new Date());
   }

   protected static void registerComponent(ComponentImpl component_)
   {
      if (_facade != null)
      {
         _facade._components.put(new Integer(component_.hashCode()), component_);
      }
   }

   protected static void unregisterComponent(Integer key_)
   {
      if (_facade != null)
      {
         _facade._components.remove(key_);
      }
   }

}
