package net.sf.jasperreports.engine.fill;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import net.sf.jasperreports.engine.JRAlignment;
import net.sf.jasperreports.engine.JRBox;
import net.sf.jasperreports.engine.JRBoxContainer;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRCommonImage;
import net.sf.jasperreports.engine.JRDefaultStyleProvider;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRGraphicElement;
import net.sf.jasperreports.engine.JRHyperlinkHelper;
import net.sf.jasperreports.engine.JRImage;
import net.sf.jasperreports.engine.JRLineBox;
import net.sf.jasperreports.engine.JROrigin;
import net.sf.jasperreports.engine.JRPen;
import net.sf.jasperreports.engine.JRPenContainer;
import net.sf.jasperreports.engine.base.JRBaseLineBox;
import net.sf.jasperreports.engine.base.JRBasePen;
import net.sf.jasperreports.engine.util.JRBoxUtil;
import net.sf.jasperreports.engine.util.JRPenUtil;
import net.sf.jasperreports.engine.util.JRStyleResolver;
import net.sf.jasperreports.engine.util.LineBoxWrapper;

public class JRTemplateImage extends JRTemplateGraphicElement implements JRAlignment, JRBox, JRCommonImage {
  private static final long serialVersionUID = 10200L;
  
  private Byte scaleImage = null;
  
  private Boolean isUsingCache = Boolean.TRUE;
  
  private Byte horizontalAlignment = null;
  
  private Byte verticalAlignment = null;
  
  protected boolean isLazy = false;
  
  private byte onErrorType = 1;
  
  private byte hyperlinkType = 0;
  
  private String linkType;
  
  private byte hyperlinkTarget = 1;
  
  private JRLineBox lineBox = null;
  
  private Byte border;
  
  private Byte topBorder;
  
  private Byte leftBorder;
  
  private Byte bottomBorder;
  
  private Byte rightBorder;
  
  private Color borderColor;
  
  private Color topBorderColor;
  
  private Color leftBorderColor;
  
  private Color bottomBorderColor;
  
  private Color rightBorderColor;
  
  private Integer padding;
  
  private Integer topPadding;
  
  private Integer leftPadding;
  
  private Integer bottomPadding;
  
  private Integer rightPadding;
  
  protected JRTemplateImage(JROrigin origin, JRDefaultStyleProvider defaultStyleProvider, JRImage image) {
    super(origin, defaultStyleProvider);
    this.border = null;
    this.topBorder = null;
    this.leftBorder = null;
    this.bottomBorder = null;
    this.rightBorder = null;
    this.borderColor = null;
    this.topBorderColor = null;
    this.leftBorderColor = null;
    this.bottomBorderColor = null;
    this.rightBorderColor = null;
    this.topPadding = null;
    this.leftPadding = null;
    this.bottomPadding = null;
    this.rightPadding = null;
    setImage(image);
  }
  
  protected JRTemplateImage(JROrigin origin, JRDefaultStyleProvider defaultStyleProvider, JRChart chart) {
    super(origin, defaultStyleProvider);
    this.border = null;
    this.topBorder = null;
    this.leftBorder = null;
    this.bottomBorder = null;
    this.rightBorder = null;
    this.borderColor = null;
    this.topBorderColor = null;
    this.leftBorderColor = null;
    this.bottomBorderColor = null;
    this.rightBorderColor = null;
    this.topPadding = null;
    this.leftPadding = null;
    this.bottomPadding = null;
    this.rightPadding = null;
    setChart(chart);
  }
  
  protected void setImage(JRImage image) {
    setGraphicElement((JRGraphicElement)image);
    this.lineBox = image.getLineBox().clone((JRBoxContainer)this);
    setScaleImage(image.getScaleImage());
    setUsingCache(image.isUsingCache());
    setHorizontalAlignment(image.getHorizontalAlignment());
    setVerticalAlignment(image.getVerticalAlignment());
    setLazy(image.isLazy());
    setOnErrorType(image.getOnErrorType());
    setLinkType(image.getLinkType());
    setHyperlinkTarget(image.getHyperlinkTarget());
  }
  
  protected void setChart(JRChart chart) {
    setElement((JRElement)chart);
    this.linePen = (JRPen)new JRBasePen((JRPenContainer)this);
    getLinePen().setLineWidth(0.0F);
    setFill((byte)1);
    this.lineBox = chart.getLineBox().clone((JRBoxContainer)this);
    setLinkType(chart.getLinkType());
    setHyperlinkTarget(chart.getHyperlinkTarget());
  }
  
  public JRBox getBox() {
    return (JRBox)new LineBoxWrapper(getLineBox());
  }
  
  public JRLineBox getLineBox() {
    return this.lineBox;
  }
  
  public void setBox(JRBox box) {
    JRBoxUtil.setBoxToLineBox(box, this.lineBox);
  }
  
  public byte getMode() {
    return JRStyleResolver.getMode(this, (byte)2);
  }
  
  public byte getScaleImage() {
    return JRStyleResolver.getScaleImage(this);
  }
  
  public Byte getOwnScaleImage() {
    return this.scaleImage;
  }
  
