require 'ipaddr'

module JamRuby
  class Score < ActiveRecord::Base

    MAX_YELLOW_LATENCY = 40 # this round-trip internet latency, not full score

    self.table_name = 'scores'

    attr_accessible :alocidispid, :anodeid, :aaddr, :auserid, :alatencytestid, :blocidispid, :bnodeid, :baddr, :buserid, :blatencytestid, :score, :score_dt, :scorer, :scoring_data

    default_scope { order('score_dt desc') }

    def self.createx(alocidispid, anodeid, aaddr, blocidispid, bnodeid, baddr, score, score_dt = Time.now, score_data = nil, user_info = {})
      score_dt = Time.new.utc if score_dt.nil?
      score = score.ceil
      raise "alocidispid must be positive" if alocidispid <= 0
      raise "blocidispid must be positive" if blocidispid <= 0
      raise "score must be positive" if score <= 0
      ascore = Score.create(alocidispid: alocidispid, anodeid: anodeid, aaddr: aaddr, auserid: user_info[:auserid], alatencytestid: user_info[:alatencytestid], blocidispid: blocidispid, bnodeid: bnodeid, baddr: baddr, buserid: user_info[:buserid], blatencytestid: user_info[:blatencytestid], score: score, scorer: 0, score_dt: score_dt, scoring_data: score_data)
      bscore = Score.create(alocidispid: blocidispid, anodeid: bnodeid, aaddr: baddr, auserid: user_info[:buserid], alatencytestid: user_info[:blatencytestid], blocidispid: alocidispid, bnodeid: anodeid, baddr: aaddr, buserid: user_info[:auserid], blatencytestid: user_info[:alatencytestid], score: score, scorer: 1, score_dt: score_dt) if alocidispid != blocidispid
      Score.connection.execute("select update_current_network_scores(#{alocidispid}, #{blocidispid})")
      return [ascore, bscore]
    end

    def self.deletex(alocidispid, blocidispid)
      Score.where(alocidispid: alocidispid, blocidispid: blocidispid).delete_all
      Score.where(alocidispid: blocidispid, blocidispid: alocidispid).delete_all if alocidispid != blocidispid
    end

    def self.findx(alocidispid, blocidispid)
      s = Score.where(alocidispid: alocidispid, blocidispid: blocidispid).first
      return -1 if s.nil?
      return s.score
    end

    def self.score_conns(c1, c2, score)
      user_info = {}
      if c1.user_id
        user_info[:auserid] = c1.user_id
      else
        user_info[:alatencytestid] = c1.latency_tester.id
      end

      if c2.user_id
        user_info[:buserid] = c2.user_id
      else
        user_info[:blatencytestid] = c2.latency_tester.id
      end

      self.createx(c1.locidispid, c1.client_id, c1.addr, c2.locidispid, c2.client_id, c2.addr, score, nil, user_info)
    end

    # locid is a geoiplocation or geoipblock locid.
    # coid  is a jamisp coid
    def self.compute_locidispid(locid, coid)
      locid * 1000000 + coid
    end

    # if you have the right models in hand, this will compute a valid locidispid
    def self.create_locidispid(geoiplocation_or_geoipblock, jamisp_or_jamcompany)
      compute_locidispid(geoiplocation_or_geoipblock.locid, jamisp_or_jamcompany.coid)
    end

    #
    def self.record(current_user, aclientid, aip_address, bclientid, bip_address, score, score_data, udpReachable)

      # parameter checks
      return {message: 'aclientid not specified', error: true} if aclientid.nil?
      return {message: 'aAddr not specified', error: true} if aip_address.nil?
      return {message: 'bclientid not specified', error: true} if bclientid.nil?
      return {message: 'bAddr not specified', error: true} if bip_address.nil?
      return {message: 'aclientid is same as bclientid', error: true} if aclientid == bclientid

      aAddr = JamRuby::JamIsp.ip_to_num(aip_address)
      return {message: 'aAddr not valid ip_address', error: true} if aAddr.nil?
      bAddr = JamRuby::JamIsp.ip_to_num(bip_address)
      return {message: 'bAddr not valid ip_address', error: true} if bAddr.nil?

      if aAddr == bAddr
        result = Score.connection.execute("SELECT connection_failed_score('#{aclientid}', '#{bclientid}', INTERVAL '#{APP_CONFIG.scoring_timeout_minutes} minutes', #{APP_CONFIG.scoring_timeout_threshold})")
        in_timeout = result[0]["connection_failed_score"]
        return {message: "aAddr and bAddr are the same (to=#{in_timeout})", error: true }
      end

      if udpReachable == false  # we don't care if it's nil; it has to be FALSE explicitely
        result = Score.connection.execute("SELECT connection_failed_score('#{aclientid}', '#{bclientid}', INTERVAL '#{APP_CONFIG.scoring_timeout_minutes} minutes', #{APP_CONFIG.scoring_timeout_threshold})")
        in_timeout = result[0]["connection_failed_score"]
        return {message: "udpReachable is false (to=#{in_timeout})", error: true }
      end

      if score.nil? || !score.is_a?(Numeric)
        return {message: 'score not specified or not numeric' , error: true}
      end

      result = Score.connection.execute("SELECT connection_good_score('#{aclientid}', '#{bclientid}')");
      in_timeout = result[0]["connection_good_score"]

      # check a connection's state
      aconn = Connection.where(client_id: aclientid, user_id: current_user.id).first
      return {message: "a's session not found", error: true} if aconn.nil?
      return {message: "a's session addr does not match aAddr", error: true} if aAddr != aconn.addr

      # check b connection's state
      bconn = Connection.where(client_id: bclientid).first
      return {message: "b's session not found", error: true} if bconn.nil?
      return {message: "b's session addr does not match bAddr", error: true} if bAddr != bconn.addr

      # check sanity of the score
      return {message: 'score < 0 or score > 999', error: true} if score < 0 or score > 999

      aloc = JamRuby::GeoIpBlocks.lookup(aAddr)
      aisp = JamRuby::JamIsp.lookup(aAddr)
      return {message: "a's location or isp not found", error: true} if aisp.nil? or aloc.nil?
      alocidispid = aloc.locid*1000000+aisp.coid;

      bloc = JamRuby::GeoIpBlocks.lookup(bAddr)
      bisp = JamRuby::JamIsp.lookup(bAddr)
      return {message: "b's location or isp not found", error: true} if bisp.nil? or bloc.nil?

      blocidispid = bloc.locid*1000000+bisp.coid


      user_info = {}
      if aconn.user_id
        user_info[:auserid] = aconn.user_id
      else
        user_info[:alatencytestid] = aconn.latency_tester.id
      end

      if bconn.user_id
        user_info[:buserid] = bconn.user_id
      else
        user_info[:blatencytestid] = bconn.latency_tester.id
      end

      JamRuby::Score.createx(alocidispid, aclientid, aAddr, blocidispid, bclientid, bAddr, score.ceil, nil, score_data, user_info)

      return {message: "OK (to=#{in_timeout})", error: false}
    end
  end
end
