import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.Writer;
import java.text.*;

import antlr.CommonAST;

public class Main {
    static boolean DEBUG = false;
    
    public static void main(String[] args) {
        try {
            SfalLexer lexer = null;
            
            if (args.length != 2) {
                throw new Exception("Usage: Movie name, sfal file");
            } 
            else {
                FileInputStream filename = new FileInputStream(args[1]);
                DataInputStream input = new DataInputStream(filename);
                lexer = new SfalLexer(new DataInputStream(input));
            }
            
            SfalParser parser = new SfalParser(lexer);
            parser.program();
            
            CommonAST tree = (CommonAST) parser.getAST();
            
            if (DEBUG) {
                Writer writer = new OutputStreamWriter(System.out);
                tree.xmlSerialize(writer);
                writer.write("\n");
                writer.flush();
            }
            
            SfalWalker walker = new SfalWalker();
            walker.program(parser.getAST());
            
            StringBuffer headerBuffer = new StringBuffer();
            
            headerBuffer.append("package {")
            .append("import flash.display.Sprite;")
            .append("import flash.text.TextField;")
            .append("import flash.events.Event;")
            .append("public class ").append(args[0])
            .append(" extends Sprite {")
            .append("public function ").append(args[0])
            .append("() {");
            
            walker.programBuffer.insert(0, headerBuffer.toString());
            
            String program = walker.programBuffer.toString();
            
            char[] chars = program.toCharArray();
            
            // Pretty print the ActionScript file
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < chars.length; i++) {
                char c = chars[i];
                if (c == '\n') continue;
                
                sb.append(c);
                
                if (c == ';' || c == '{' || c == '}') 
                    sb.append('\n');
            }
            
            BufferedReader in = new BufferedReader(new StringReader(sb.toString()));
            BufferedWriter out = new BufferedWriter(new FileWriter(args[0] + ".as"));
            
            String tabs = "";
            
            String line = null;
            try {
                while ((line = in.readLine()) != null) {
                    line = line.trim();
                    
                    if (line.length() == 0) continue;
                    
                    if (line.charAt(line.length()-1) == '}') {
                    	tabs=tabs.substring(0,tabs.length()-1);
                    }
                    
                    out.write(tabs); out.write(line); out.write('\n');
                    
                    if (line.charAt(line.length()-1) == '{') {
                        tabs += "\t";
                    }
                }            
            }
            catch (IOException e) {
                System.err.println(e);
            }
            finally {
                try {
                    if (out != null) {
                        out.flush();
                        out.close();
                    }
                }
                catch (IOException e) {}
            }
            
            in = new BufferedReader(new FileReader("embedder.txt"));
            out = new BufferedWriter(new FileWriter(args[0] + ".html"));
            
            try {
            	out.write("<!-- AUTO-GENERATED -->\n");
            	while ((line = in.readLine()) != null) {
            		out.write(MessageFormat.format(line,args));
            		out.write("\n");
            	}
            	out.flush();
            	out.close();
            }
            catch (IOException e) {
            	System.err.println(e);
            }
            finally {
                try {
                    if (out != null) {
                        out.flush();
                        out.close();
                    }
                }
                catch (IOException e) {}	
            }
        }
        catch (Exception e) {
            System.err.println("MAIN: " + e);
            e.printStackTrace();
        }
    }
}