  protected void setScaleImage(byte scaleImage) {
    this.scaleImage = new Byte(scaleImage);
  }
  
  protected void setScaleImage(Byte scaleImage) {
    this.scaleImage = scaleImage;
  }
  
  public boolean isUsingCache() {
    return (this.isUsingCache == null) ? true : this.isUsingCache.booleanValue();
  }
  
  public void setUsingCache(boolean isUsingCache) {
    this.isUsingCache = isUsingCache ? Boolean.TRUE : Boolean.FALSE;
  }
  
  public byte getHorizontalAlignment() {
    return JRStyleResolver.getHorizontalAlignment(this);
  }
  
  public Byte getOwnHorizontalAlignment() {
    return this.horizontalAlignment;
  }
  
  public void setHorizontalAlignment(byte horizontalAlignment) {
    this.horizontalAlignment = new Byte(horizontalAlignment);
  }
  
  public void setHorizontalAlignment(Byte horizontalAlignment) {
    this.horizontalAlignment = horizontalAlignment;
  }
  
  public byte getVerticalAlignment() {
    return JRStyleResolver.getVerticalAlignment(this);
  }
  
  public Byte getOwnVerticalAlignment() {
    return this.verticalAlignment;
  }
  
  public void setVerticalAlignment(byte verticalAlignment) {
    this.verticalAlignment = new Byte(verticalAlignment);
  }
  
  public void setVerticalAlignment(Byte verticalAlignment) {
    this.verticalAlignment = verticalAlignment;
  }
  
  public boolean isLazy() {
    return this.isLazy;
  }
  
  public void setLazy(boolean isLazy) {
    this.isLazy = isLazy;
  }
  
  public byte getOnErrorType() {
    return this.onErrorType;
  }
  
  public void setOnErrorType(byte onErrorType) {
    this.onErrorType = onErrorType;
  }
  
  public byte getHyperlinkType() {
    return JRHyperlinkHelper.getHyperlinkType(getLinkType());
  }
  
  protected void setHyperlinkType(byte hyperlinkType) {
    setLinkType(JRHyperlinkHelper.getLinkType(hyperlinkType));
  }
  
  public byte getHyperlinkTarget() {
    return this.hyperlinkTarget;
  }
  
  protected void setHyperlinkTarget(byte hyperlinkTarget) {
    this.hyperlinkTarget = hyperlinkTarget;
  }
  
  public String getLinkType() {
    return this.linkType;
  }
  
  public void setLinkType(String linkType) {
    this.linkType = linkType;
  }
  
  protected void normalizeLinkType() {
    if (this.linkType == null)
      this.linkType = JRHyperlinkHelper.getLinkType(this.hyperlinkType); 
    this.hyperlinkType = 0;
  }
  
  public Float getDefaultLineWidth() {
    return JRPen.LINE_WIDTH_0;
  }
  
