/*
 * 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;
    
    
    /** 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 Entity[50];
        stocks = new Entity[50];
    }
    
    public void next()
    {
        compteur++;
        for(int i = 0; i < stocks.length ; i++)
        {
            if(stocks[i] != null && stocks[i].delay-- <= 0)
            {
                for(int j = 0; j < entities.length; j++)
                {
                    if(entities[j]==null)
                    {
                        System.out.println("stocks -> entities");
                        entities[j] = stocks[i];
                        stocks[i] = null;
                        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: LoadLevel line.charAt(0) == \'#\' \n exception :" + e.toString());
            return false;
        }
        
        return true; //pas d'erreur
    }
    
    private boolean GoToEtape(int etape)
    {
        //false = not found    
        
        try
        {
            line = this.br.readLine();
            if(line == null)
            {
                return false;
            }         
            
            this.skip_comment();
            while(Integer.decode(Character.toString(line.charAt(0))) != etape)
            {
                this.skip_comment(); //avance d'une ligne et skip les commentaires
            }
        }catch(Exception e)
        { 
            System.out.println("Erreur Level: LoadLevel line.charAt(0) == \'#\' \n exception :" + e.toString());
            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");
        }
        
        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"))
                {
                    for(int i = 0 ; i < stocks.length ; i++)
                    {
                        if(stocks[i] == null)
                        {
                            //(int type, int taille, int x, int y, Applet applet)
                            stocks[i] = new Meteor(Integer.decode(explode[6]), 
                                                    Integer.decode(explode[7]), 
                                                    Integer.decode(explode[3]), 0, applet);
                            stocks[i].delay = Integer.decode(explode[1]) * 50;
                            stocks[i].speed_y = Integer.decode(explode[4]);
                            break;
                        }
                    }
                }//Fin traitement meteor
                else if(explode[2].equals("Item"))
                {
                    for(int i = 0 ; i < stocks.length ; i++)
                    {
                        if(stocks[i] == null)
                        {
                            //(int x, int y, int type, int stype, Applet applet)
                            stocks[i] = new Item(Integer.decode(explode[3]), 
                                                    0, 
                                                    Integer.decode(explode[6]), Integer.decode(explode[7]), applet);
                            stocks[i].delay = Integer.decode(explode[1]) * 50;
                            stocks[i].speed_y = Integer.decode(explode[4]);
                            break;
                        }
                    }
                    
                }//Fin traitement Item
            }//Fin traitement Entity
            
            
            //on avance
            if(! this.skip_comment()) break;
        }
            
            
        
        return true;
    }
    
}
