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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/util-time"

module Qwik
  class Action
    def plg_null
      ""
    end

    def plg_test
      "test"
    end

    def plg_br
      [:br]
    end

    def plg_menu(cmd, msg=nil)
      return "" if @req.user.nil?
      return "" if !defined?(@req.base) || @req.base.nil?
      case cmd
      when "newpage"
	return [:a, {:href=>".new"}, _("newpage")]
      when "edit"
	return [:a, {:href=>@req.base+".edit"}, _("edit")]
      when "wysiwyg"
	return [:a, {:href=>@req.base+".wysiwyg"}, _("wysiwyg")]
      else
	return
      end
    end

    def plg_last_modified
      return "" if !defined?(@req.base) || @req.base.nil?
      page = @site[@req.base]
      return "" if page.nil?
      date = page.mtime
      [:span, {:class=>"attribute"}, _("Last modified"), ": ",
	[:em, date.ymd]]
    end

    def plg_generate_time
      return "" if @req.user.nil?
      diff = Time.now - @req.start_time
      diffsec = sprintf("%.2f", diff)
      [:span, {:class=>"attribute"}, _("Generate time"), ": ",
	[:em, diffsec, _("sec.")]]
    end

    def plg_show_sitemenu
      [:span, {:class=>"attribute"},
	[:a, {:href=>"_SiteMenu.html"}, _("SiteMenu")]]
    end

    def plg_only_guest
      return if @req.user
      s = yield
      return if s.nil?
      c_res(s)
    end

    def plg_only_member
      return if @req.user.nil?
      s = yield
      return if s.nil?
      c_res(s)
    end

    def plg_window(url, text=nil)
      text = url if text.nil?
      [:a, {:href=>url, :target=>"_blank"}, text]
    end

  end
end

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

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

    def test_all
      # test_null
      assert_wiki([""], "{{null}}")

      # test_test
      assert_wiki(["test"], "{{test}}")

      # test_br
      assert_wiki([:br], "{{br}}")

      # test_menu
      assert_wiki([:a, {:href=>".new"}, "newpage"], "{{menu(newpage)}}")
      assert_wiki([:a, {:href=>"1.edit"}, "edit"], "{{menu(edit)}}")
      assert_wiki([], "{{menu(nosuchmenu)}}")
      # test for not logined mode
      #assert_wiki("", "{{menu(newpage)}}", nil)
      #assert_wiki("", "{{menu(edit)}}", nil)
      #assert_wiki("", "{{menu(nosuchmenu)}}", nil)

      # test_last_modified
      assert_wiki(/Last modified: /, "{{last_modified}}")

      # test_generate_time
      assert_wiki(/Generate time: /, "{{generate_time}}")

      # test_show_sitemenu
      assert_wiki([:span, {:class=>"attribute"},
		    [:a, {:href=>"_SiteMenu.html"}, "SiteMenu"]],
		  "{{show_sitemenu}}")

      # test_only_member_or_guest
      assert_wiki([:p, "m"], "{{only_member\nm\n}}")
      assert_wiki([], "{{only_member\nm\n}}", nil)
      assert_wiki([], "{{only_guest\ng\n}}")
      assert_wiki([:p, "g"], "{{only_guest\ng\n}}", nil)

      # test_window
      assert_wiki([:a, {:target=>"_blank", :href=>"url"}, "t"],
		  "{{window(url,t)}}")
      assert_wiki([:a, {:target=>"_blank", :href=>"url"}, "url"],
		  "{{window(url)}}")
    end
  end
end
