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

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/smil"
require "qwik/media"

module Qwik
  class Action
    def ext_smil
      # $smil_debug = true
      $smil_debug = false
      ar = []
      ar << @req.base
      ar += @req.ext_args
      ar << @req.ext
      filename = ar.join(".")
      file = @site.files(@req.base).path(filename)
#      qp file.to_s
      if $smil_debug
	return c_notice(1){"filename is "+filename}
      end

      return c_simple_send(file.to_s, "application/smil")
    end

    def plg_smil(title=nil)
      @smil_num = 0 if !defined?(@smil_num)
      @smil_num += 1
      file = @req.base+"."+@smil_num.to_s+".smil"
      str = yield
      title = file if title.nil?

      gen = SmilGenerator.new
      gen.parse(str)
      smil = gen.generate_smil
      smil_str = smil.format_xml.sjistou8 # make it to utf8
      @site.files(@req.base).put(file, smil_str, true) # override

      table = gen.generate_html

      return [:div, {:class=>"smil"},
	table,
#	[:p, [:a, {:href=>@req.base+".files/"+file}, file]]]
	[:p, [:a, {:href=>file}, file]]]
    end

    def plg_video(*args)
      name = @req.base
      name = args.shift if 0 < args.length
      msg = yield
      v = Video.new(@site, name)
      v.parse(msg)
      v.generate_file
      v.to_xml
    end
  end
end

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

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

    def test_plg_video
      t_add_user
      page = @site.create_new
      page.put("{{smil
:url:rtsp://example.com/test/1.files/t.rm
,00,05.05,msg1
,12:10,12:15.15,msg2
}}")
      session("/test/1.html")
      assert_xml([:div, {:class=>"smil"},
		   [:table,
		     [:tr, [:th, "IN"], [:th, "OUT"], [:th, "MSG"]],
		     [:tr, [:td, "00:00:00"], [:td, "00:00:05.05"],
		       [:td, "msg1"]],
		     [:tr, [:td, "00:12:10"], [:td, "00:12:15.15"],
		       [:td, "msg2"]]],
		   [:p, [:a, {:href=>"1.1.smil"}, "1.1.smil"]]],
		 "//div[@class='section']")

      path = @site.files("1").path("1.1.smil")
      str = path.open {|f| f.read }
      assert(str.include?("http://www.w3.org/2001/SMIL20/Language"))

#      session("/test/1.files/1.1.smil") # get the file
      session("/test/1.1.smil") # get the file
      str = @res.body.read
      @res.body.close # important
      assert(str.include?("http://www.w3.org/2001/SMIL20/Language"))
      assert_equal("application/smil", @res["Content-Type"])
    end

    def test_media_plugin
      assert_wiki([:div, {:class=>"box"},
		    [:table, [:tr, [:td, "IN"], [:td, "OUT"], [:td, "MSG"]]],
		    [:p, [:a, {:href=>".attach/1.smil"}, "1"]]],
		  "{{video\n,k,v\n}}")
      t_add_user
      session("/test/.attach/1.smil") # get a file
      str = @res.body.read
      @res.body.close # important

      assert_equal("application/smil", @res["Content-Type"])

      # test real situation
      assert_wiki([:div, {:class=>"box"},
		    [:table,
		      [:tr, [:td, "IN"], [:td, "OUT"], [:td, "MSG"]],
		      [:tr, [:td, "00:03"], [:td, "00:13"]],
		      [:tr, [:td, "00:20"], [:td, "00:28"]]],
		    [:p, [:a, {:href=>".attach/TestSmil.smil"}, "TestSmil"]]],
		  "{{video(TestSmil)
:width:160
:height:120
:url:rtsp://stream.nhk.or.jp/news/20030914000046002.rm
,00:03,00:13,
,00:20,00:28,
}}")
      session("/test/.attach/TestSmil.smil")
      str = @res.body.read
      @res.body.close # important

      # test real situation
      assert_wiki([:div, {:class=>"box"},
		    [:table,
		      [:tr, [:td, "IN"], [:td, "OUT"], [:td, "MSG"]],
		      [:tr, [:td, "00:03"], [:td, "00:13"], [:td, "‚ "]],
		      [:tr, [:td, "00:20"], [:td, "00:28"]]],
		    [:p, [:a, {:href=>".attach/TestSmil.smil"}, "TestSmil"]]],
		  "{{video(TestSmil)
:width:160
:height:120
:url:rtsp://stream.nhk.or.jp/news/20030914000046002.rm
,00:03,00:13,‚ 
,00:20,00:28,
}}")
      session("/test/.attach/TestSmil.smil")
      str = @res.body.read
      @res.body.close # important
      assert(str.include?("http://www.w3.org/2001/SMIL20/Language"))
    end
  end
end
