# frozen_string_literal: true

module Boxr
  class BoxrError < StandardError
    attr_reader :response_body, :type, :status, :code, :help_uri, :box_message, :boxr_message,
                :request_id

    def initialize(status: nil, body: nil, header: nil, boxr_message: nil)
      super()
      @status = status
      @response_body = body
      @header = header
      @boxr_message = boxr_message

      return unless body

      begin
        body_json = JSON.parse(body)

        if body_json
          @type = body_json['type']
          @box_status = body_json['status']
          @code = body_json['code']
          @help_uri = body_json['help_uri']
          @box_message = body_json['message']
          @request_id = body_json['request_id']
        end
      rescue JSON::ParserError
        # Ignore JSON parsing errors
      end
    end

    def message
      unless @header.nil? || @header['WWW-Authenticate'].nil?
        auth_header = @header['WWW-Authenticate'][0]
      end
      if auth_header && auth_header != []
        "#{@status}: #{auth_header}"
      elsif @box_message
        "#{@status}: #{@box_message}"
      elsif @boxr_message
        @boxr_message
      else
        "#{@status}: #{@response_body}"
      end
    end

    def to_s
      message
    end
  end
end
