# 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_editor # only for check design
      w = c_get("TextFormatSimple")
      #w = c_get("TextFormat")
      #w = login_page("http://example.com/HelloQwik/")
      generate_editor_page("act_test_editor", "h1", w, w, w)
    end

    def c_editor(site, pagename, contents=nil, message=[""])
      page = site[pagename]

      page_title = page.get_title

      title = _("Edit")+" | "+page_title

      h1 = [_("Edit"), " | ", [:a, {:href=>page.url}, page_title]]

      str = contents
      str = page.load if contents.nil?

      md5hex = str.md5hex

      edit_form = editor_edit_form(pagename, str, md5hex)

      attach_form = editor_attach_form(pagename)

      main = editor_main(edit_form, attach_form, pagename)

      ar = []
      ar << editor_site_admin
      ar << editor_edit_help
      ar << editor_edit_history(pagename)
      ar << editor_experimental(pagename)
      sidebar = editor_sidebar(*ar)

      generate_editor_page(title, h1, message, main, sidebar)
    end

    def editor_edit_form(pagename, str, md5hex)
      [:form, {:method=>"POST", :action=>pagename+".save"},
	[:textarea, {:name=>"contents", :id=>"contents",
	    :cols=>"70", :rows=>"20", :class=>"focus"}, str],
	[:div,
	  [:input, {:type=>"hidden", :name=>"md5hex", :value=>md5hex}],
	  [:input, {:type=>"submit", :name=>"save", :value=>_("Save")}]]]
    end

    def editor_attach_form(pagename)
      [:form, {:action=>pagename+".files",
	  :method=>"POST", :enctype=>"multipart/form-data"},
	[:input, {:type=>"file", :name=>"content", :size=>"50"}],
	[:input, {:type=>"submit", :value=>_("Attach")}]]
    end

    def editor_main(edit_form, attach_form, pagename)
      [
	[:div, {:class=>"day edit"},
	  [:h2, _("Edit")],
	  [:div, {:class=>"section"}, edit_form]],
	[:div, {:class=>"day attach"},
	  [:h2, _("Attach Files")],
	  [:div, {:class=>"section"},
	    attach_form,
	    [:p, [:a, {:href=>pagename+".files"}, _("Attach many files")]]]]
      ]
    end

    def editor_site_admin
      [:div, {:class=>"section edithistory"},
	[:h2, _("Site admin")],
	[:ul,
	  [:li, [:a, {:href=>"_SiteMenu.html"}, _("SiteMenu")]]]]
    end

    def editor_edit_help
      [:div, {:class=>"section edithelp"},
	[:h2, _("Text Format")],
	[:ul,
	  [:li, [:tt, "*"], " ", _("Header")],
	  [:li, [:tt, "-"], " ", _("List")],
	  [:li, [:tt, "+"], " ", _("Ordered list")],
	  [:li, [:tt, ">"], " ", _("Blockquote")],
	  [:li, [:tt, ":"], " ", _("Word"), ": ", _("Definition")],
	  [:li, [:tt, ","], " ", _("Table")],
	  [:li, [:tt, "''"], _("Emphasis"), [:tt, "''"]],
	  [:li, [:tt, "'''"], _("Strong"), [:tt, "'''"]],
	  [:li, [:tt, "[["], "FrontPage", [:tt, "]]"], " ", _("Link")]],
	[:p, [:a, {:href=>"TextFormat.html"}, _("more help")]]]
    end

    def editor_edit_history(pagename)
      [:div, {:class=>"section edithistory"},
	[:h2, _("Edit History")],
	[:ul,
	  [:li, [:a, {:href=>pagename+".backup"}, _("Backup")]],
	  [:li, [:a, {:href=>pagename+".history"}, _("Time machine")]]]]
    end

    def editor_experimental(pagename)
      [:div, {:class=>"section edithistory"},
	[:h2, _("Experimental functions")],
	[:ul,
	  [:li, [:a, {:href=>pagename+".presen"}, _("Presentation mode")]],
	  [:li, [:a, {:href=>pagename+".wysiwyg"}, _("Edit in this page")]],
	]]
    end

    def editor_sidebar(*a)
      a
    end

    def generate_editor_page(title, h1, message, main, sidebar)
      @res.status = 200
      template = @memory.template.get("editor")
      @res.body = Action.editor_generate(template, title, h1,
				       message, main, sidebar)
      c_set_html
      c_set_no_cache
      nil
    end

    def self.editor_generate(template, title, h1, message, main, sidebar)
      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"}]

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

      # insert message
      w = template.get_by_class("message")
      w << message

      # insert main
      w = template.get_by_class("main")
      w.insert(2, *main)

      # insert sidebar
      w = template.get_by_class("sidebar")
      w.insert(2, *sidebar)

      template
    end

  end
