# -*- coding: utf-8 -*-
module CommLogistics::Modules::FusionchartsHelper

  # Stacked3D等用のクラス
  class FCCategoryDataset
    attr_reader :results_hash, :categories

    def initialize(options)
      @results     = options[:results]
      @categories  = options[:categories]
      @datasets    = options[:datasets]
      @category_as = options[:category_as]
      @dataset_as  = options[:dataset_as]
      self.results_to_hash
    end

    def results_to_hash
      @results_hash = Hash.new
      for p in @datasets do
        @results_hash[p[@dataset_as]] = Hash.new
        for c in @categories do
          @results_hash[p[@dataset_as]][c[@category_as]] = 0
        end
      end
    end

    def sum_up_results(options)
      #@categories は納品先データのハッシュ
      @total_quantity = Hash.new
      for c in @categories do
        @total_quantity[c[@category_as]] = 0
      end

      # @results_hash[データセット][カテゴリ] を作成
      # カテゴリ毎の総計も同時に集計する
      for r in @results do
        @results_hash[r[@dataset_as]][r[@category_as]] += r[options[:sum]].to_i
        @total_quantity[r[@category_as]] += r[options[:sum]].to_i
      end
      
      # 総計が多い順にカテゴリを並びかえ
      if(options[:sort])
        @categories.sort!{|a,b|
          @total_quantity[b[@category_as]] <=> @total_quantity[a[@category_as]]
        }
      end

      # options[:limit] が設定されていなかったらここで終わり
      return if options[:limit].nil?
      
      # options[:limit] で設定された順位で切り出す
      # 3位タイなどは全て表示する
      count = 0
      #タイが多すぎるときに使う変数
      before_count = 0
      before_quantity = 9999999
      @categories.each_with_index do |value, i|
        quantity = @total_quantity[value[@category_as]]
        #puts "#{quantity}:#{before_quantity}:#{count}"
        count += 1
        if quantity < before_quantity
          if count > options[:limit]
            break
          else 
            before_count = count
          end
        end
        before_quantity = quantity
      end
      #タイが多すぎて、:extra_limitに及ぶときは、最後のbefore_quantityの記録をすべてのぞく
      if !options[:extra_limit].blank? && count > options[:extra_limit]
        @categories = @categories.slice(0,before_count-1)
      else
        @categories = @categories.slice(0,count) #こっちは、count数を表示したいので、-1はしない
      end
    end

  end

  # Bubble 用クラス
  class FCBubbleDataSet
    attr_reader :results, :zmax
    def initialize(options)
      @results = options[:results]
      @xaxis   = options[:xaxis]
      @yaxis   = options[:yaxis]
      @zaxis   = options[:zaxis]
      @group   = options[:group]
      #x,y の最大値を算出しておく
      self.calcMax
    end

    def calcMax
      @zmax = Hash.new
      for r in @results
        p = r[@group].to_i
        if(!@zmax[p] || r[@zaxis].to_i > @zmax[p])
          @zmax[p] = r[@zaxis].to_i
        end
#        if(r[@xaxis].to_i > @xmax)
#          @xmax = r[@xaxis].to_i
#        end
#        if(r[@yaxis].to_i > @ymax)
#          @ymax = r[@yaxis].to_i
#        end
      end
      # 最大値を桁数の冪乗にあわせる
      #@xmax = 10 ** digit(@xmax) 
      #@ymax = 10 ** digit(@ymax)
    end

    def xrange
      tmp = 0
      if(@xtics > 0)
        step = @xmax / @xtics
      end
      step = 1 if(step == 0)

      while(tmp <= @xmax) do
        yield(tmp)
        tmp = tmp + step
      end
    end

    def yrange
      tmp = 0
      if(@ytics > 0)
        step = @ymax / @ytics
      end
      step = 1 if(step == 0)

      while(tmp <= @ymax) do
        yield(tmp)
        tmp = tmp + step
      end
    end
    
    def digit(value)
      if(value > 0)
        Math.log10(value).ceil
      else
        1
      end
    end

  end
      
end
