#!/usr/bin/env python
# Copyright 2011 Google Inc. All Rights Reserved.
"""A viewer for the timeline objects."""

import functools
import urllib

from grr.gui import renderers
from grr.gui.plugins import fileview
from grr.lib import aff4
from grr.lib import rdfvalue
from grr.lib import utils


class TimelineViewRenderer(renderers.RDFValueRenderer):
  """Render a container View.

  Post Parameters:
    - aff4_path: The path to the currently drawn object.
  """
  classname = "TimelineView"

  layout_template = renderers.Template("""
<a href='#{{this.hash|escape}}'
   onclick='grr.loadFromHash("{{this.hash|escape}}");'
   class="grr-button grr-button-red">
  View details.
</a>
""")

  def Layout(self, request, response):
    client_id = request.REQ.get("client_id")

    container = request.REQ.get("aff4_path", "")
    if container:
      self.container = rdfvalue.RDFURN(container)
      self.hash_dict = dict(
          container=self.container, main="TimelineMain", c=client_id,
          reason=request.token.reason)
      self.hash = urllib.urlencode(sorted(self.hash_dict.items()))

      return super(TimelineViewRenderer, self).Layout(request, response)


class TimelineMain(renderers.TemplateRenderer):
  """This is the main view to the timeline.

  Internal State (from hash value):
    - container: The container name for the timeline.
    - query: The query to filter.
  """

  layout_template = renderers.Template("""