  public byte getBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getPen());
  }
  
  public Byte getOwnBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getPen());
  }
  
  public void setBorder(byte border) {
    JRPenUtil.setLinePenFromPen(border, (JRPen)this.lineBox.getPen());
  }
  
  public void setBorder(Byte border) {
    JRPenUtil.setLinePenFromPen(border, (JRPen)this.lineBox.getPen());
  }
  
  public Color getBorderColor() {
    return this.lineBox.getPen().getLineColor();
  }
  
  public Color getOwnBorderColor() {
    return this.lineBox.getPen().getOwnLineColor();
  }
  
  public void setBorderColor(Color borderColor) {
    this.lineBox.getPen().setLineColor(borderColor);
  }
  
  public int getPadding() {
    return this.lineBox.getPadding().intValue();
  }
  
  public Integer getOwnPadding() {
    return this.lineBox.getOwnPadding();
  }
  
  public void setPadding(int padding) {
    this.lineBox.setPadding(padding);
  }
  
  public void setPadding(Integer padding) {
    this.lineBox.setPadding(padding);
  }
  
  public byte getTopBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getTopPen());
  }
  
  public Byte getOwnTopBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getTopPen());
  }
  
  public void setTopBorder(byte topBorder) {
    JRPenUtil.setLinePenFromPen(topBorder, (JRPen)this.lineBox.getTopPen());
  }
  
  public void setTopBorder(Byte topBorder) {
    JRPenUtil.setLinePenFromPen(topBorder, (JRPen)this.lineBox.getTopPen());
  }
  
  public Color getTopBorderColor() {
    return this.lineBox.getTopPen().getLineColor();
  }
  
  public Color getOwnTopBorderColor() {
    return this.lineBox.getTopPen().getOwnLineColor();
  }
  
  public void setTopBorderColor(Color topBorderColor) {
    this.lineBox.getTopPen().setLineColor(topBorderColor);
  }
  
  public int getTopPadding() {
    return this.lineBox.getTopPadding().intValue();
  }
  
  public Integer getOwnTopPadding() {
    return this.lineBox.getOwnTopPadding();
  }
  
  public void setTopPadding(int topPadding) {
    this.lineBox.setTopPadding(topPadding);
  }
  
  public void setTopPadding(Integer topPadding) {
    this.lineBox.setTopPadding(topPadding);
  }
  
  public byte getLeftBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getLeftPen());
  }
  
  public Byte getOwnLeftBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getLeftPen());
  }
  
  public void setLeftBorder(byte leftBorder) {
    JRPenUtil.setLinePenFromPen(leftBorder, (JRPen)this.lineBox.getLeftPen());
  }
  
  public void setLeftBorder(Byte leftBorder) {
    JRPenUtil.setLinePenFromPen(leftBorder, (JRPen)this.lineBox.getLeftPen());
  }
  
  public Color getLeftBorderColor() {
    return this.lineBox.getLeftPen().getLineColor();
  }
  
  public Color getOwnLeftBorderColor() {
    return this.lineBox.getLeftPen().getOwnLineColor();
  }
  
  public void setLeftBorderColor(Color leftBorderColor) {
    this.lineBox.getLeftPen().setLineColor(leftBorderColor);
  }
  
  public int getLeftPadding() {
    return this.lineBox.getLeftPadding().intValue();
  }
  
  public Integer getOwnLeftPadding() {
    return this.lineBox.getOwnLeftPadding();
  }
  
  public void setLeftPadding(int leftPadding) {
    this.lineBox.setLeftPadding(leftPadding);
  }
  
  public void setLeftPadding(Integer leftPadding) {
    this.lineBox.setLeftPadding(leftPadding);
  }
  
  public byte getBottomBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getBottomPen());
  }
  
  public Byte getOwnBottomBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getBottomPen());
  }
  
  public void setBottomBorder(byte bottomBorder) {
    JRPenUtil.setLinePenFromPen(bottomBorder, (JRPen)this.lineBox.getBottomPen());
  }
  
  public void setBottomBorder(Byte bottomBorder) {
    JRPenUtil.setLinePenFromPen(bottomBorder, (JRPen)this.lineBox.getBottomPen());
  }
  
  public Color getBottomBorderColor() {
    return this.lineBox.getBottomPen().getLineColor();
  }
  
  public Color getOwnBottomBorderColor() {
    return this.lineBox.getBottomPen().getOwnLineColor();
  }
  
  public void setBottomBorderColor(Color bottomBorderColor) {
    this.lineBox.getBottomPen().setLineColor(bottomBorderColor);
  }
  
  public int getBottomPadding() {
    return this.lineBox.getBottomPadding().intValue();
  }
  
  public Integer getOwnBottomPadding() {
    return this.lineBox.getOwnBottomPadding();
  }
  
  public void setBottomPadding(int bottomPadding) {
    this.lineBox.setBottomPadding(bottomPadding);
  }
  
  public void setBottomPadding(Integer bottomPadding) {
    this.lineBox.setBottomPadding(bottomPadding);
  }
  
  public byte getRightBorder() {
    return JRPenUtil.getPenFromLinePen((JRPen)this.lineBox.getRightPen());
  }
  
  public Byte getOwnRightBorder() {
    return JRPenUtil.getOwnPenFromLinePen((JRPen)this.lineBox.getRightPen());
  }
  
  public void setRightBorder(byte rightBorder) {
    JRPenUtil.setLinePenFromPen(rightBorder, (JRPen)this.lineBox.getRightPen());
  }
  
  public void setRightBorder(Byte rightBorder) {
    JRPenUtil.setLinePenFromPen(rightBorder, (JRPen)this.lineBox.getRightPen());
  }
  
  public Color getRightBorderColor() {
    return this.lineBox.getRightPen().getLineColor();
  }
  
  public Color getOwnRightBorderColor() {
    return this.lineBox.getRightPen().getOwnLineColor();
  }
  
  public void setRightBorderColor(Color rightBorderColor) {
    this.lineBox.getRightPen().setLineColor(rightBorderColor);
  }
  
  public int getRightPadding() {
    return this.lineBox.getRightPadding().intValue();
  }
  
  public Integer getOwnRightPadding() {
    return this.lineBox.getOwnRightPadding();
  }
  
  public void setRightPadding(int rightPadding) {
    this.lineBox.setRightPadding(rightPadding);
  }
  
  public void setRightPadding(Integer rightPadding) {
    this.lineBox.setRightPadding(rightPadding);
  }
  
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if (this.lineBox == null) {
      this.lineBox = (JRLineBox)new JRBaseLineBox((JRBoxContainer)this);
      JRBoxUtil.setToBox(this.border, this.topBorder, this.leftBorder, this.bottomBorder, this.rightBorder, this.borderColor, this.topBorderColor, this.leftBorderColor, this.bottomBorderColor, this.rightBorderColor, this.padding, this.topPadding, this.leftPadding, this.bottomPadding, this.rightPadding, this.lineBox);
      this.border = null;
      this.topBorder = null;
      this.leftBorder = null;
      this.bottomBorder = null;
      this.rightBorder = null;
      this.borderColor = null;
      this.topBorderColor = null;
      this.leftBorderColor = null;
      this.bottomBorderColor = null;
      this.rightBorderColor = null;
      this.padding = null;
      this.topPadding = null;
      this.leftPadding = null;
      this.bottomPadding = null;
      this.rightPadding = null;
    } 
    normalizeLinkType();
  }
}
