module Access
  class Response

    attr_accessor :info, :offers, :stores, :locations, :categories, :suggestions, :oauth_applications, :access_tokens, :oauth_tokens, :oauth_token, :verify, :reports, :members, :filters, :links, :dev_node, :message, :status, :success, :error, :response_status, :content_type, :redemption_method, :details, :oauth_access_token, :api_calls_over_time, :member_usage, :offer_count_in_categories, :offer_count_by_redemption_method, :offer_count_by_facet, :custom_aggregation, :internal_details, :users, :response

    def initialize(response)
      @response = response # Setting this temporarily so i can have a working member reg call, since it doesn't follow the resource [] best practices
      response.each { |key, value| instance_variable_set("@#{key}", value) if self.class.instance_methods.include? key.to_sym }
      @response_status = response.message
      @status ||= response.code
      check_success(response)
      if @success
        @info = Access::Info.new(@info) if @info
        (@links = @links.is_a?(Array) ? Access::Link.process_batch(@links) : Access::Link.new(@links)) if @links
        process_data
      end

    end

    def check_success(response)
      if response.success?
        @success = true
      else
        create_error
      end
    end

    def create_error
      @success = false
      @error = Access::Error.new(@response_status, @status, @message)
      remove_instance_variable(:@message) if @message
      remove_instance_variable(:@status)
    end
  end

  class OfferResponse < Response
    def process_data
      # for when you search and it returns 0
      (@offers = []; create_error) if @message
      @offers = Access::Offer.process_batch(@offers)
      @offer_count_in_categories = Access::Aggregations.process_batch(@offer_count_in_categories) if @offer_count_in_categories
      @offer_count_by_redemption_method = Access::Aggregations.process_batch(@offer_count_by_redemption_method) if @offer_count_by_redemption_method
      @offer_count_by_facet = Access::Aggregations.process_batch(@offer_count_by_facet) if @offer_count_by_facet
      @custom_aggregation = Access::Aggregations.process_batch(@custom_aggregation) if @custom_aggregation
    end
  end

  class StoreResponse < Response
    def process_data
      (@stores = []; create_error) if @message
      @stores = Access::Store.process_batch(@stores)
      @offer_count_in_categories = Access::Aggregations.process_batch(@offer_count_in_categories) if @offer_count_in_categories
      @custom_aggregation = Access::Aggregations.process_batch(@custom_aggregation) if @custom_aggregation
    end
  end

  class LocationResponse < Response
    def process_data
      (@locations = []; create_error) if @message
      @locations = Access::Location.process_batch(@locations)
      @custom_aggregation = Access::Aggregations.process_batch(@custom_aggregation) if @custom_aggregation
    end
  end

  class CategoryResponse < Response
    def process_data
      (@categories = []; create_error) if @message
      @categories = Access::Category.process_batch(@categories)
    end
  end

  class AutocompleteResponse < Response
    def process_data
      # (@suggestions = []; create_error) if @message
      @suggestions = Access::Autocomplete.new(@suggestions)
    end
  end

  class OauthApplicationResponse < Response
    def process_data
      (@oauth_applications = []; create_error) if @message
      @oauth_applications = Access::OauthApplication.process_batch(@oauth_applications)
    end
  end

  class TokenResponse < Response
    def process_data
      (create_error) if @message
      @access_tokens = Access::Token.process_batch(@access_tokens) if @access_tokens
      @oauth_tokens = Access::Token.process_batch(@oauth_tokens) if @oauth_tokens
      @oauth_token = Access::Token.new(@oauth_token) if @oauth_token
    end
  end

  class VerifyResponse < Response
    def process_data
      @oauth_access_token = Access::Verify.new(@oauth_access_token) if @oauth_access_token
      @offers = Access::Verify.new(@offers) if @offers
      @categories = Access::Verify.new(@categories) if @categories
      @locations = Access::Verify.new(@locations) if @locations
      @stores = Access::Verify.new(@stores) if @stores
    end
  end

  class RedeemResponse < Response
    def process_data
      @details = Access::Redeem.new(@details) if @details
    end
  end

  class ReportResponse < Response
    def process_data
      @api_calls_over_time = Access::Report.process_batch(@api_calls_over_time) if @api_calls_over_time
      @member_query_terms = Access::Report.process_batch(@member_query_terms) if @member_query_terms
      @member_postal_codes = Access::Report.process_batch(@member_postal_codes) if @member_postal_codes
    end
  end

  class MemberRegistrationResponse < Response
    def process_data
      @users = Access::Member.process_batch([@response])
    end
  end


  class MemberResponse < Response
    def process_data
      @users = Access::Member.process_batch(@users)
    end
  end

  class FilterResponse < Response
    def process_data
      @filters = Access::Filter.process_batch(@filters)
    end
  end

  class CitySavingsResponse < Response
    def process_data

    end
  end

  class ChannelResponse < Response
    def process_data
      @channels = Access::Channel.process_batch(@channels)
    end
  end

  class CampaignResponse < Response
    def process_data
      @campaigns = Access::Campaign.process_batch(@campaigns)
    end
  end

  class SpotResponse < Response
    def process_data
      @spots = Access::Spot.process_batch(@spots)
    end
  end



end