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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/generator-view"
require "qwik/act-simple-send"
require "qwik/act-surface"

module Qwik
  class Action
    def ext_html
      c_require_page_exist
      c_require_member if private_page?(@req.base)

      unless c_login?
	#c_monitor("view") # do not check.
	return view_page_cache_check
      end

      #c_make_log("view") # do not check.
      #c_monitor("view")
      # @res.body = view_page_generate(@req.base)
      surface_view(@req.base)
    end

    def private_page?(pagename)
      return /\A_/ =~ pagename
    end

    def view_page_generate(pagename)
      gen = ViewPageGenerator.new(@config, @memory, self)
      gen.generate(@site, pagename, @site.theme)
    end

    def view_page_cache_check
      c_set_html
      #c_set_no_cache	# You can cache the page.

      if @config.test
	w = view_page_cache_generate(@req.base)
	@res.body = w
	return w
      end

      f = view_page_cache_path(@req.base)
      unless f.exist? && @site.last_page_time <= f.mtime
	view_page_cache_generate(@req.base)
      end

      c_simple_send(f.to_s, "text/html; charset=Shift_JIS")
    end

    # called from act-archive
    def view_page_cache_generate(pagename)
      #w = view_page_generate(pagename)
      w = surface_view_generate(pagename)
      str = w.format_xml	# with indent
      view_page_cache_store(pagename, str)
      return w
    end

    def view_page_cache_store(pagename, str)
      fp = view_page_cache_path(pagename)
      fp.open("wb") {|f| f.print str }		# write it to file
    end

    def view_page_cache_path(pagename)
      path = @site.dir.path
      (path+(pagename+".html")).cleanpath
    end
  end
end

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

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

    def test_all
      session
      assert_equal(nil, @action.private_page?("t"))
      assert_equal(0, @action.private_page?("_t"))

      t_add_user

      # test_protect_underbar
      page = @site.create("_t")
      page.store("*t")

      session("/test/_t.html") # with login
      assert_text("t", "title")

      session("/test/_t.html") {|req|
	req.cookies.clear
      }
      assert_text("Login", "title")

      t_site_open

      # test_ext_html
#      @site.erase_all
      page = @site["1"]
      assert_nil(page)
      
      session("/test/1.html")
      assert_equal(404, @res.status)
      assert_text("Page not found.", "title")

      page = @site.create_new
      page.store("t")

      session("/test/1.html")
      assert_xml("t", "//div[@class='section']/p")

      # test_cache
      session("/test/") {|req|
	req.cookies.clear
      }
      assert_text("FrontPage", "title") # you can see the page.
      assert_xml("Login", "//div[@class='adminmenu']//a") # not logged in.
      assert_equal("text/html; charset=Shift_JIS", @res.headers["Content-Type"])

      org_test = @config.test
      @config.test = false

      # do it again
      session("/test/") {|req|
	req.cookies.clear
      }
      assert_instance_of(File, @res.body) # is cached content
      str = @res.body.read
      @res.body.close # important
      assert_match(/FrontPage/, str)
      assert_equal("text/html; charset=Shift_JIS", @res.headers["Content-Type"])

      @config.test = org_test
    end
  end
end
