/**
 * <p>MyGUIStyle is a simple collection of colours and a font(s) that is used to
 * give a constant look and feel to all elements drawn through processing.</p>
 * <p>Version: 0.01<br/>
 * Created by John Beech (processing@mkv25.net)<br/>
 * Website: http://mkv25.net/</p>
 * <p>Current features include:
 * <ul>
 * <li>Specify font, font size</li>
 * <li>Set colours for:
 *   <ul>
 *   <li>Background</li>
 *   <li>Button Face</li>
 *   <li>Button Highlight</li>
 *   <li>Button Shadow</li>
 *   <li>Button Text</li>
 *   <li>General Face</li>
 *   <li>General Highlight</li>
 *   <li>General Shadow</li>
 *   <li>General Disabled</li>
 *   <li>Highlighted Text</li>
 *   <li>Scroll Bar Background</li>
 *   </ul>
 * <li>
 * </ul></p>
 */

public class MyGUIStyle {
  // Variables (public as they will need to be referenced easily)
  public PFont font;
  public int fontSize = 12, strokeWeight = 1, padding = 1;
  private float transparency = 255;
  public color background, buttonFace, buttonHighlight, buttonShadow, buttonText, icon, highlight, face, shadow, disabled, highlightText, scrollBar;
  
  // Constructors
  // Default, sets up default colours
  public MyGUIStyle() {
    font = loadFont("ArialNarrow-48.vlw");
    setDefaultColors();
  }

  // Tinted Default
  public MyGUIStyle(color tint) {
    font = loadFont("ArialNarrow-48.vlw");
    setDefaultColors();
    tint(tint);
  }
      
  // Full advanced constructor, font, fontSize and all colours
  public MyGUIStyle(PFont font, int fontSize, color background, color buttonFace, color buttonHighlight, color buttonShadow,
    color buttonText, color icon, color face, color highlight, color shadow, color disabled, color highlightText, color scrollBar)
  {
    this.font = font;
    this.fontSize = fontSize;
    this.background = background;
    this.buttonFace = buttonFace;
    this.buttonHighlight = buttonHighlight;
    this.buttonShadow = buttonShadow;
    this.buttonText = buttonText;
    this.icon = icon;
    this.face = face;
    this.highlight = highlight;
    this.shadow = shadow;
    this.disabled = disabled;
    this.highlightText = highlightText;
    this.scrollBar = scrollBar;
  }
  
  // Condensed constructor, takes simpler set of colours and reuses them
   public MyGUIStyle(PFont font, int fontSize, color buttonFace, color buttonHighlight, color buttonShadow,
    color buttonText)
  {
    this.font = font;
    this.fontSize = fontSize;
    this.background = color(153, 153, 153, transparency);
    this.buttonFace = buttonFace;
    this.buttonHighlight = buttonHighlight;
    this.buttonShadow = buttonShadow;
    this.buttonText = buttonText;
    this.icon = buttonText;
    this.face = buttonFace;
    this.highlight = buttonHighlight;
    this.shadow = buttonShadow;
    this.disabled = tintColor(buttonFace, background);
    this.highlightText = buttonHighlight;
    this.scrollBar = background;
  } 
  
  private void setDefaultColors() {
    // Storage method to save repetition in constructors
    background = color(153, 153, 153, transparency);
    buttonFace = color(102, 124, 196, transparency);
    buttonHighlight = color(137, 157, 221, transparency);
    buttonShadow = color(78, 93, 140, transparency);
    buttonText = color(0, 0 ,0, transparency);
    icon = color(0, 0, 0, transparency);
    face = color(102, 124, 196, transparency);
    highlight = color(137, 157, 221, transparency);
    shadow = color(78, 93, 140, transparency);
    disabled = tintColor(color(200), highlight, 0.2);
    highlightText = color(78, 93, 140, transparency);
    scrollBar = color(207, 217, 251, transparency);
  }
  
  // Setter methods - note all variables are public and can be modified using dot syntax
  void setFont(PFont newFont) {
    font = newFont;
  }
  
  void setFont(PFont newFont, int newFontSize) {
    font = newFont;
    fontSize = newFontSize;
  }  
  
  void setStrokeWeight(int weight) {
    strokeWeight = weight;
  }
  
  void setPadding(int pad) {
    padding = pad;
  }
  
  // this method alters all the colours by tinting them
  void tint(color c) {
    this.background = tintColor(background, c);
    this.buttonFace = tintColor(buttonFace, c);
    this.buttonHighlight = tintColor(buttonHighlight, c);
    this.buttonShadow = tintColor(buttonShadow, c);
    this.buttonText = tintColor(buttonText, c);
    this.icon = tintColor(icon, c);
    this.face = tintColor(face, c);
    this.highlight = tintColor(highlight, c);
    this.shadow = tintColor(shadow, c);
    this.highlightText = tintColor(highlightText, c);
    this.disabled = tintColor(color(200), highlight, 0.2);
    this.scrollBar = tintColor(scrollBar, c);
  }
  
  // alternative tint, resets to default colours first
  void tintDefault(color c) {
    setDefaultColors();
    tint(c);
  }

  // calls tintColor with maximum amount
  color tintColor(color c, color tint) {
    return tintColor(c, tint, 1.0);
  }
                  
  // sub-method, returns a single tinted value
  color tintColor(color c, color tint, float str) {
    float br = brightness(c) * brightness(tint) / 255 / 255;
    float r = (red(tint)*str + red(c)*(1-str)) * br;
    float g = (green(tint)*str + green(c)*(1-str)) * br;
    float b = (blue(tint)*str + blue(c)*(1-str)) * br;
    return color(round(r), round(g), round(b), alpha(c));
  }
  
  void setTransparency(float a) {
    transparency = a;
    this.background = updateColorTransparency(background, a);
    this.buttonFace = updateColorTransparency(buttonFace, a);
    this.buttonHighlight = updateColorTransparency(buttonHighlight, a);
    this.buttonShadow = updateColorTransparency(buttonShadow, a);
    this.buttonText = updateColorTransparency(buttonText, a);
    this.icon = updateColorTransparency(icon, a);
    this.face = updateColorTransparency(face, a);
    this.highlight = updateColorTransparency(highlight, a);
    this.shadow = updateColorTransparency(shadow, a);
    this.disabled = updateColorTransparency(disabled, a);
    this.highlightText = updateColorTransparency(highlightText, a);
    this.scrollBar = updateColorTransparency(scrollBar, a);    
  }
  
  void setTransparency(int a) {
    setTransparency((float)a);
  }
  
  private color updateColorTransparency(color c, float a) {
    return color(red(c), green(c), blue(c), a); 
  }
  
  // Note: All values are public can be easily accessed using dot sytax
  
  // Creates a copy/clone e.g. if a variant style is needed
  public MyGUIStyle copy() {
    // Create new style object using existing colours/fonts
    MyGUIStyle cst = new MyGUIStyle(font, fontSize, background, buttonFace, buttonHighlight, buttonShadow, buttonText, icon, face, highlight, shadow, disabled, highlightText, scrollBar);
    cst.setStrokeWeight(strokeWeight);
    cst.setPadding(padding);
    return cst;
  }
}
