# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/act-plain"

module Qwik
  class Action
    def plg_show_history
      return "" if !defined?(@req.base)
      s = @req.base.to_s
      [:span, {:class=>"attribute"},
	[:a, {:href=>s+".history"}, _("Show history")], " "]
    end

    def ext_history
      c_require_pagename
      c_require_member
      #c_require_page_exist # does not require the page is exist
      # because you can see the list of the page that is already deleted.

      return c_nerror("no path args") if 0 < @req.path_args.length
      return c_nerror("no ext args")  if 0 < @req.ext_args.length

      divs = []
      key = @req.base
      page = @site[key]
      @site.backupdb.each_by_key(key) {|v, time|
	res = [:div, {:class=>"period"}, c_res(v)]
	divs << [:div, {:class=>"era", :id=>time.to_i.to_s}, res]
      }

      return history_show(@req.base, divs)
    end

    def history_show(pagename, divs)
      ar = []

      ar << [:style, "@import '.theme/css/wema.css';"]

      handle = [:div, {:class=>"wema", :id=>"curosr",
	  :style=>"left:0px;top:0px;"},
	[:div, {:class=>"menubar"},
	  [:span, {:class=>"handle"}, "handle"],
	  [:span, {:class=>"close"}, [:a, {:href=>"#"}, " X "]]], # noop
	[:div, {:class=>"cont"},
	  [:span, {:id=>"indicator"}, _("Move this")]]]
      ar << handle

      section = []
      section << [:div, {:id=>"eratable"}, ""]
      section << divs
      section << [:div, {:id=>"lines"}, ""]
      ar << [:div, {:class=>"day"},
	[:div, {:class=>"section"},
	  section]]

      ar << [:script, {:type=>"text/javascript", :src=>".theme/js/wema.js"}, ""]
      ar << [:script, {:type=>"text/javascript",:src=>".theme/js/history.js"}, ""]

      return c_plain(_("Time machine")+" | "+pagename){ar}
    end

  end
end

if $0 == __FILE__
  require "t/test"
  $debug = true
end

if $debug
  class TC_ActHistory < Test::Unit::TestCase
    include TestSession

    def test_act_history
      t_add_user

      assert_wiki([:span, {:class=>"attribute"},
		    [:a, {:href=>"1.history"}, "Show history"], " "],
		  "{{show_history}}")

      page = @site["1"]
      page.store("* t1") # store the first
      page.store("* t2") # store the second

      session("/test/1.html")
      assert_in(["t2"], "//title")

      session("/test/1.history")
      assert_in(["Time machine | 1"], "//title")

    end
  end
end
