# frozen_string_literal: true

module Shared
  module WikipediaModule
    def wikipedia
      @title ||= params[:id]
      coder = HTMLEntities.new
      w = @wikipedia = WikipediaService.new
      @decoded = ""
      begin
        query_results = w.query(
          titles: @title,
          redirects: "",
          prop: "revisions",
          rvprop: "content"
        )
        raw = query_results.blank? ? nil : query_results.at( "page" )
        unless raw.blank? || raw["missing"]
          parsed = w.parse( page: raw["title"] )&.at( "text" )&.try( :inner_text )&.to_s
          @decoded = coder.decode( parsed )
          @decoded.gsub!( 'href="//', 'href="https://' )
          @decoded.gsub!( 'src="//', 'src="https://' )
          @decoded.gsub!( 'href="/', "href=\"#{w.base_url}/" )
          @decoded.gsub!( 'src="/', "src=\"#{w.base_url}/" )
          filter_wikipedia_content
        end
      rescue Timeout::Error => e
        Rails.logger.info "[INFO] Wikipedia API call failed: #{e.message}"
      end

      respond_to do | format |
        format.html do
          if @decoded.empty?
            render( plain: "#{@before_wikipedia} Wikipedia doesn't have a page for #{@title}", status: 404 )
          else
            render( plain: "#{@before_wikipedia} #{@decoded}" )
          end
        end
      end
    rescue SocketError
      raise unless Rails.env.development?

      Rails.logger.debug "[DEBUG] Looks like you're offline, skipping wikipedia"
      render plain: "You're offline."
    end

    private

    # Override and filter the contents of @decoded if you need to
    def filter_wikipedia_content; end
  end
end
