package sillysagiri;

import java.util.Enumeration;
import java.util.Hashtable;

public final class Input {
  public Hashtable hash_keyState = new Hashtable(20);
  
  public int state_anyKey = 0;
  public int state_touchState = Sagiri.TOUCH_NONE;
  public int state_lastKey = 0;

  public int touchX = 0;
  public int touchY = 0;

  public Input()
  {
  }
  
  public final void Update()
  {
    Enumeration i = hash_keyState.keys();
    while(i.hasMoreElements())
    {
      Integer key = (Integer)i.nextElement();
      Integer value = (Integer)hash_keyState.get(key);

      
      if (value.intValue() == -1) hash_keyState.remove(key);
      else hash_keyState.put(key, new Integer(value.intValue()+1));
    }

    if (state_anyKey != 0) state_anyKey++;
    state_touchState = Sagiri.TOUCH_NONE;
  }
  
  public final void HandleKeyDown(int keycode)
  {
    Integer key = new Integer(keycode);
    Integer value = (Integer)hash_keyState.get(key);

    if (value != null)
      hash_keyState.put(key, new Integer(Math.max(value.intValue(), 1)));
    else
      hash_keyState.put(key, new Integer(1));

    state_anyKey = Math.max(state_anyKey, 1);
    state_lastKey = keycode;
  }
  
  public final void HandleKeyUp(int keycode)
  {
    Integer key = new Integer(keycode);

    hash_keyState.put(key, new Integer(-1));
    state_anyKey = -1;
    state_lastKey = keycode;
  }
  
  public final void HandleTouch(int phase, int x, int y)
  {
    state_touchState = phase;
    touchX = x;
    touchY = y;
  }

  public final boolean IsKeyHeld(int keycode)
  {
    Integer key = new Integer(keycode);
    Integer value = (Integer)hash_keyState.get(key);

    if (value != null)
      return value.intValue() > 0;
    else
      return false;
  }

  public final boolean IsKeyHeld(int[] keycode)
  {
    for (int i=0; i<keycode.length; i++)
      if (IsKeyHeld(keycode[i])) return true;
    return false;
  }

  public final boolean IsKeyDown(int keycode)
  {
    Integer key = new Integer(keycode);
    Integer value = (Integer)hash_keyState.get(key);

    if (value != null)
      return value.intValue() == 0;
    else
      return false;
  }

  public final boolean IsKeyDown(int[] keycode)
  {
    for (int i=0; i<keycode.length; i++)
      if (IsKeyDown(keycode[i])) return true;
    return false;
  }

  public final boolean IsKeyReleased(int keycode)
  {
    Integer key = new Integer(keycode);
    Integer value = (Integer)hash_keyState.get(key);

    if (value != null)
      return value.intValue() == -1;
    else
      return false;
  }

  public final boolean IsKeyReleased(int[] keycode)
  {
    for (int i=0; i<keycode.length; i++)
      if (IsKeyReleased(keycode[i])) return true;
    return false;
  }

  public final boolean IsKeyHeldAny() { return state_anyKey > 1; }
  public final boolean IsKeyDownAny() { return state_anyKey == 1; }
  public final boolean IsKeyReleasedAny() { return state_anyKey == -1; }

  public final Integer getKeyStatus(int keycode) { return (Integer)hash_keyState.get(new Integer(keycode)); }
}
