# $Id$

namespace eval emoteicons {
    array set emoteicons {}
}

proc emoteicons::add {word image} {
    variable emoteicons
    set emoteicons($word) $image
}

proc emoteicons::get {word} {
    variable emoteicons

    if {[info exists emoteicons($word)]} {
	return $emoteicons($word)
    } else {
	return ""
    }
}


#image create photo facestartled -file [fullpath gnus-smilies/FaceStartled.gif]
#image create photo facestraight -file [fullpath gnus-smilies/FaceStraight.gif]
#image create photo faceironic   -file [fullpath gnus-smilies/FaceIronic.gif]
#image create photo facehappy    -file [fullpath jajc-bitmaps/facehappy.gif]
#image create photo facesad      -file [fullpath jajc-bitmaps/facesad.gif]
#image create photo facegrinning -file [fullpath jajc-bitmaps/facegrinning.gif]
#image create photo faceyukky    -file [fullpath jajc-bitmaps/faceyukky.gif]
#image create photo facewinking  -file [fullpath jajc-bitmaps/facewinking.gif]
#image create photo beer         -file [fullpath jajc-bitmaps/beer.gif]
#
#
#
#emoteicons::add :)     facehappy
#emoteicons::add :-)    facehappy
#emoteicons::add :(     facesad
#emoteicons::add :-(    facesad
#emoteicons::add :-O    facestartled
#emoteicons::add :-|    facestraight
#emoteicons::add :-/    faceironic
#emoteicons::add :-D    facegrinning
#emoteicons::add ";)"   facewinking
#emoteicons::add ";-)"  facewinking
#emoteicons::add :-P    faceyukky
#emoteicons::add :P     faceyukky
#emoteicons::add :beer: beer
#emoteicons::add (B)    beer


proc emoteicons::load_dir {dir} {
    set icondef_path [file join $dir icondef.xml]
    if {![file isfile $icondef_path]} {
	### TODO: some error messages
	return
    }
    set f [open $icondef_path]
    set icondef [read $f]
    close $f

    set parser [jlib::wrapper:new "#" "#" \
		    [list emoteicons::parse_icondef $dir]]
    jlib::wrapper:elementstart $parser stream:stream {} {}
    jlib::wrapper:parser $parser parse $icondef
    jlib::wrapper:parser $parser configure -final 0
}

proc emoteicons::parse_icondef {dir xmldata} {
    jlib::wrapper:splitxml $xmldata tag vars isempty chdata children

    if {$tag != "icondef"} {
	# TODO: error message
	return
    }

    foreach child $children {
	parse_item $dir $child
    }

}

proc emoteicons::parse_item {dir item} {
    jlib::wrapper:splitxml $item tag vars isempty chdata children

    switch -- $tag {
	name {}
	version {}
	description {}
	author {}
	creation {}
	icon {
	    parse_icon $dir $children
	}
    }
}

proc emoteicons::parse_icon {dir items} {
    set txts {}
    set graphic ""
    foreach item $items {
	jlib::wrapper:splitxml $item tag vars isempty chdata children
	switch -- $tag {
	    text {
		lappend txts $chdata
	    }
	    graphic {
		switch -- [jlib::wrapper:getattr $vars type] {
		    image/gif {set graphic $chdata}
		}
	    }
	    sound {}
	}
    }
    #puts "E: $graphic; $txts"
    if {$graphic != ""} {
	set img $dir/$graphic
	image create photo $img -file [file join $dir $graphic]
	foreach txt $txts {
	    emoteicons::add $txt $img
	}
    }    
}

emoteicons::load_dir [fullpath emoticons-tkabber]

