package istlab.KisoJikken;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.FontUIResource;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rtextarea.RTextScrollPane;

public class Editor extends JFrame implements ActionListener {
    File file;
    RSyntaxTextArea textArea;
    JPanel topP;
    JSlider fontSizeJS;

    public Editor(File f) {
        file = f;

        // JPanel contentPane = new JPanel(new BorderLayout());
        textArea = new RSyntaxTextArea(20, 40);
        textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
        textArea.setCodeFoldingEnabled(true);
        textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, 16));
        getContentPane().add(new RTextScrollPane(textArea));

        try {
            setTitle(file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        setSize(new Dimension(800, 400));// pack();
        setLocationRelativeTo(Launcher.theapp);
        setLocation(Launcher.theapp.getWidth(), Launcher.theapp.file2editor.size() * 40);
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        load();

        topP = new JPanel();
        topP.add(new JLabel("size:"));
        fontSizeJS = new JSlider(10, 50, 16);
        topP.add(fontSizeJS);
        fontSizeJS.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int size = fontSizeJS.getValue();
                textArea.setFont(new FontUIResource("sansserif", Font.PLAIN, size));

            }
        });
        JButton saveB = new JButton("Save");
        saveB.addActionListener(this);
        topP.add(saveB);

        JButton loadB = new JButton("Reload");
        loadB.addActionListener(this);
        topP.add(loadB);

        getContentPane().add(topP, BorderLayout.NORTH);

        setVisible(true);
    }

    public void load() {
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        textArea.setText(sb.toString());
    }

    public void save(){
        String src = textArea.getText();
        System.out.println(src);
        try {
            PrintWriter out = new PrintWriter(file.getAbsolutePath());
            out.println(src);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Save")){
            save();
        } else if (e.getActionCommand().equals("Reload")){
            load();
        }
        
    }
}
