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

module Qwik
  class Action
    def plg_book(key)
      page = @site["_BookSearch"]
      ar = []
      ar << "book:#{key} "
      page.wikidb.array.each {|name, args|
	url, encoding = args
	sk = key
	sk = sk.escape if encoding == "sjis"
	sk = sk.sjistoeuc.escape  if encoding == "euc"
	sk = sk.sjistou8.escape if encoding == "utf8"
	url = url.index("$1") ? url.sub(/\$1/, sk) : url+sk
	ar << [:a, {:href=>url}, name]
	ar << " "
      }
      return [:div, {:class=>"box"}, ar]
    end

    def plg_isbn_amazon(isbn, t=nil)
      isbn = isbn.to_s
      msg = t
      msg = "isbn:"+isbn if t.nil?
      a = @site.siteconfig["aid"]
      aid = "/"+a if a
      return [:a, {:href=>"http://www.amazon.co.jp/exec/obidos/ASIN/#{isbn}#{aid}/ref=nosim/"}, msg]
    end

    def plg_isbn(isbn, t=nil)
      isbn = isbn.to_s
      isbn1 = isbn.gsub(/ISBN/i, "")
      isbn2 = isbn1.gsub(/-/, "")
      a = @site.siteconfig["aid"]
      aid = "/"+a if a

      link = @site["_IsbnLink"]
      db = link.wikidb
      ar = []

      msg = t
      msg = "isbn:"+isbn if t.nil?

      ar << msg
      ar << " "

      db.array.each {|name, args|
	#qp name, args
	args = args.dup
	url = args.shift
	next if url.nil?

	url = url.sub_str('#{isbn1}', isbn1)
	url = url.sub_str('#{isbn2}', isbn2)
	url = url.sub_str('#{aid}',   aid)
	ar << [:a, {:href=>url}, name]
	ar << " "
      }

      return [:div, {:class=>"box"}, ar]
    end

  end
end

if $0 == __FILE__
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "t/test"
  $debug = true
end

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

    def test_book
      page = @site["_BookSearch"]
      page.store(',utf8,http://example.com/utf8/,utf8
,sjis,http://example.com/sjis/,sjis
,euc,http://example.com/euc/,euc')

      assert_wiki([:div, {:class=>"box"},
		    ["book:–{ ",
		      [:a, {:href=>"http://example.com/utf8/%E6%9C%AC"},
			"utf8"], " ",
		      [:a, {:href=>"http://example.com/sjis/%96%7B"},
			"sjis"], " ",
		      [:a, {:href=>"http://example.com/euc/%CB%DC"},
			"euc"], " "]],
		  "{{book(–{)}}")

      # test_isbn
      #<"<a href=\"http://www.amazon.co.jp/exec/obidos/ASIN/4797318325/q02-22/ref=nosim/\">isbn:4797318325</a>">.
      assert_wiki("<a class=\"external\" href=\"http://www.amazon.co.jp/exec/obidos/ASIN/4797318325/q02-22/ref=nosim/\">isbn:4797318325</a>", "{{isbn_amazon(4797318325)}}")
      assert_wiki("<a class=\"external\" href=\"http://www.amazon.co.jp/exec/obidos/ASIN/4797318325/q02-22/ref=nosim/\">t</a>", "{{isbn_amazon(4797318325,t)}}")

      page = @site["_IsbnLink"]
      page.store(',i1,http://example.com/i1/#{isbn1}
,i2,http://example.com/i2/#{isbn2}
,aid,http://example.com/aid/#{isbn2}#{aid}')

      assert_wiki("<div class=\"box\">isbn:4797318325 <a href=\"http://example.com/i1/4797318325\">i1</a> <a href=\"http://example.com/i2/4797318325\">i2</a> <a href=\"http://example.com/aid/4797318325/q02-22\">aid</a> </div>", "{{isbn(4797318325)}}")
    end
  end
end
