module JusticeGovSk
  class Parser
    class List < JusticeGovSk::Parser
      include Core::Parser::List

      def list(document)
        find_values 'list', document, 'table.GridTable td[align=right] a', verbose: false do |anchors|
          anchors.map { |a| "#{JusticeGovSk::URL.base}#{a[:href].strip}" }
        end
      end

      def page(document)
        # TODO figure our caching and clear

        find_value 'page', document, 'tr.CssPager select:first-child option[selected="selected"]' do |option|
          option.text.to_i
        end
      end

      def pages(document)
        # TODO figure out caching and clear

        find_value 'pages', document, 'tr.CssPager select:first-child option:last-child' do |option|
          option.text.to_i
        end
      end

      def per_page(document)
        find_value 'per page', document, 'tr.CssPager select:last-child option[selected="selected"]' do |option|
          option.text.to_i
        end
      end

      protected

      def clear
        @page  = nil
        @pages = nil
      end
    end
  end
end
