module Comm
  module Module
    module OutputXls
      def output_csv(hrs, param)
        sps = Comm::Module::OutputCsv.new()
        sps.output(hrs, param)
      end
      
      def output_xls(hrs, param)
        require 'config/site_config'
        if param.include?('spread_sheet_type')
          sheet_type =  param['spread_sheet_type']
        else
          $OUTPUT_SPREADSHEET_TYPE ||= 2
          sheet_type = $OUTPUT_SPREADSHEET_TYPE
        end
             
        case sheet_type.to_s
          when '1'
          #POI
          ###drb版(Poi::ExcelPoiServerを起動して使います)
          #require 'drb/drb'
          #sps = DRbObject.new_with_uri('druby://localhost:28787')
          ###ノーマル版(passengerでは不明なエラーが起こる版)
          sps = Poi::ExcelPoi.new()
          when '2'
          #SPREADSHEET
          sps = Comm::Module::OutputXlsReal::OutputXls.new(params)
          when '3'
          #CSV
          sps = Comm::Module::OutputCsv.new(param)
          when '4'
          sps = self.class::OutputCustomXls.new(params)
        else
          #その他は（なんでもいいけど）SPREADSHEET
          sps = Comm::Module::OutputXlsReal::OutputXls.new(params)
        end
        sps.output(hrs, param[:c])
      end
    end
    #xls,csv共通処理
    class OutputSpreadSheet
      attr_accessor :output_options
      attr_accessor :total_temp
      def initialize(params)
        @output_options = {}
        if params.include?('output_xls_no_id') and params['output_xls_no_id'] == 'true'
          @output_options[:no_id] = true
        end
        if params.include?('total_columns') and params['total_columns'] != nil
          total_columns = params['total_columns'].values
          @total_temp = {}
          total_columns.each{|c| @total_temp[c] = 0}   
        end
        if params.include?('total_title_column') and params['total_title_column'] != nil
          @output_options[:total_title_column] = params['total_title_column']
        end
        #画面に応答を返す際にリロードを要求するフラグ
        @reply_reload = false
      end
      #出力メソッドの最後に、固有の処理をするオーバーライド用
      def treat_additions_on_end(hr, col_info)
      end
      #応答に要素を加える
      def add_keys_to_reply
        add_hash = {}
        if @reply_reload
          add_hash['reload'] = true
        end
        add_hash
      end
      #出力していいカ(ラム)?
      def output_field?(field)
        if @output_options[:no_id] and field =~ /(_code|_id)$/
          return false
        end
        true
      end
      #合計値を出力する最終列向けに
      #カラム名に応じて合計ナドを返します
      def output_sum_ups(field)
        if field == @output_options[:total_title_column]
          #"no_id"な場合はhoge_idは厳密には一時的なカラム(hoge_dn)に入るのですが
          #意図している動作ではあるのでまあいいや
          val = '合計'
        elsif @total_temp.has_key?(field)
          val = @total_temp[field]
        else
          val = ''
        end
        val
      end
      #コンストラクタで指定されたカラム名の場合には値を足していきます
      def sum_up_if_need(field, value)
        if @total_temp.has_key?(field) and value
          if value.class == String
            if integer_str?(value)
              @total_temp[field] += value.to_i
            elsif float_str?(value)
              @total_temp[field] += value.to_f
            else
              #合計できる数値以外のカラムは指定されないハズ...
              raise '想定外の値です。:'+value.inspect+' :'+field.inspect
            end
          else
            @total_temp[field] += value
          end
        end
      end
      def integer_str?(str)
        Integer(str)
        true
      rescue ArgumentError
        false
      end
      #整数でもtrueになるので
      #区別するときはinteger_str?から使うこと
      def float_str?(str)
        Float(str)
        true
      rescue ArgumentError
        false
      end
    end
  end
end