
package tmcsim.cadsimulator.viewer;

import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Scanner;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * ConfigStatusPanel displays the content of configuration files.
 *
 * @author Drew Miller
 * @author jdalbey
 */
public class ConfigStatusTab extends javax.swing.JPanel
{
    /** Root config directory name */
    private String configDir;
    
    /**
     * Creates new form 
     */
    public ConfigStatusTab()
    {
        initComponents();
        if (System.getProperty("CONFIG_DIR") == null)
        {
            System.setProperty("CONFIG_DIR", "config");
        }
        configDir = System.getProperty("CONFIG_DIR");
        setupComboBox();
        setupTextView();
    }

    private String getFileContent(Path pathToFile) 
    {
        FileInputStream fis = null;
        try
        {
            //Read a file into a single string.
            fis = new FileInputStream(pathToFile.toString());
            Scanner s = new Scanner(fis).useDelimiter("\\A");
            return s.next();
        }
        catch (FileNotFoundException ex)
        {
            return "Error: config file not found.";
        }
        
    }

    private Path getPathToFile(String fileName)
    {
        Path p = FileSystems.getDefault().getPath(configDir, fileName);
        return p;
    }
    /** Refresh the display using the given config file */
    private void updateDisplay(Path pathToFileName)
    {
        view.setText(getFileContent(pathToFileName));
        view.setCaretPosition(view.getDocument().getLength());
    }

    /** Combo Box with names of config files for user to select */
    private void setupComboBox()
    {
        selectMenu.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                JComboBox src = (JComboBox) e.getSource();
                Path p = getPathToFile(src.getSelectedItem().toString());
                updateDisplay(p);
            }

        });
    }

    private void setupTextView()
    {
        view.setEditable(false);
        view.setLineWrap(true);
        view.setWrapStyleWord(true);
        // Initialize with first config file in list
        Path path = getPathToFile(selectMenu.getItemAt(0));
        view.setText(getFileContent(path));
    }

     /**
     * Local main for unit testing. 
     *
     * @param args not used
     */
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Config Status Panel Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1050, 450);
        ConfigStatusTab pnl = new ConfigStatusTab();
        frame.add(pnl);
        frame.setVisible(true);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        selectMenu = new javax.swing.JComboBox<>();
        jScrollPane1 = new javax.swing.JScrollPane();
        view = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());

        selectMenu.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "cad_simulator_config.properties", "cad_simulator_media_config.properties", "cad_simulator_paramics_config.properties", "cad_simulator_smoketest_config.properties", "traffic_model_config.properties" }));
        add(selectMenu, java.awt.BorderLayout.PAGE_START);

        view.setColumns(20);
        view.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
        view.setRows(5);
        jScrollPane1.setViewportView(view);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JComboBox<String> selectMenu;
    private javax.swing.JTextArea view;
    // End of variables declaration//GEN-END:variables
}
