# 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_notice # only for check design
      w = login_page("http://example.com/HelloQwik/")
      c_notice("act_test_notice"){w}
    end

    def c_notice(title, url=nil, status=200, &b)
      msg = yield
      generate_notice_page(status, title, url, msg)
    end

    def c_nerror(title, url=nil, status=500, &b)
      msg = ""
      msg = yield if block_given?
      generate_notice_page(status, title, url, msg)
    end

    def c_nredirect(title, url, &b)
      msg = ""
      msg = yield if block_given?
      @res["Location"] =  c_relative_to_full(url)
      generate_notice_page(302, title, url, msg)
    end

    def generate_notice_page(status, title, url=nil, msg="")
      #url = nil # for debug
      @res.status = status
      template = @memory.template.get("notice")
      @res.body = Action.notice_generate(template, title, msg, url)
      c_set_html
      c_set_no_cache
      nil
    end

    def self.notice_generate(template, title, msg, url=nil, redirectflag=false)
      w = template.get_tag("head")

      # insert title
      w.insert(1, [:title, title])

      # insert JavaScript
      js = GeneratorModule.generate_js("/")
      w.insert(w.length, *js)

      # insert meta
      w << [:meta, {:name=>"ROBOTS", :content=>"NOINDEX,NOFOLLOW"}]
      if url && !redirectflag # redirect
	w << [:meta, {"http-equiv"=>"Refresh", :content=>"0; url=#{url}"}]
      end

      # insert h1
      w = template.get_tag("h1")
      w << title

      # insert section
      w = template.get_by_class("section")
      w << msg

      template
    end

  end
end

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

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

    def test_all
      res = session

      @action.generate_notice_page(200, "title"){"msg"}
      assert_equal(200, res.status)
      assert_xpath([:title, "title"], "title", res)

      @action.generate_notice_page(200, "title", "u"){"msg"}
      assert_equal(200, res.status)
      assert_xpath([:title, "title"], "title", res)
      assert_xpath([:meta, {:content=>"0; url=u", "http-equiv"=>"Refresh"}],
		   "meta[2]", res)

      @action.c_notice("c_notice title"){"msg"}
      assert_equal(200, res.status)
      assert_xpath([:title, "c_notice title"], "title", res)
      @action.c_nerror("c_notice_error title"){"msg"}
      assert_equal(500, res.status)
      assert_xpath([:title, "c_notice_error title"], "title", res)

      t_add_user

      res = session("/test/.test_notice")
      assert_xpath([:title, "act_test_notice"], "title", res)
    end

    def test_c_nredirect
      session
      @action.c_nredirect("t", "t.html")
      #assert_equal("http://127.0.0.1:9190/t.html", @res["Location"])
      assert_match(/\Ahttp:/, @res["Location"])
      @action.c_nredirect("t", "http://e.com/")
      assert_equal("http://e.com/", @res["Location"])
    end

    def test_notice_generate
      template = @memory.template.get("notice")
      assert_equal([:h1], template.get_tag("h1"))
      assert_equal([:div, {:class=>"section"}],
		   template.get_by_class("section"))

      res = Qwik::Action.notice_generate(template, "title", "msg")
      assert_equal([:title, "title"], res.get_tag("title"))
      assert_equal([:script, {:src=>"/.theme/js/base.js",
		       :type=>"text/javascript"}, ""], res.get_tag("script"))
      assert_equal([:meta, {:content=>"NOINDEX,NOFOLLOW", :name=>"ROBOTS"}],
		   res.get_tag("meta"))
      assert_equal([:h1, "title"], res.get_tag("h1"))
      assert_equal([:div, {:class=>"section"}, "msg"],
		   res.get_path("//div[@class='section']"))
      assert_equal(nil, res.get_tag("meta[2]")) # not redirected

      template = @memory.template.get("notice")
      res = Qwik::Action.notice_generate(template, "title", "msg", "url")
      assert_equal([:meta, {:content=>"0; url=url", "http-equiv"=>"Refresh"}],
		   res.get_tag("meta[2]")) # redirected
    end
  end
end
