/*
 *  Copyright 2006-2009 Mikhail Kryshen
 *
 *  This file is part of Tema.
 *
 *  Tema is free software: you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  Tema is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the 
 *  GNU Lesser General Public License along with Tema.  
 *  If not, see <http://www.gnu.org/licenses/>.
 */

package kryshen.tema.demo;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URL;
import kryshen.tema.TemplateParser;
import kryshen.tema.io.TemplateReader;

/**
 * Tema demonstation console.
 */
public class DemoFrame extends Frame {
    private static final Font TEXT_FONT =
            new Font("monospaced", Font.PLAIN, 12);
    
    private TextArea input;
    private TextArea output;
    private TextArea error;
    
    private String sample;
    
    public DemoFrame() {
        super("Tema demo console");
        
        GridBagLayout gbl = new GridBagLayout();
        
        GridBagConstraints c = new GridBagConstraints();
        
        setLayout(gbl);
        
        input = new TextArea(28, 50);
        input.setEditable(true);
        input.setFont(TEXT_FONT);
        
        output = new TextArea(28, 50);
        output.setEditable(false);
        output.setFont(TEXT_FONT);
        
        error = new TextArea(5, 90);
        error.setEditable(false);
        error.setFont(TEXT_FONT);
        
        Label l;
        
        c.insets = new Insets(3, 3, 3, 3);
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.RELATIVE;
        
        l = new Label("Enter your code and press \"Process\".");
        
        gbl.setConstraints(l, c);
        add(l);
        
        Panel buttons = new Panel();
        buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
        
        Button process = new Button("Process");
        Button clear = new Button("Clear");
        Button reset = new Button("Reset");
        Button close = new Button("Close");
        
        buttons.add(process);
        buttons.add(clear);
        buttons.add(reset);
        buttons.add(close);
        
        c.gridwidth = GridBagConstraints.REMAINDER;
        
        gbl.setConstraints(buttons, c);
        add(buttons);
        
        c.fill = GridBagConstraints.BOTH;
        c.anchor = GridBagConstraints.CENTER;
        c.gridwidth = GridBagConstraints.RELATIVE;
        
        l = new Label("Input:");
        
        gbl.setConstraints(l, c);
        add(l);
        
        c.gridwidth = GridBagConstraints.REMAINDER;
        
        l = new Label("Output:");
        
        gbl.setConstraints(l, c);
        add(l);
        
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.weightx = 1.0;
        c.weighty = 1.0;
        
        gbl.setConstraints(input, c);
        add(input);
        
        c.gridwidth = GridBagConstraints.REMAINDER;
        
        gbl.setConstraints(output, c);
        add(output);
        
        c.weightx = 0.0;
        c.weighty = 0.0;
        
        l = new Label("Errors:");
        
        gbl.setConstraints(l, c);
        add(l);
        
        c.weightx = 1.0;
        c.weighty = 0.25;
        
        gbl.setConstraints(error, c);
        add(error);
        
        process.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                process();
            }
        });
        
        clear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                error.setText("");
                output.setText("");
                input.setText("");
                input.requestFocus();
            }
        });
        
        reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                error.setText("");
                output.setText("");
                input.setText(sample);
            }
        });
        
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
        
        URL source = getClass().getResource("demo.template");
        StringBuffer buffer = new StringBuffer();
        
        try {
            Reader in = new BufferedReader
                    (new InputStreamReader(source.openStream(), "UTF-8"));
            
            for (int ch = in.read(); ch >= 0; ch = in.read()) {
                buffer.append((char)ch);
            }
            
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        sample = buffer.toString();
        
        input.setText(sample);
    }
    
    private void process() {
        String s = input.getText();
        
        TemplateReader tr = new TemplateReader(new StringReader(s), "input");
        TemplateParser tp = new TemplateParser();
        StringWriter sw = new StringWriter();
        
        error.setText("");
        
        try {
            tp.parse(tr, sw);
        } catch (Exception e) {
            error.setText(e.toString());
            e.printStackTrace();
        }
        
        output.setText(sw.toString());
    }
}