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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")

module Qwik
  class Action
    def act_theme
      args = @req.path_args
      path = @config.theme_dir.path+(args.join("/"))
      ext = path.extname.sub(/\A\./, "")
      type = @res.mimetypes[ext]
      c_simple_send(path, type)
    end

    def plg_theme_list
      [:ul, theme_list.map {|t| [:li, t] }]
    end

    def theme_list
      themes = []
      theme_path = @config.theme_dir.path
      theme_path.each_entry {|d|
	dir = theme_path+d
	next unless dir.directory?
	s = d.to_s
	next if /\A\./ =~ s || /\Asfc/ =~ s
	next if s == "CVS" || s == "icons" || s == "s5"
	themes << d.to_s
      }
      themes
    end

    def theme_files(theme)
      dir = @config.theme_dir.path+theme
      return unless dir.directory?
      files = []
      dir.each_entry {|f|
	file = dir+f
	next if file.directory?
	s = f.to_s
	next if /\A\./ =~ s
	next if s == "CVS"
	files << f.to_s
      }
      files
    end
  end
end

if $0 == __FILE__
  require "t/test"
  require "qwik/generator-view"
  $debug = true
end

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

    def test_all
      # test_plg_theme_list
      assert_wiki(/<li>qwikborder<\/li>/, "{{theme_list}}")
      assert_wiki(/<li>qwiksystem<\/li>/, "{{theme_list}}")

      # test_theme_list
      session
      list = @action.theme_list
      assert_equal(true, list.include?("qwiksystem"))
      assert_equal(true, list.include?("qwikborder"))
      assert_equal(true, 4 <= list.length) # at least 4 themes

      # test_theme_files
      list = @action.theme_files("qwikborder")
      assert_equal(["qwikborder.css", "qwikborder_ball.png",
		     "qwikborder_h2.png", "qwikborder_li.png"], list.sort)
      assert_equal(true, 4 <= list.length) # at least 4 themes

=begin
      # test_theme_path
      org_site = @site
      @site = @memory.farm.get_site("www")

      page = @site.create_new
      page.store("* h2\ntext")
      gen = Qwik::ViewPageGenerator.new(@config, @memory, @action)

      d = @dir+".attach"
      d.erase_all_for_test if d.exist?

      page = @site["_SiteConfig"]
      page.store(":theme:q")
      assert_equal("q", @site.theme)
      assert_equal(".theme/q/q.css", @site.theme_path)

      assert_equal("* h2\ntext", @site["1"].load)
      assert_match(/\A\* menu/, @site["_SideMenu"].load)
      assert_match(/qrcode/, @site["_PageAttribute"].load)

      @res.body = gen.generate(@site, "1", @site.theme)
      assert_xpath([:link,
		   {:rel=>"stylesheet",
		     :href=>".theme/css/base.css",
		     :media=>"screen,tv,print",
		     :type=>"text/css"}],
		 "//link")

      page.store(":theme:http://e.com/q.css")
      assert_equal("http://e.com/q.css", @site.theme)
      assert_equal("/.css/http://e.com/q.css", @site.theme_path)
      @res.body = gen.generate(@site, "1", @site.theme)
      assert_xpath([:link,
		     {:media=>"screen,tv,print",
		       :type=>"text/css",
		       :rel=>"stylesheet",
		       :href=>"/.css/http://e.com/q.css"}],
		   "//link[2]")

      @site.attach.delete("test.css") if @site.attach.exist?("test.css")
      @site.attach.put("test.css", "q")
      assert_equal(true, @site.attach.exist?("test.css"))
      assert_equal("/.css/http://e.com/q.css", @site.theme_path)
      @res.body = gen.generate(@site, "1", @site.theme)
      assert_rattr({"href"=>".theme/css/base.css", "rel"=>"stylesheet",
		     "type"=>"text/css", "media"=>"screen,tv,print"}, "//link")
      assert_xpath([:link,
		     {:media=>"screen,tv,print",
		       :type=>"text/css",
		       :rel=>"stylesheet",
		       :href=>"/.css/http://e.com/q.css"}],
		   "//link[2]")

      @site = org_site
=end
    end
  end
end
