module JamRuby
  class Country < ActiveRecord::Base

    @@log = Logging.logger[Country]

    self.table_name = 'countries'

    def self.get_all()
      self.order('countryname asc').all
    end

    def self.import_from_iso3166(options)

      file = options[:file]
      use_copy = options[:use_copy] ||= false

      start = Time.now

      copied_table_name = Database.copy_table(self.table_name)

      if use_copy
        Database.copy(copied_table_name, file)
      else
        File.open(file, 'r:ISO-8859-1') do |io|

          saved_level = ActiveRecord::Base.logger ? ActiveRecord::Base.logger.level : 0
          count = 0

          stmt = "INSERT INTO #{copied_table_name} (countrycode, countryname) VALUES"

          vals = ''
          sep = ''
          i = 0
          n = 20

          csv = ::CSV.new(io, {encoding: 'ISO-8859-1', headers: false})
          csv.each do |row|
            vals = vals+sep+"(#{ActiveRecord::Base.quote_value(row[0], nil)}, #{ActiveRecord::Base.quote_value(row[1], nil)})"
            sep = ','
            i += 1

            if count == 0 or i >= n then
              self.connection.execute stmt+vals
              count += i
              vals = ''
              sep = ''
              i = 0

              if ActiveRecord::Base.logger and ActiveRecord::Base.logger.level > 1 then
                ActiveRecord::Base.logger.debug "... logging inserts into #{copied_table_name} suspended ..."
                ActiveRecord::Base.logger.level = 1
              end

              if ActiveRecord::Base.logger and count%10000 < n then
                ActiveRecord::Base.logger.level = saved_level
                ActiveRecord::Base.logger.debug "... inserted #{count} into #{copied_table_name} ..."
                ActiveRecord::Base.logger.level = 1
              end
            end
          end

          if i > 0
            self.connection.execute stmt+vals
            count += i
          end

          if ActiveRecord::Base.logger then
            ActiveRecord::Base.logger.level = saved_level
            ActiveRecord::Base.logger.debug "loaded #{count} records into #{copied_table_name}"
          end
        end # file
      end

      elapsed = Time.now - start
      @@log.debug("#{copied_table_name} import took #{elapsed} seconds")
    end

    def self.after_maxmind_import
      self.connection.execute("DROP TABLE #{self.table_name}").check
      self.connection.execute("ALTER TABLE #{self.table_name}_copied RENAME TO #{self.table_name}").check
    end
  end
end
