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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/template"
require "qwik/wabisabi-get"
require "qwik/generator-module"

module Qwik
  class Action
    def act_test_surface # only for test
      surface_view("TextFormatSimple")
    end

    def get_super(site, k)
      return c_get(k) if site.exist?(k)
      k = "_"+k
      return c_get(k) if site.exist?(k)
      raise "no such super file" # assert
    end

    def surface_view(pagename)
      @res.body = surface_view_generate(pagename)
      @res.status = 200
      c_set_html
      c_set_no_cache
    end

    def surface_view_generate(pagename)
      site = @site
      page = site[pagename]
      h1 = page.get_title
      title = h1
      if site.title != ""
	title = site.title+" - "+h1
      else
	t = site.url
	t = t.sub(/\Ahttp:\/\//, "")
	t = t.sub(/\/\z/, "")
	title = t+" - "+h1
	title = h1
      end
      theme_path = site.theme_path
      toc = toc_inside
      bodystr = page.get_body
      body = c_res(bodystr)
      tres = TDiaryResolver.new(@config, site, self)
      body = tres.resolve(body)
      sidemenu = get_super(site, "SideMenu")
      # insert the values into template
      template = @memory.template.get("surface")
      surface_template(template, title, theme_path, toc, h1, body, sidemenu)
      return template
    end

    def surface_template(template, title, theme_path, toc, h1, body, sidemenu)
      w = template.get_tag("head")
      w.insert(1, [:title, title])
      w.insert(w.length, [:link, {:rel=>"stylesheet", :type=>"text/css",
		   :href=>".theme/css/base.css", :media=>"screen,tv"}])
      w.insert(w.length, [:link, {:rel=>"stylesheet", :type=>"text/css",
		   :href=>theme_path, :media=>"screen,tv"}])
      js = GeneratorModule.generate_js("/")
      w.insert(w.length, *js)
      w = template.get_by_class("adminmenu")
      w << c_get("_AdminMenu")
      w = template.get_by_class("toc")
      if toc.nil?
	w.clear
      else
	w << toc
      end
      w = template.get_tag("h1")
      w << h1
      w = template.get_by_class("body_enter")
      w.clear
      w = template.get_by_class("body_main")
      w << body
      w = template.get_by_class("sidebar")
      w << sidemenu
      w = template.get_by_class("body_leave")
      pageattribute = c_get("_PageAttribute")
      w << [:div, {:class=>"day"},
	[:div, {:class=>"comment"},
	  [:div, {:class=>"caption"},
	    [:div, {:class=>"page_attribute"},
	      pageattribute]]]]
      nil
    end

  end
end

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

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

    def test_all
=begin
      res = session
      @action.generate_surface_page("title", "top", "h1", "main", "sidebar")
      assert_equal(200, res.status)
      assert_xpath([:title, "title"], "title", res)
      assert_xpath([:div, {:class=>"sidebar"}, "sidebar"],
		   "//div[@class='sidebar']")
=end
      t_add_user
      res = session("/test/.test_surface")
    end
  end
end
