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

module Qwik
  class Media
    def initialize(site, name)
      @site = site
      @table = []
      @msg = nil
      @name = name
      @param = {}
      @param[:url] = nil
      @param[:width]  = 320 # default
      @param[:height] = 240
      @hash_delim = [":"]
      @table_delim = ["|", ","]
    end

    def parse(msg)
      @msg = msg
      lines = msg.split(/\n/)
      lines.each {|line|
	firstchar = line[0, 1]
	if @table_delim.include?(firstchar)
	  ar = line.split(firstchar)
	  ar.shift # null
	  cbegin = cend = msg = nil
	  cbegin = VideoTime.new(ar.shift) if ! ar.empty?
	  cend   = VideoTime.new(ar.shift) if ! ar.empty?
	  msg = ar.shift if ! ar.empty?
	  if @param[:url] && cbegin && cend
	    @table << {:url=>@param[:url],
	      :cbegin=>cbegin, :cend=>cend, :msg=>msg}
	  end
	  next
	end
	if @hash_delim.include?(firstchar)
	  ar = line.split(firstchar, 3)
	  ar.shift # null
	  name = ar.shift if ! ar.empty?
	  @param[name.intern] = ar.shift if ! ar.empty?
	end
      }
    end

    def generate_file
      smil = make_smil.format_xml
      smil = smil.sjistou8
      @site.attach.put(@name+".smil", smil, true)
    end

    def make_smil
      g = Generator.new
      time = 0
      par = []
      @table.each {|tab|
	dur = (tab[:cend] - tab[:cbegin]).to_i
	
	ar = [{"region"=>"v"}, {"begin"=>time.to_s+"s"}, {"src"=>tab[:url]}]
	ar << {"clip-begin"=>tab[:cbegin]} if tab[:cbegin]
	ar << {"clip-end"=>tab[:cend]} if tab[:cend]
	par << g.video(ar)
	time += dur
      }

      g.smil(:xmlns=>"http://www.w3.org/2001/SMIL20/Language",
	     "xmlns:rn"=>"http://features.real.com/2001/SMIL20/Extensions"){[
	  g.head{
	    g.layout{[
		g.make("root-layout",
		       {"width"=>@param[:width]}, {"height"=>@param[:height]}),
		g.region({"id"=>"v"}, {"fit"=>"meet"}),
	      ]}
	  },
	  g.body{
	    g.par{par}
	  }
	]}
    end

    def to_xml
      table = []
      table << [:tr,
	[:td, "IN"],
	[:td, "OUT"],
	[:td, "MSG"]
      ]
      @table.each {|tab|
	tr = []
	tr << [:td, tab[:cbegin].to_s]
	tr << [:td, tab[:cend].to_s]
	tr << [:td, tab[:msg].to_s] if tab[:msg]
	table << [:tr, *tr]
      }
      [:div, {:class=>"box"},
	[:table, *table],
	[:p, [:a, {:href=>".attach/"+@name+".smil"}, @name]]]
    end
  end

  class Video < Media
  end

  class VideoTime
    def initialize(arg=0)
      @sec = @min = @hour = 0
      @str = nil
      if arg.kind_of? String
	@str = arg
	ar = @str.split(/:/)
	@sec = ar.pop
	@min = ar.pop
	@hour = ar.empty? ? 0 : ar.pop
      end
      @t = Time.gm(2000, 1, 1, @hour, @min, @sec, 0) # start 2000/01/01 00:00
    end
    attr_reader :t

    def -(v) @t-v.t; end

    def +(v)
      return @t+v if v.kind_of? Numeric
      @t+v.t
    end

    def to_s
      return @str if @str
    end
    alias inspect to_s
  end
end

if $0 == __FILE__
  require "qwik/test-common"
  $debug = true
end

if $debug
  class TC_Media < Test::Unit::TestCase
    def test_all
    end
  end
end
