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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/act-icon"
require "qwik/act-thumbnail"
require "qwik/act-attach"

module Qwik
  class Action
    # ============================== plg_ref
    def ref_html(href, klass, src, alt, message)
      return [:div, {:class=>"ref"}, 
	[:a, {:href=>href},
	  [:img, {:class=>klass, :src=>src, :alt=>alt}],
	  [:br], message]]
    end

    def plg_ref(f=nil, alt=f)
      if f.nil?
	return [:div, {:class=>"ref"}, attach_form(@req.base+".attach")]
      end

      src = ".attach/"+f

      if ! @site.attach.exist?(f)
	icon = icon_path("broken")
	return ref_html(src, "icon", icon, alt, alt)
      end

      ext = File.extname(f).sub(/\A\./, "")

      if is_image?(ext)
	thumb = thumb_generate(f)
	if thumb # else, go through
	  return ref_html(src, "thumb", thumb, alt, alt)
	end
      end

      icon = icon_find(ext)
      if icon
	return ref_html(src, "icon", icon, alt, alt)
      end

      return [:div, {:class=>"ref"}, 
	[:a, {:href=>src}, alt]]
    end
  end
end

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

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

    def test_all
      # test_ref_plugin
      @site.attach.delete("1x1.png") if @site.attach.exist?("1x1.png")

      assert_wiki([:div, {:class=>"ref"},
		    [:form, {:method=>"post", :action=>"1.attach", :enctype=>"multipart/form-data"},
		      [:input, {:style=>"width:30em;",
			  :type=>"file", :name=>"content"}],
		      [:br],
		      [:input, {:value=>"attach", :type=>"submit"}]]],
		  "{{ref}}")

      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>".attach/1x1.png"},
		      [:img, {:alt=>"1x1.png", :class=>"icon",
			  :src=>".theme/icons/broken.gif"}],
		      [:br], "1x1.png"]],
		  "{{ref(1x1.png)}}")

      png = "\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000\001\000\000\000\001\010\002\000\000\000\220wS\336\000\000\000\fIDATx\332c\370\377\377?\000\005\376\002\3763\022\225\024\000\000\000\000IEND\256B`\202"
      @site.attach.put("1x1.png", png)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>".attach/1x1.png"},
		      [:img, {:alt=>"1x1.png", :class=>"thumb",
			  :src=>".attach/.thumb/1x1.png"}],
		      [:br], "1x1.png"]],
		  "{{ref(1x1.png)}}")

      @site.attach.delete("test.pdf") if @site.attach.exist?("test.pdf")
      pdf = "PDF...dummy"
      @site.attach.put("test.pdf", pdf)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>".attach/test.pdf"},
		      [:img, {:alt=>"test.pdf", :class=>"icon", :src=>".theme/icons/pdf.gif"}],
		      [:br], "test.pdf"]],
		  "{{ref(test.pdf)}}")

      @site.attach.delete("test.txt") if @site.attach.exist?("test.txt")
      file = "text file dummy"
      @site.attach.put("test.txt", file)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>".attach/test.txt"},
		      [:img, {:alt=>"test.txt", :class=>"icon", :src=>".theme/icons/text.gif"}],
		      [:br], "test.txt"]],
		  "{{ref(test.txt)}}")

      if @site.attach.exist?("test.nosuchext")
	@site.attach.delete("test.nosuchext")
      end
      file = "unknown file dummy"
      @site.attach.put("test.nosuchext", file)
      assert_wiki([:div, {:class=>"ref"},
		    [:a, {:href=>".attach/test.nosuchext"},
		      [:img, {:alt=>"test.nosuchext", :class=>"icon", 
			  :src=>".theme/icons/generic.gif"}],
		      [:br], "test.nosuchext"]],
		  "{{ref(test.nosuchext)}}")
    end
  end
end
