package com.cloudera.server.web.cmon.include;

import com.cloudera.cmon.display.Column;
import com.cloudera.server.web.cmon.JobComparisonTableModel;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import org.jamon.AbstractTemplateImpl;
import org.jamon.TemplateManager;
import org.jamon.emit.StandardEmitter;
import org.jamon.escaping.Escaping;

public class JobComparisonBarTmplImpl extends AbstractTemplateImpl
  implements JobComparisonBarTmpl.Intf
{
  private final JobComparisonTableModel tableModel;
  private final Column column;

  protected static JobComparisonBarTmpl.ImplData __jamon_setOptionalArguments(JobComparisonBarTmpl.ImplData p_implData)
  {
    return p_implData;
  }

  public JobComparisonBarTmplImpl(TemplateManager p_templateManager, JobComparisonBarTmpl.ImplData p_implData) {
    super(p_templateManager, __jamon_setOptionalArguments(p_implData));
    this.tableModel = p_implData.getTableModel();
    this.column = p_implData.getColumn();
  }

  public void renderNoFlush(Writer jamonWriter)
    throws IOException
  {
    Double distance = (Double)this.tableModel.getDistanceMap().get(this.column.getColumnName());

    if (distance == null)
    {
      jamonWriter.write("\n    <td></td>\n");
    }
    else
    {
      jamonWriter.write("\n    ");

      String extra = "";
      String polarity = distance.doubleValue() > 0.0D ? "positive" : "negative";
      distance = Double.valueOf(Math.abs(distance.doubleValue()));
      if (distance.doubleValue() > 2.0D) {
        extra = "warning";
        distance = Double.valueOf(2.0D);
      }
      int height = (int)(distance.doubleValue() * 50.0D / 2.0D);

      jamonWriter.write("<td>\n        <span class=\"StdDevGraph\">\n            <span class=\"");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf(polarity), jamonWriter);

      jamonWriter.write(" ");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf(extra), jamonWriter);

      jamonWriter.write("\" style=\"height: ");

      Escaping.STRICT_HTML.write(StandardEmitter.valueOf(height), jamonWriter);

      jamonWriter.write("%\"></span>\n            <hr/>\n        </span>\n    </td>\n");
    }

    jamonWriter.write("\n");
  }
}