package jetbrains.mps.baseLanguage.unitTest.execution.tool;

/*Generated by MPS */

import javax.swing.table.TableCellRenderer;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import jetbrains.mps.nodeEditor.MPSColors;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Component;
import javax.swing.JTable;
import org.jetbrains.annotations.Nullable;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.BorderFactory;

public class StatisticsRowRenderer implements TableCellRenderer {
  private static final String INDENT = "    ";
  private final JPanel myTextPanel;
  private final JLabel myText;
  private final JLabel myAdvancedText;
  private final JLabel mySimpleField;
  private final JPanel myStatePanel;
  private final JLabel mySuccess;
  private final JLabel myError;
  private final JLabel myFailure;
  private final JLabel myAloneSuccess;
  private final JLabel myAloneError;
  private final JLabel myAloneFailure;
  public StatisticsRowRenderer() {

    myTextPanel = new JPanel(new BorderLayout());
    myText = new JLabel();
    myTextPanel.add(myText, BorderLayout.WEST);
    myAdvancedText = new JLabel();
    myAdvancedText.setForeground(MPSColors.GRAY);
    myTextPanel.add(myAdvancedText, BorderLayout.CENTER);
    mySimpleField = new JLabel("", SwingConstants.RIGHT);

    // container states

    mySuccess = new JLabel("", SwingConstants.RIGHT);
    Font font = mySuccess.getFont();
    Font boldFont = new Font(font.getName(), Font.BOLD, font.getSize());
    mySuccess.setForeground(new Color(0, 127, 0));
    mySuccess.setFont(boldFont);

    myFailure = new JLabel("", SwingConstants.RIGHT);
    myFailure.setForeground(MPSColors.RED);
    myFailure.setFont(boldFont);

    myError = new JLabel("", SwingConstants.RIGHT);
    myError.setForeground(MPSColors.RED);
    myError.setFont(boldFont);

    myStatePanel = new JPanel(new GridLayout(1, 3));
    myStatePanel.add(mySuccess, BorderLayout.WEST);
    myStatePanel.add(myFailure, BorderLayout.CENTER);
    myStatePanel.add(myError, BorderLayout.EAST);

    // single test states

    myAloneSuccess = new JLabel("Passed", SwingConstants.RIGHT);
    myAloneSuccess.setForeground(new Color(0, 127, 0));
    myAloneSuccess.setFont(boldFont);

    myAloneFailure = new JLabel("Failed", SwingConstants.RIGHT);
    myAloneFailure.setForeground(MPSColors.RED);
    myAloneFailure.setFont(boldFont);

    myAloneError = new JLabel("Error", SwingConstants.RIGHT);
    myAloneError.setForeground(MPSColors.RED);
    myAloneError.setFont(boldFont);
  }
  @Override
  public Component getTableCellRendererComponent(JTable table, @Nullable Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (value == null) {
      value = table.getModel().getValueAt(row, column);
    }
    TestStatisticsRow rowValue = (TestStatisticsRow) value;
    JComponent result = null;
    boolean keepForeground = false;
    JComponent adjustFont = null;
    boolean container = rowValue instanceof TestContainerRow;
    switch (column) {
      case 0:
        // Test
        String text = rowValue.getText();
        if (!(container)) {
          text = INDENT + text;
        }
        myText.setText(text);
        String additionalText = rowValue.getAdditionalText();
        if (additionalText != null) {
          myAdvancedText.setText(" (" + additionalText + ")");
        } else {
          myAdvancedText.setText("");
        }
        result = myTextPanel;
        adjustFont = myText;
        break;
      case 1:
        // Time elapsed
        result = setTime(rowValue.getElapsedTime());
        adjustFont = result;
        break;
      case 2:
        // Usage Delta
        result = setMemoryUsage(rowValue.getUsageDelta());
        adjustFont = result;
        break;
      case 3:
        // Usage Before
        result = setMemoryUsage(rowValue.getUsageBefore());
        adjustFont = result;
        break;
      case 4:
        // Usage After
        result = setMemoryUsage(rowValue.getUsageAfter());
        adjustFont = result;
        break;
      case 5:
        // Results
        int s = rowValue.getSuccessful();
        int f = rowValue.getFailed();
        int e = rowValue.getErrored();
        if (container) {
          mySuccess.setText((s > 0 ? ("P:" + s) : ""));
          myFailure.setText((f > 0 ? ("F:" + f) : ""));
          myError.setText((e > 0 ? ("E:" + e) : ""));
          result = myStatePanel;
        } else if ((s + f + e) >= 1) {
          if (s > 0) {
            result = myAloneSuccess;
          }
          if (f > 0) {
            result = myAloneFailure;
          }
          if (e > 0) {
            result = myAloneError;
          }
        } else {
          mySimpleField.setText("");
          result = mySimpleField;
        }
        keepForeground = true;
        break;
      default:
    }
    result.setOpaque(true);
    if (isSelected) {
      if (!(keepForeground)) {
        result.setForeground(table.getSelectionForeground());
      }
      result.setBackground(table.getSelectionBackground());
    } else {
      if (!(keepForeground)) {
        result.setForeground(table.getForeground());
      }
      result.setBackground(table.getBackground());
    }
    if (hasFocus) {
      result.setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
    } else {
      result.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    }
    result.setEnabled(table.isEnabled());
    if (adjustFont != null) {
      if (container) {
        Font font = table.getFont();
        adjustFont.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
      } else {
        adjustFont.setFont(table.getFont());
      }
    }
    return result;
  }
  private JLabel setTime(long time) {
    String text = "";
    if (time >= 0) {
      text = String.format("%.3f s", time / 1000.0);
    }
    mySimpleField.setText(text);
    return mySimpleField;
  }
  private JLabel setMemoryUsage(long usage) {
    String s = String.format("%d Kb", usage / 1024);
    mySimpleField.setText(s);
    return mySimpleField;
  }
}
