/*
 * Level.java
 *
 * Created on 25 janvier 2005, 15:31
 */

package hyperion;
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;

/**
 *
 * @author CryoGen
 */
public class Level {

    private Applet applet;
    
    private String level_name;
    private URL url;
    private InputStream is;
    private InputStreamReader isr;
    private BufferedReader br;
    
    private Date date;
    private long millis;
    
    private int etape;
    private int compteur;;
    
    //public Entity [] entities;
    //private Entity [] stocks;
    public LinkedList entities;
    private LinkedList stocks;
    
    public Image bg;
    public BgSlider bgs1, bgs2;
    
    private String line;
    private String [] explode;
    
    public boolean isEnd; 
    
    /** Creates a new instance of Level */
    public Level(Applet applet, String levelname)
    {
        this.applet = applet;
        try
        {
            url = new URL(applet.getCodeBase(), levelname);
            is = url.openStream();
        }catch(Exception e) { System.out.println("Erreur Level : url/openstream");
        }
        
        isr = new InputStreamReader(is);
        br = new BufferedReader(isr);
        
        etape = 0;
        date = new Date();
        
        entities = new LinkedList();
        stocks = new LinkedList();
        isEnd = false;
    }
    
    public void next()
    {
        compteur++;
        for(int i = 0; i < stocks.size() ; i++)
        {
            if(((Entity)stocks.get(i)).delay-- <= 0)
            {
                
                    System.out.println("stocks -> entities");
                    entities.add(stocks.get(i));
                    stocks.remove(i);
                    break;
                    
                 
                    
              }
            
        }
    }
    
    private boolean skip_comment()
    {
        try
        {
            line = this.br.readLine();
            if(line == null)
            {
                return false;
            }         
            
            while(line.charAt(0) == '#')
            {
                line = this.br.readLine();
            }
        }catch(Exception e)
        { 
            System.out.println("Erreur Level: skip comment \n exception :" + e.toString());
            return false;
        }
        
        return true; //pas d'erreur
    }
    
    private boolean GoToEtape(int etape)
    {
        //false = not found    
        
        try
        {
                     
            
            if(! this.skip_comment()) return false;
            
            while(Integer.decode(Character.toString(line.charAt(0))) != etape)
            {
                if(! this.skip_comment()) return false; //avance d'une ligne et skip les commentaires
            }
        }catch(Exception e)
        { 
            System.out.println("Erreur Level: GoToEtape\n exception :");
            e.printStackTrace();
            return false;
        }
        
        return true; //pas d'erreur
        
        
    }
    
    public boolean LoadLevel(int etape)
    {
        
        Integer num_etape;
        num_etape = etape;
        
        
        
        if(! this.GoToEtape(etape))
        {
            System.out.println("Erreur Level : Etape not found");
            isEnd = true;
            return false;
        }
        
        while(Integer.decode(Character.toString(line.charAt(0))) == etape)
        {
            System.out.println(line);    //Affiche dans la console la ligne en cours
            explode = line.split(":");   //On decoupe la ligne pour pouvoir l'analyser
            
            if(explode[1].equals("Fond"))
            {
                System.out.println("Level : Fond : " + Main.codebase.toString() + " : " + explode[2]);
                this.bg = this.applet.getImage(Main.codebase, explode[2]);
                if(this.bg == null)
                {
                    System.out.println("Erreur Level : Fond : " + Main.codebase.toString() + " : " + explode[2]);
                    applet.stop();
                }
            }
            else if(explode[1].equals("BgSlider"))
            {
                if(explode[2].equals("null")) this.bgs1 = null;
                else if(explode[2].equals("PARAM"))
                {
                    //1 verif != null
                    //2 changement vitesse
                    if(this.bgs1 != null) this.bgs1.slide_y(Integer.decode(explode[3]));
                }
                else if(explode[2].equals("PREVIOUS")) {}
                else
                {
                    //chargement bgs1
                    this.bgs1 = new BgSlider(applet.getImage(applet.getCodeBase(), explode[2]), applet);
                    this.bgs1.slide_y(Integer.decode(explode[3]));               
                }
            
                if(explode[4].equals("null")) this.bgs2 = null;
                else if(explode[4].equals("PARAM"))
                {
                    //1 verif != null
                    //2 changement vitesse
                    if(this.bgs2 != null) this.bgs1.slide_y(Integer.decode(explode[5]));
                }
                else if(explode[4].equals("PREVIOUS")) {}
                else
                {
                    //chargement bgs2
                    this.bgs2 = new BgSlider(applet.getImage(applet.getCodeBase(), explode[4]), applet);
                    this.bgs2.slide_y(Integer.decode(explode[5]));               
                }
            }//Fin traitement BgSlider
            else
            {
                //Traitement des Entity
                if(explode[2].equals("Meteor"))
                {
                    
                        
                            //(int type, int taille, int x, int y, Applet applet)
                            stocks.add(new Meteor(Integer.decode(explode[6]), 
                                                    Integer.decode(explode[7]), 
                                                    Integer.decode(explode[3]), 0, applet));
                            ((Entity)stocks.getLast()).delay = Integer.decode(explode[1]) * 50;
                            ((Entity)stocks.getLast()).speed_y = Integer.decode(explode[4]);
                            
                        
                
                }//Fin traitement meteor
                else if(explode[2].equals("Item"))
                {
                    
                            //(int x, int y, int type, int stype, Applet applet)
                            stocks.add(new Item(Integer.decode(explode[3]), 
                                                    0, 
                                                    Integer.decode(explode[6]), Integer.decode(explode[7]), applet));
                            ((Entity)stocks.getLast()).delay = Integer.decode(explode[1]) * 50;
                            ((Entity)stocks.getLast()).speed_y = Integer.decode(explode[4]);
                           
                       
                    
                }//Fin traitement Item
            }//Fin traitement Entity
            
            
            //on avance
            if(! this.skip_comment()) break;
        }
            
            
        
        return true;
    }
    
    public boolean isNext()
    {
        if(this.stocks.size() == 0)
        return true;
        return false;
    }
    
}
