import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import Collection.*;
import project.*;

public class Game extends Applet implements Runnable, ActionListener {
  
  private Button forward, back, drop, attack, eat, pick, quit, start, useWeapon;
  private Panel top, bottom;
  private ManageText text;
  
  Thread animation; 
  
  private Player1 player;
  private PlayerBattleMode battle;
  private ItemEncounter item;
  private MonsterRevenge revenge;
  private boolean started;
  
  public void init() {
    
	text = new ManageText();
	setLayout( new BorderLayout() );
    setBackground( Color.black );
    setName( "MonsterRevenge" );
    setVisible ( true );
	
	top  = new Panel();
	bottom = new Panel();
    forward = makeButton( "Walk Forward", this );
	back = makeButton( "Walk Back", this );
	attack  = makeButton( "Attack",  this );
	useWeapon  = makeButton( "Use Weapon",  this );
	start  = makeButton( "Start Game",  this );
	quit  = makeButton( "Quit Game",  this );
	eat  = makeButton( "Eat",  this );
    drop = makeButton( "Drop Weapon", this );
	pick  = makeButton( "Pick",  this );
	
	add( top, BorderLayout.NORTH );
	add( bottom, BorderLayout.SOUTH );
	add( text.getText(), BorderLayout.CENTER );
	top.add( start );
	top.add( quit );
    bottom.add( forward );
	bottom.add( back );
    bottom.add( attack );
	bottom.add( useWeapon );
    bottom.add( eat );
	bottom.add( drop );
	bottom.add( pick );
	quit.disable();
	
	text.println ("~~MonsterRevenge~~ the avenue of the Deads");
	text.println ("Press <Start Game> to start");
  }
  
  public void start() { }
  
  public void update(Graphics g) { paint(g); }
  
  public void paint(Graphics page) {}
  
  public void initGame() {
    player = new Player1(text);
	item = new ItemEncounter ( player );
    battle = new PlayerBattleMode ( player, item );	
	revenge = new MonsterRevenge (player, text);
  } //method initGame
  
  public void run() { 
    revenge.start();
    while (revenge.getGameState() && started) {
	  try { revenge.update();
	  } catch (StageClearException exception) {
	    text.println ("~~As you existed..The sun shines on the peaceful land...");
		text.println ("you've walked out the world of evil..Alive");
		text.println ("while all the horror and violent just seemed like a nightmare~~");
		text.println ("<<Game Cleared!!>>");
	    stop();
	  }
	} 
	stop();
  } //method run 
  
  public void stop() {
    if (animation!=null) {
	  started = false;
	  quit.disable();
	  start.enable();
	  animation.stop();
    } //if
  } //method stop
  
  public Button makeButton( String button_name, ActionListener listener ) {
    Button b = new Button( button_name );
    b.addActionListener( listener );	 
    return b;
  }//makeButton
  
  public void actionPerformed(ActionEvent e ) {
	if( e.getSource() == start ) {  
	  animation = new Thread(this);
	  started = true;
	  start.disable();
	  quit.enable();
	  initGame();
	  animation.start();
	}
	else if( e.getSource() == quit ) { 
	  stop();
	  text.println ("Thanks for playing!");
	  text.println ("~~~MonsterRevenge~~~");
	  text.println ("Creadits:");
	  text.println ("Programmed by Nai-Shou Kuo");
	}
    else if( e.getSource() == forward ) { 
	  try {
	    if (animation!=null && started) player.moveFwd(); 
	  } catch (inBattleException exception) {
	   text.println ("The monster blocked your way..");
	  } //catch
	}
	else if( e.getSource() == back ) { 
	  try { 
	  if (animation!=null && started) player.moveBk(); 
	  } catch ( NoPassageException exception ) {
	   text.println ("There are no way back!"); 
	  }
	}
	else if( e.getSource() == useWeapon ) { 
	  try { 
	    if (animation!=null && started) {
	      battle.EnterBattleMode();
	      battle.WeaponAttack();
		  revenge.AttackPlayer();
		} //if
	  } catch (NullTargetException exception) {
	   text.println ("There seems to be no apparent target!"); }
	}
	else if( e.getSource() == attack ) { 
	  try {
	    if (animation!=null && started) {
	      battle.EnterBattleMode();
	      battle.Attack();
		  revenge.AttackPlayer();
		} //if
	  } catch (NullTargetException exception) { 
	    text.println ("There seems to be no apparent enemy!"); 
	  }
	}
	else if( e.getSource() == eat ) { 
	  try {
	    if (animation!=null && started) { player.eat(); }
	  } catch ( NoFoodToEatException exception) { 
	   text.println ("Are you that hungry??"); 
	  }
	}
	else if( e.getSource() == pick ) { 
	  try { 
	    if (animation!=null && started){ item.PickUpWeapon(); }
	   } catch ( NoWeaponException exception) { 
	    text.println ("No items to pick up at the moment"); 
	   }
	}
	else if( e.getSource() == drop ) { 
	  try { 
	    if (animation!=null && started) {item.DropWeapon(); }  
	  } catch (NoWeaponException exception) { 
	   text.println ("you don't have any weapons in your WeaponBox!"); 
	  }
    }
  }
}