package jxl.write.biff;

import common.Logger;
import jxl.biff.DisplayFormat;
import jxl.biff.FontRecord;
import jxl.biff.XFRecord;
import jxl.format.Font;
import jxl.write.DateFormat;
import jxl.write.DateFormats;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;

class Styles {
  private static Logger logger = Logger.getLogger(Styles.class);
  
  private WritableFont arial10pt = null;
  
  private WritableFont hyperlinkFont = null;
  
  private WritableCellFormat normalStyle = null;
  
  private WritableCellFormat hyperlinkStyle = null;
  
  private WritableCellFormat hiddenStyle = null;
  
  private WritableCellFormat defaultDateFormat;
  
  private synchronized void initNormalStyle() {
    this.normalStyle = new WritableCellFormat(getArial10Pt(), NumberFormats.DEFAULT);
    this.normalStyle.setFont((FontRecord)getArial10Pt());
  }
  
  public WritableCellFormat getNormalStyle() {
    if (this.normalStyle == null)
      initNormalStyle(); 
    return this.normalStyle;
  }
  
  private synchronized void initHiddenStyle() {
    this.hiddenStyle = new WritableCellFormat(getArial10Pt(), (DisplayFormat)new DateFormat(";;;"));
  }
  
  public WritableCellFormat getHiddenStyle() {
    if (this.hiddenStyle == null)
      initHiddenStyle(); 
    return this.hiddenStyle;
  }
  
  private synchronized void initHyperlinkStyle() {
    this.hyperlinkStyle = new WritableCellFormat(getHyperlinkFont(), NumberFormats.DEFAULT);
  }
  
  public WritableCellFormat getHyperlinkStyle() {
    if (this.hyperlinkStyle == null)
      initHyperlinkStyle(); 
    return this.hyperlinkStyle;
  }
  
  private synchronized void initArial10Pt() {
    this.arial10pt = new WritableFont((Font)WritableWorkbook.ARIAL_10_PT);
  }
  
  public WritableFont getArial10Pt() {
    if (this.arial10pt == null)
      initArial10Pt(); 
    return this.arial10pt;
  }
  
  private synchronized void initHyperlinkFont() {
    this.hyperlinkFont = new WritableFont((Font)WritableWorkbook.HYPERLINK_FONT);
  }
  
  public WritableFont getHyperlinkFont() {
    if (this.hyperlinkFont == null)
      initHyperlinkFont(); 
    return this.hyperlinkFont;
  }
  
  private synchronized void initDefaultDateFormat() {
    this.defaultDateFormat = new WritableCellFormat(DateFormats.DEFAULT);
  }
  
  public WritableCellFormat getDefaultDateFormat() {
    if (this.defaultDateFormat == null)
      initDefaultDateFormat(); 
    return this.defaultDateFormat;
  }
  
  public XFRecord getFormat(XFRecord wf) {
    WritableCellFormat writableCellFormat;
    XFRecord format = wf;
    if (format == WritableWorkbook.NORMAL_STYLE) {
      writableCellFormat = getNormalStyle();
    } else if (writableCellFormat == WritableWorkbook.HYPERLINK_STYLE) {
      writableCellFormat = getHyperlinkStyle();
    } else if (writableCellFormat == WritableWorkbook.HIDDEN_STYLE) {
      writableCellFormat = getHiddenStyle();
    } else if (writableCellFormat == DateRecord.defaultDateFormat) {
      writableCellFormat = getDefaultDateFormat();
    } 
    if (writableCellFormat.getFont() == WritableWorkbook.ARIAL_10_PT) {
      writableCellFormat.setFont((FontRecord)getArial10Pt());
    } else if (writableCellFormat.getFont() == WritableWorkbook.HYPERLINK_FONT) {
      writableCellFormat.setFont((FontRecord)getHyperlinkFont());
    } 
    return (XFRecord)writableCellFormat;
  }
}
