#--
# Copyright (c) 2010-2012 Michael Berkovich, tr8nhub.com
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++

module Tr8n::Admin::BaseHelper

  def tr8n_will_filter(results)
    will_filter_tag(results)
  end

  def tr8n_pretty_print_hash(hash)
    return "" unless hash
    html = ""
    hash.each do |key, value|
       html << "<strong>" 
       html << key << ": </strong>"
       if value.is_a?(Hash)
         html << "{"
         html << tr8n_pretty_print_hash(value)
         html << "} "
       else
         html << value.strip if value
         html << "; "
       end
   end
   html
  end
  
  def language_metric_chart(field = :user_count, limit = 20)
    labels = []
    label_positions = []
    counts = []
    Tr8n::TotalLanguageMetric.find(:all, :conditions => ["language_id <> ?", Tr8n::Config.default_language.id], :order => "#{field} desc", :limit => limit).each_with_index do |metric, index|
      labels << metric.language.english_name
      label_positions << ((index+1) * 30)
      counts << (metric.send(field) || 0)
    end

    max_count = counts.max
    max_count = 100 if max_count < 100
    counts = counts.collect{|c| c/(max_count * 1.0) * 100}
    
    chart_params = []
    chart_params << "chxl=1:|#{labels.join('|')}|" 
    chart_params << "chxp=#{label_positions.join(',')}"
    chart_params << "chxr=0,0,#{limit}|0,0,#{max_count}"
    chart_params << "chxt=x,y"
    chart_params << "chs=1000x300"
    chart_params << "cht=bhs"
    chart_params << "chbh=10"
    chart_params << "chco=008000"
    chart_params << "chd=t:#{counts.reverse.join(',')}"
    
    image_tag("http://chart.apis.google.com/chart?#{chart_params.join('&')}")     
  end
  
  def tr8n_sections_tag(opts = {})
    render(:partial => "/tr8n/admin/common/sections", :locals => {:mode => params[:mode], :modes => opts[:modes], :opts => opts})
  end

  def tr8n_lightbox_buttons_tag(opts = {})
    render(:partial => "/tr8n/admin/common/lightbox_buttons")
  end

end