end

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

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

    def test_generator
      session

      template = @memory.template.get("editor")
      assert_equal([:h1], template.get_tag("h1"))
      assert_equal([:div, {:class=>"main"}],
		   template.get_by_class("main"))

      res = Qwik::Action.editor_generate(template, "title", "h1",
					 "msg", ["m"], ["s"])

      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([:link, {:href=>"/.theme/css/base.css", :rel=>"stylesheet",
		       :media=>"screen,tv", :type=>"text/css"}],
		   res.get_tag("link"))
      assert_equal([:link, {:media=>"screen,tv", :type=>"text/css",
		       :href=>"/.theme/qwikeditor/qwikeditor.css",
		       :rel=>"stylesheet"}],
		   res.get_tag("link[2]"))
      assert_equal([:meta, {:content=>"NOINDEX,NOFOLLOW", :name=>"ROBOTS"}],
		   res.get_tag("meta"))
      assert_equal([:h1, "h1"], res.get_tag("h1"))
      assert_equal([:div, {:class=>"message"}, "msg"],
		   res.get_path("//div[@class='message']"))
      assert_equal([:div, {:class=>"main"}, "m"],
		   res.get_path("//div[@class='main']"))
      assert_equal([:div, {:class=>"sidebar"}, "s"],
		   res.get_path("//div[@class='sidebar']"))
    end

    def test_all
      res = session

      @action.generate_editor_page("title", "h1", "msg", ["ma"], ["si"])
      assert_equal(200, res.status)
      assert_xpath([:title, "title"], "title", res)
      assert_xpath([:div, {:class=>"message"}, "msg"],
		   "//div[@class='message']")
      assert_xpath([:div, {:class=>"main"}, "ma"],
		   "//div[@class='main']")
      assert_xpath([:div, {:class=>"sidebar"}, "si"],
		   "//div[@class='sidebar']")

      t_add_user

      res = session("/test/.test_editor")
      #assert_xpath([:title, "Edit | 1"], "title", res)
      #assert_xpath([:title, "act_test_editor"], "title", res)

      page = @site.create_new
      page.store("t")
      @action.c_editor(@site, "1")
      assert_xpath([:title, "Edit | 1"], "title")
      assert_xpath([:div, {:class=>"message"}, [""]],
		   "//div[@class='message']")

      # edit form
      assert_rattr({"action"=>"1.save", "method"=>"POST"},
		   "//div[@class='day edit']/form")
      assert_xpath([:textarea, {:id=>"contents", :name=>"contents",
		       :cols=>"70", :rows=>"20"}, "t"],
		   "//div[@class='day edit']/form/textarea")
      assert_xpath([:input, {:value=>"e358efa489f58062f10dd7316b65649e",
		       :type=>"hidden", :name=>"md5hex"}],
		   "//div[@class='day edit']/form/input")
      assert_xpath([:input, {:value=>"Save", :type=>"submit", :name=>"save"}],
		   "//div[@class='day edit']/form/input[2]")

      # attach form
      assert_rattr({"enctype"=>"multipart/form-data",
		     "action"=>"1.files", "method"=>"POST"},
		   "//div[@class='day attach']/form")
      assert_xpath([:input, {:size=>"50", :type=>"file", :name=>"content"}],
		   "//div[@class='day attach']/form/input")
      assert_xpath([:input, {:value=>"Attach", :type=>"submit"}],
		   "//div[@class='day attach']/form/input[2]")
      assert_xpath([:p, [:a, {:href=>"1.files"}, "Attach many files"]],
		   "//div[@class='day attach']/p")

#      assert_rattr({"href"=>"TextFormat.html"},
#		   "//div[@class='sidebar']//a")
    end

    def test_editor
      session

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

      session
      @action.c_editor(@site, "1", "contents", "msg")
      assert_text("Edit | test", "title")
      assert_text("Edit | test", "h1")
      assert_attr({"name"=>"ROBOTS", "content"=>"NOINDEX,NOFOLLOW"}, "meta")
      assert_in(["msg"], "//div[@class='message']")
      assert_attr({"action"=>"1.save", "method"=>"POST"},
		  "form")
      assert_text("contents", "textarea")
      assert_attr({"name"=>"md5hex", "type"=>"hidden",
		    "value"=>"98bf7d8c15784f0a3d63204441e1e2aa"}, "input")
      assert_attr({"name"=>"save", "type"=>"submit", "value"=>"Save"},
		  "//input[2]")
#      assert_rattr({"href"=>"TextFormat.html"},
#		   "//div[@class='sidebar']//a")
    end
  end
end
