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

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

module Qwik
  class Action
    def act_list
      str = @site.title_list.map {|page|
	"- "+page.mtime.ymd+" : [[#{page.key}]]"
      }.join("\n")
      msg = _("Page List")
      c_plain(msg){
	[:div, {:class=>"day"},
	  [:div, {:class=>"section"},
	    c_res(str)]]
      }
    end

    def act_recent(max = -1)
      msg = _("Recent Changes")
      str = recent_str(max)
      c_plain(msg){
	[:div, {:class=>"day"},
	  [:div, {:class=>"section"},
	    c_res(str)]]
      }
    end

    def plg_recent(max = -1)
      c_res(recent_str(max))
    end

    def plg_srecent(max = -1)
      max = max.to_i
      ar = []
      now = Time.now
      list = @site.date_list
      list.reverse.each_with_index {|page, i|
	break if 0 <= max && max < (i += 1)
	time = page.mtime
	difftime = now.to_i - time.to_i
	timestr = time.ymdx.to_s
	li = [:li,
	  [:a, {:href=>page.key+".html", :title=>timestr}, page.get_title]]
	if @req.user
	  li += [" ", [:span, {:class=>"ago"}, int_to_time(difftime)+_(" ago")]]
	end
	ar << li
      }
      [:div, {:class=>"recent"}, [:ul, ar],
	[:p,{:class=>"recent"}, [:a, {:href=>".recent"}, [:em, _("more...")]]]]
    end

    def recent_str(max = -1)
      max = max.to_i
      ar = []
      last_day = nil
      add_day = true
      @site.date_list.reverse.each_with_index {|page, i|
	break if 0 <= max && max < (i += 1)
	if add_day
	  day = page.mtime.ymd.to_s
	  if last_day.nil? || day != last_day
	    ar << "** #{day}"
	    last_day = day
	  end
	end
	ar << "- [[#{page.key}]]"
      }
      ar.join("\n")
    end

    def int_to_time(n)
      return n.to_s+_("sec.") if n < 60 # under 1 min 
      return (n/60).to_s+_("min.") if n < 60*60 # under 1 hour
      return (n/(60*60)).to_s+_("hour") if n < 60*60*24 # under 1 day
      return (n/(60*60*24)).to_s+_("day") if n < 60*60*24*30 # under 1 month
      return (n/(60*60*24*30)).to_s+_("month") if n < 60*60*24*365 # under 1 year
      return (n/(60*60*24*365)).to_s+_("year") if n < 60*60*24*365*100 # under 1 century
      return (n/(60*60*24*365*100)).to_s+_("century")
    end
  end
end

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

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

    def assert_time(e, n)
      assert_equal(e, @action.int_to_time(n))
    end

    def test_all
      t_add_user

      session("/test/.list")
      assert_text("Page List", "title")

      session("/test/.recent")
      assert_text("Recent Changes", "title")

      #assert_wiki(/<ul><li>/, "{{list}}")
      #assert_wiki(%r| : <a href="1.html">t</a></li>|, "{{list}}")
      assert_wiki(/<h3>/, "{{recent}}")
      assert_wiki(%r|<li><a href=\"1.html\">1</a></li>|, "{{recent}}")
      assert_wiki(/<h3>/, "{{recent(1)}}")

      # test_time
      assert_time("1sec.", 1)
      assert_time("1min.", 60)
      assert_time("1hour", 60*60)
      assert_time("1day", 60*60*24)
      assert_time("1month", 60*60*24*30)
      assert_time("1year", 60*60*24*365)
      assert_time("1century", 60*60*24*365*100)
    end
  end
end

