module JamRuby
  class GeoIpBlocks < ActiveRecord::Base

    # index names created on the copied table used during import.
    # they do not exist except during import
    COPIED_GEOIPBLOCKS_INDEX_NAME = 'geoipblocks_copied_geom_gix'
    GEOIPBLOCKS_INDEX_NAME = "geoipblocks_geom_gix"

    @@log = Logging.logger[GeoIpBlocks]

    self.table_name = 'geoipblocks'

    belongs_to :location, class_name: 'JamRuby::GeoIpLocations', inverse_of: 'blocks', foreign_key: 'locid'

    def self.lookup(ipnum)
      self.where('geom && ST_MakePoint(?, 0) AND ? BETWEEN beginip AND endip', ipnum, ipnum)
      .limit(1)
      .first
    end

    def self.createx(beginip, endip, locid)
      c = connection.raw_connection
      c.exec_params("insert into #{self.table_name} (beginip, endip, locid, geom) values($1::bigint, $2::bigint, $3, ST_MakeEnvelope($1::bigint, -1, $2::bigint, 1))", [beginip, endip, locid])
    end

    def self.ip_lookup(ip_addy)
      addr = ip_address_to_int(ip_addy)
      self.where(["beginip <= ? AND ? <= endip", addr, addr])
      .limit(1)
      .first
    end

    def self.import_from_max_mind(options)

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

      # File Geo-134
      # Format:
      # startIpNum,endIpNum,locId

      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|
          s = io.gets.strip # eat the copyright line. gah, why do they have that in their file??
          unless s.eql? 'Copyright (c) 2011 MaxMind Inc.  All Rights Reserved.'
            puts s
            puts 'Copyright (c) 2011 MaxMind Inc.  All Rights Reserved.'
            raise 'file does not start with expected copyright (line 1): Copyright (c) 2011 MaxMind Inc.  All Rights Reserved.'
          end

          s = io.gets.strip # eat the headers line
          unless s.eql? 'startIpNum,endIpNum,locId'
            puts s
            puts 'startIpNum,endIpNum,locId'
            raise 'file does not start with expected header (line 2): startIpNum,endIpNum,locId'
          end

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

          stmt = "INSERT INTO #{copied_table_name} (beginip, endip, locid) VALUES"

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

          csv = ::CSV.new(io, {encoding: 'ISO-8859-1', headers: false})
          csv.each do |row|
            raise "file does not have expected number of columns (3): #{row.length}" unless row.length == 3

            beginip = ip_address_to_int(strip_quotes(row[0]))
            endip = ip_address_to_int(strip_quotes(row[1]))
            locid = row[2]

            vals = vals+sep+"(#{beginip}, #{endip}, #{locid})"
            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 then
            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

      sts = self.connection.execute "ALTER TABLE #{copied_table_name} DROP COLUMN geom;"
      ActiveRecord::Base.logger.debug "DROP COLUMN geom returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
      # sts.check [we don't care]

      sts = self.connection.execute "ALTER TABLE #{copied_table_name} ADD COLUMN geom geometry(polygon);"
      ActiveRecord::Base.logger.debug "ADD COLUMN geom returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
      sts.check

      sts = self.connection.execute "UPDATE #{copied_table_name} SET geom = ST_MakeEnvelope(beginip, -1, endip, 1);"
      ActiveRecord::Base.logger.debug "SET geom returned sts #{sts.cmd_tuples}" if ActiveRecord::Base.logger
      sts.check

      sts = self.connection.execute "CREATE INDEX #{COPIED_GEOIPBLOCKS_INDEX_NAME} ON #{copied_table_name} USING GIST (geom);"
      ActiveRecord::Base.logger.debug "CREATE INDEX #{COPIED_GEOIPBLOCKS_INDEX_NAME} returned sts #{sts.cmd_status}" if ActiveRecord::Base.logger
      sts.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_GEOIPBLOCKS_INDEX_NAME} RENAME TO #{GEOIPBLOCKS_INDEX_NAME}").check
      self.connection.execute("ALTER TABLE #{self.table_name}_copied RENAME TO #{self.table_name}").check
    end
  end
end
