package de.dtele.data {
  
  /**
   * Wrapper around durations to allow for
   * convenient access to its time parts
   * (days, hours, minutes, seconds)
   * 
   * @author Mathias Brodala
   */
  public class Duration {
    
    /**
     * The amount of days
     */
    public var days:int = 0;
    
    /**
     * The amount of hours
     */
    public var hours:int = 0;
    
    /**
     * The amount of minutes
     */
    public var minutes:int = 0;
    
    /**
     * The amount of seconds
     */
    public var seconds:int = 0;
    
    /**
     * Sets up the wrapper
     * 
     * @param duration A duration in seconds
     */
    public function Duration(duration:int) {
      
      this.seconds = Math.floor(duration % 60);
      duration = Math.floor(duration / 60);
      this.minutes = Math.floor(duration % 60);
      duration = Math.floor(duration / 60);
      this.hours = Math.floor(duration % 60);
      this.days = Math.floor(duration / 60);
    }
    
    /**
     * Formats this duration as human readable string
     * 
     * @return 
     */
    public function format():String {
      
      var text:String = "";
      
      if (this.days > 0) {
        
        text = this.days + "d, ";
      }
      
      // Always show if at least 1 day
      if (this.hours > 0 || text) {
        
        text += this.hours + ":" +
                (this.minutes < 10 ? "0" : "") + this.minutes + ":" +
                (this.seconds < 10 ? "0" : "") + this.seconds;
      } else {
        
        text += this.minutes + ":" +
                (this.seconds < 10 ? "0" : "") + this.seconds;
      }
      
      return text;
    }
    
    public function toString():String {
      
      return "[object Duration(" +
        this.days + "d, " +
        this.hours + "h, " +
        this.minutes + "m, " +
        this.seconds + "s"
      ")]";
    }
  }
}