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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/qrcode"
require "qwik/qrcode-html"
require "qwik/qrcode-image"
require "qwik/memory"

module Qwik
  class Action
    def plg_qrcode(str=nil)
      y = yield if block_given?
      str = y.chomp if y && y != ""

      if str.nil?
	str = @site.url
	n = @site.sitename
      else
	str = str.to_s
	n = str.md5hex
      end

      f = "qrcode-"+n+".png"
      if !@site.attach.exist?(f)
	res = qrcode_generate(f, str)
	return res if res
      end

      pre = c_pre_text(str)
      ar = [[:img, {:src=>".attach/"+f, :alt=>str}], [:br], pre]
      h = ar
      h = [:a, {:href=>str}, ar] if is_valid_url?(str)
      return [:div, {:class=>"qrcode"}, h]
    end

    def is_valid_url?(str)
      (%r!\Ahttp://[\-\_\.\/\?A-Za-z]+\z! =~ str) != nil
    end

    def qrcode_generate(f, str)
      return if @config.test && !(defined?($test_qrcode) && $test_qrcode)

      q = @memory.qrcode

      begin
	png = q.generate_png(str)
	return [:div, {:class=>"qrcode"}, ""] if png.nil?
      rescue
	return [:div, {:class=>"qrcode"}, ""] # no GD library
      end

      @site.attach.put(f, png)
      nil
    end
  end

  class QRCodeMemory
    def initialize(config, memory)
      @config = config
      @memory = memory
      #path = @config.lib_dir+"/qrcode"
      path = @config.qrcode_dir
      @qrcode = ::QRCode.new(path)
      @qrcodehtml = ::QRCodeHtml.new(path)
      @qrcodeimage = ::QRCodeImage.new(path)
    end

    def generate_data(d)
      begin
	return @qrcode.make_qrcode(d)
      rescue
	qp $!
	qp $!.backtrace
	return ""
      end
    end

    def generate_htree(d)
      @qrcodehtml.generate_htree_from_data(generate_data(d))
    end

    def generate_png(d)
      @qrcodeimage.generate_png_from_data(generate_data(d))
    end
  end
end

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

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

    def test_qrcode_plugin
      $test_qrcode = true

      session

      assert_equal(true, @action.is_valid_url?("http://e.com/"))
      assert_equal(true, @action.is_valid_url?("http://e.com/?"))

      assert_wiki([:div, {:class=>"qrcode"}, [:a, {:href=>"http://example.com/test/"}, [[:img, {:src=>".attach/qrcode-test.png", :alt=>"http://example.com/test/"}], [:br], [[:p, [:a, {:href=>"http://example.com/test/", :class=>"external"}, "http://example.com/test/"]]]]]], "{{qrcode}}")
      assert_equal(true, @site.attach.exist?("qrcode-test.png"))
      str = @site.attach.path("qrcode-test.png").open {|f| f.read }
#      assert_equal("\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000B\000\000\000B\001\003\000\000\000\331\232\222\253\000\000\000\006PLTE\377\377\377\000\000\000U\302\323~\000\000\000\236IDAT\010\231\255\3201\252D1\010\005P!\255\220\255\010i\037d\353\027\322\nnEH+8\217\377\3471\230i\307\3524^\256\022\375z2\266\364D\0218T\251\3124\222\343\224\210~\311q\n\314\374\344=\312;\330\262\212\210/\274{=Z\271\232\030\212\0309[R\221k\337[P\264\305{\237U\236v\3757\370h\272\030-*j\027u\374m|4\a\302\265Jt\2564*\272ooXU\031\210\373!E\3405\200*\323=r\237\202\367q\252\215mU\340\341\355P\206\271K\325o\347\005\342\331\316_\025\372nF\000\000\000\000IEND\256B`\202", str)
      assert_match(/\A\211PNG\r\n/, str)

      assert_wiki([:div, {:class=>"qrcode"}, [[:img, {:src=>".attach/qrcode-cfcd208495d565ef66e7dff9f98764da.png", :alt=>"0"}], [:br], [[:p, "0"]]]], "{{qrcode(0)}}")
      assert_equal(true, @site.attach.exist?("qrcode-cfcd208495d565ef66e7dff9f98764da.png"))
      assert_equal("\211PNG\r\n\032\n\000\000\000\rIHDR\000\000\000:\000\000\000:\001\003\000\000\000\333u\330k\000\000\000\006PLTE\377\377\377\000\000\000U\302\323~\000\000\000|IDAT\010\231\255\3161\n\304P\010\204a!\255\340U\002i\003{\365\001[\301\253\274\003\010\356\203}\t\232m3\325\327\315O\364\3262\322\023\025\340\323\250\301\2159\036\210?\230u\200\223\251!\303\256\257\005\"\305\nY\200\347g\240\202U\364hPv\t\252\010\214\331Z\001\031!\250\230| c\214\215*\300\233\356\rn.\211\216\225Qp\3742n\200\367\323\e\346\305}ze\274\261/Y\240\2213\223\213$\000\000\000\000\000IEND\256B`\202", @site.attach.path("qrcode-cfcd208495d565ef66e7dff9f98764da.png").open {|f| f.read })

      assert_wiki([:div, {:class=>"qrcode"}, [[:img, {:src=>".attach/qrcode-cfcd208495d565ef66e7dff9f98764da.png", :alt=>"0"}], [:br], [[:p, "0"]]]], "{{qrcode\n0\n}}")

      $test_qrcode = nil
    end
  end
end
