package commons.ui.views.main;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;
import basics.profiler.Profiler;
import commons.ui.CommandInputReader;
import commons.ui.toolbox.GU;
public class MainView
{

int             SCR_W, SCR_H, LINE_H;
ConsoleDocument modelConsole   = new ConsoleDocument();
JTextPane       console        = GU.textPane(modelConsole);
JTable          profilerTable  = new JTable(new DefaultTableModel());
JTextArea       currentContext = GU.textarea();
JTextArea       currentError   = GU.textarea();
JTextField      currentMemory  = GU.textfield();

public MainView(Dimension size, int lh, CommandInputReader inputReader)
{
   SCR_W = (int) size.getWidth();
   SCR_H = (int) size.getHeight();
   LINE_H = lh;
   console.addKeyListener(inputReader);
}

public JSplitPane getView ()
{
   JScrollPane paneConsole = GU.scrollpane(console);
   GU.size(paneConsole, SCR_W / 2, SCR_H / 2);
   GU.defaultborder(paneConsole);
   JScrollPane paneProfiler = GU.tablepane(Profiler.COLDEF, profilerTable, null, null);
   GU.size(profilerTable, SCR_W, SCR_H / 2);
   GU.readonly(currentContext);
   GU.size(currentContext, SCR_W / 2, SCR_H / 2 - LINE_H / 2);
   JScrollPane contextPane = GU.scrollpane(currentContext);
   GU.defaultborder(contextPane);
   currentMemory.setPreferredSize(new Dimension(SCR_W / 2, LINE_H));
   currentMemory.setForeground(Color.gray);
   GU.readonly(currentError);
   currentError.setForeground(Color.red);
   GU.size(currentError, SCR_W / 2, SCR_H / 2 - LINE_H / 2);
   JScrollPane errorPane = GU.scrollpane(currentError);
   GU.defaultborder(errorPane);
   Box stateBox = GU.vbox(new Component[] { currentMemory, contextPane, errorPane });
   JSplitPane southSplitPane = GU.hsplitpane(paneConsole, stateBox);
   southSplitPane.setDividerLocation(SCR_W / 2);
   JSplitPane mainSplitPane = GU.vsplitpane(southSplitPane, paneProfiler);
   mainSplitPane.setDividerLocation(SCR_H / 2);
   return mainSplitPane;
}

public void inputFocus ()
{
   console.requestFocus();
}

public void appendMessage (String msg)
{
   String txt = console.getText();
   int offset = txt.length();
   if (txt.endsWith("\n")) modelConsole.insertString(offset, "E>" + msg + "\n");
   else modelConsole.insertString(offset, "E>\n" + msg + "\n");
   console.setCaretPosition(console.getText().length());
}

public void cls ()
{
   console.setText("");
}

public void clearInput ()
{
   String txt = console.getText();
   int idx = txt.lastIndexOf('\n') + 1;
   txt = txt.substring(0, idx);
   console.setText(txt);
   console.setCaretPosition(txt.length());
}

public String getInput ()
{
   String txt = console.getText();
   int idx = txt.lastIndexOf('\n', console.getCaretPosition() - 1);
   String input = txt.substring(idx != -1 ? idx : 0, console.getCaretPosition()); // txt.length());
   return input;
}

public void printContext (String msg)
{
   currentContext.setText(msg);
}

public void printError (String msg)
{
   currentError.setText(msg);
}

public void memoryEvent (String msg)
{
   currentMemory.setText(msg);
}

/**
 * Interface implementation for
 * @{link InspectListener}
 */
public void profilerStateEvent (DefaultTableModel t)
{
   if (t.getRowCount() > 0)
   {
      DefaultTableModel tm = (DefaultTableModel) profilerTable.getModel();
      if (tm == null)
      {
         profilerTable.setModel(t);
      }
      else
      {
         for (int i = tm.getRowCount() - 1; i >= 0; i--)
         {
            tm.removeRow(i);
         }
         for (int i = 0; i < t.getRowCount(); i++)
         {
            Object row[] = new Object[t.getColumnCount()];
            for (int c = 0; c < t.getColumnCount(); c++)
            {
               row[c] = t.getValueAt(i, c);
            }
            tm.addRow(row);
         }
      }
   }
}
}
