require 'debugger'              # optional, may be helpful
require 'open-uri'              # allows open('http://...') to return body
require 'cgi'                   # for escaping URIs
require 'nokogiri'              # XML parser
require 'active_model'          # for validations

class OracleOfBacon

  class InvalidError < RuntimeError ; end
  class NetworkError < RuntimeError ; end
  class InvalidKeyError < RuntimeError ; end

  attr_accessor :from, :to
  attr_reader :api_key, :response, :uri
  
  include ActiveModel::Validations
  validates_presence_of :from
  validates_presence_of :to
  validates_presence_of :api_key
  validate :from_does_not_equal_to

  def from_does_not_equal_to
    if @from == @to
      #return false
      errors.add(:base, 'From cannot be the same as To')
    else
      return true
      #errors.add(:base, 'From cannot be the same as To')
    end
  print errors
  end

  def initialize(api_key='38b99ce9ec87')
    @api_key = api_key
    @from = "Kevin Bacon"
    @to = "Kevin Bacon"
    #@errors = ActiveModel::Errors.new(self)
  end
 
  def find_connections
    make_uri_from_arguments
    begin
      xml = URI.parse(uri).read
    rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
      Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
      Net::ProtocolError => e
      # convert all of these into a generic OracleOfBacon::NetworkError,
      #  but keep the original error message
      # your code here
      if e
        raise OracleOfBacon::NetworkError
      end
    end
    # your code here: create the OracleOfBacon::Response object
    Response.new(xml)
  end

  def make_uri_from_arguments
    # your code here: set the @uri attribute to properly-escaped URI
    #   constructed from the @from, @to, @api_key arguments
    @uri = "http://oracleofbacon.org/cgi-bin/xml?p=#{CGI.escape(@api_key)}&a=#{CGI.escape(@from)}&b=#{CGI.escape(@to)}"
  end
      
  class Response
    attr_reader :type, :data
    # create a Response object from a string of XML markup.
    def initialize(xml)
      @doc = Nokogiri::XML(xml)
      parse_response
    end

    private

    def parse_response
      if ! @doc.xpath('/error').empty?
        parse_error_response
      elsif @doc.xpath('//actor').length > 0 #code here
        @type = :graph
        a = []
        @doc.xpath('//actor').each{|actor| a << actor.text}
        b = [] 
        @doc.xpath('//movie').each{|movie| b << movie.text}
        #c = a.zip(b)
        @data = a.zip(b).flatten.compact
      elsif !@doc.xpath('/spellcheck').empty? #spellcheck code here
        @type = :spellcheck
        a= []
        @doc.xpath('//match').each{|match| a << match.text}
        #print @doc.xpath('/spellcheck')
        #print a
        @data = a
      else
        @type = :unknown
        @data = "unknown response type"
             
      end 
    end
    def parse_error_response
      @type = :error
      @data = 'Unauthorized access'
    end
  end
end

