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

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

module Qwik
  class ViewPageGenerator
    include GeneratorModule

    def initialize(config, memory, action)
      @config = config
      @memory = memory
      @action = action
    end

    def generate(site, pagename, theme=nil)
      data = {}

      page = site[pagename]
      title = page.get_title
      str = page.get_body
      generate_by_str(data, site, title, str)

      data[:adminmenu] = get_super(site, "AdminMenu")

      data[:toc] = @action.toc_inside

      pageattribute = get_super(site, "PageAttribute")

      data[:body_leave] = [
	[:div, {:class=>"day"},
	  [:div, {:class=>"comment"},
	    [:div, {:class=>"caption"},
	      [:div, {:class=>"page_attribute"},
		pageattribute,
		[:br, {:style=>"clear:both;"}]],
	      [:"!--", "page_attribute"]],
	    [:"!--", "caption"]],
	  [:"!--", "comment"],
	  [:div, {:class=>"body_leave"}, ""],
	  [:"!--", "body_leave"]],
	[:"!--", "day"]
      ]

      apply_template(data, "view")
    end

    def set_view_meesage_page_adminmenu(data, site)
      data[:adminmenu]  = @action.c_res("{{login_status}}\n- .")
    end

    def apply_template(data, template)
      xml = @memory.template.get(template)
      xml.apply(data)
    end

    def generate_by_str(data, site, title, str)
      w = @action.c_res(str)
      tres = TDiaryResolver.new(@config, site, @action)
      w = tres.resolve(w)
      generate_by_wabisabi(data, site, title, w)
    end

    def get_window_title(site, title)
      site_title = site.title
      if site_title != ""
	#window_title = title+" - "+site_title
	window_title = site_title+" - "+title
      else
	window_title = title
      end
      return window_title
    end

    def generate_by_wabisabi(data, site, title, wabisabi)
      #window_title = get_window_title(site, title)
      window_title = title
      
      theme_path = site.theme_path

      data[:body] = wabisabi

      head = []
#     head << [:title, page_title]
#     head << [:title, title]
      head << [:title, window_title]
      m = "screen,tv,print"
      head << [:link, {:rel=>"stylesheet", :type=>"text/css",
	  :href=>".theme/css/base.css", :media=>m}]
      head << [:link, {:rel=>"stylesheet", :type=>"text/css",
	  :href=>theme_path, :media=>m}]
#      head << [:link, {:rel=>"stylesheet", :type=>"text/css",
#	  :href=>".theme/ap/ArekorePopup.css", :media=>m}]

      head += generate_js

      data[:view_title] = title
      data[:body_enter] = nil
      data[:header] = head
      data[:sidemenu]   = get_super(site, "SideMenu")
      data[:footer]     = get_super(site, "SiteFooter")
    end

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

if $0 == __FILE__
  require "qwik/test-common"
  $debug = true
end

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

    def test_all
      session

      # test_view_page_generator
      page = @site.create_new
      page.store("* h2\ntext")
      gen = Qwik::ViewPageGenerator.new(@config, @memory, @action)
      @res.body = gen.generate(@site, page.key, "qwikgreen")
      assert_text("h2", "title")
      assert_text("h2", "h1")
      assert_xml("text", "//div[@class='section']//p")
      assert_xml("menu", "//div[@class='sidebar']//h2")

      # test_basic_structure
      t_add_user
      page = @site.create_new
      page.store("* test\ntestbody\n** h3\nh3body\n* h2\nh2body\n")
      side = @site["_SideMenu"]
      side.store("* side\nsidebody [[1]]")

      # See the basic structure of the page
      session("/test/#{page.key}.html")
      assert_text("test", "title")
      assert_xml('test', "//div[@class='main']/h1")
      assert_text("h2", "h2")

      assert_xml("h3", "//div[@class='body']//h3")
      x = @res.body.get_path("//div[@class='body']//h3")
      assert_equal("h3", x.extract_text.to_s)
      assert_xml("h3", "//div[@class='day']/h3")
      assert_xml("testbody", "//div[@class='day']//p")
      assert_xml("side", "//div[@class='sidebar']//h2")
      assert_xml("h2", "//div[@class='sidebar']//a")
    end
  end
end
