module JamRuby
  class Region < ActiveRecord::Base

    # index names created on the copied table used during import.
    # they do not exist except during import
    COUNTRY_CODE_INDEX_NAME = 'regions_countrycode_ndx'
    COPIED_COUNTRY_CODE_INDEX_NAME = 'regions_copied_countrycode_ndx'

    UNIQUE_INDEX_NAME = 'regions_countrycode_region_ndx'
    COPIED_UNIQUE_INDEX_NAME = 'regions_copied_countrycode_region_ndx'

    @@log = Logging.logger[Region]

    self.table_name = 'regions'

    def self.get_all(country)
      self.where(countrycode: country).order('regionname asc').all
    end

    def self.import_from_region_codes(options)

      file = options[:file]
      use_copy = options[:use_copy]

      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, region, regionname) 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)}, #{ActiveRecord::Base.quote_value(row[2], 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
      end

      # create indexes -- these will be renamed later in the import process
      Region.connection.execute("CREATE INDEX #{COPIED_COUNTRY_CODE_INDEX_NAME} ON #{copied_table_name} (countrycode)").check
      Region.connection.execute("CREATE UNIQUE INDEX #{COPIED_UNIQUE_INDEX_NAME} ON #{copied_table_name} (countrycode, region)").check

      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 INDEX #{COPIED_COUNTRY_CODE_INDEX_NAME} RENAME TO #{COUNTRY_CODE_INDEX_NAME}").check
      self.connection.execute("ALTER INDEX #{COPIED_UNIQUE_INDEX_NAME} RENAME TO #{UNIQUE_INDEX_NAME}").check
      self.connection.execute("ALTER TABLE #{self.table_name}_copied RENAME TO #{self.table_name}").check
    end
  end
end
