# $Id$

namespace eval presence {}

proc presence::recv {from type x args} {
    variable presence

    

}





###############################################################################
###############################################################################

proc client:presence {from type x args} {
    global presence w

    debugmsg presence "PRESENCE: $from; $type; $x; $args"

    set from [tolower_node_and_domain $from]

    switch -- $type {
	unavailable {
	    array unset presence *,$from
	    #roster:changeicon $w.roster [user_from_jid $from] unavailable
	    #roster:set_item_presence [user_from_jid $from]
	    roster::on_change_jid_presence $from
	    debugmsg presence "[user_from_jid $from] unavailable"
	    chat::change_presence $from unavailable
	}
	subscribe {eval [list message::show_subscribe_dlg $from $x] $args}
	subscribed {}
	unsubscribe {}
	unsubscribed {tk_dialog .unsubscrb "Unsubscribed from $from" \
			  "We unsubscribed from $from" "" 0 OK}
	probe {}
	error {}
	default {
	    set presence(type,$from)     available
	    set presence(status,$from)   ""
	    set presence(priority,$from) ""
	    set presence(meta,$from)     ""
	    set presence(icon,$from)     ""
	    set presence(show,$from)     available
	    set presence(loc,$from)      ""
	    set presence(x,$from)        $x
	    
	    foreach {attr val} $args {
		switch -- $attr {
		    -status   {set presence(status,$from)   $val}
		    -priority {set presence(priority,$from) $val}
		    -meta     {set presence(meta,$from)     $val}
		    -icon     {set presence(icon,$from)     $val}
		    -show     {set presence(show,$from)     $val}
		    -loc      {set presence(loc,$from)      $val}
		}
	    }
	    
	    set presence(show,$from) [normalize_show $presence(show,$from)]
	    set show $presence(show,$from)

	    #roster:changeicon $w.roster [user_from_jid $from] $show
	    #roster:changeicon $w.roster \
	    #    [user_from_jid $from] [get_jid_icon [user_from_jid $from]]
	    #roster:set_item_presence [user_from_jid $from]
	    # FIX                           vvvvvvvvvvvvvvvvvvvvv
	    #roster::on_change_jid_presence [user_from_jid $from]
	    roster::on_change_jid_presence $from
	    chat::change_presence $from $show
	}
    }
    
    
    #debugmsg presence [array get presence]
}




proc get_jid_of_user {user} {
    global presence

    # FIX THIS ALL
    set idxs [array names presence type,${user}*]

    if {[llength $idxs]} {
	set idx [lindex $idxs 0]
	
	return [crange $idx 5 end]
    } else {
	return $user
    }
}

proc get_jid_status {jid} {
    global presence

    # FIX THIS ALL
    set idxs [array names presence show,${jid}]

    if {[llength $idxs]} {
	set idx [lindex $idxs 0]
	return $presence($idx)
    } else {
	return unavailable
    }
}

proc get_user_status {user} {
    global presence

    # FIX THIS ALL
    set idxs [array names presence show,${user}*]

    if {[llength $idxs]} {
	set idx [lindex $idxs 0]
	return $presence($idx)
    } else {
	return unavailable
    }
}

proc get_user_status_desc {user} {
    global presence

    # FIX THIS ALL
    set idxs [array names presence status,${user}*]

    if {[llength $idxs]} {
	set idx [lindex $idxs 0]
	return $presence($idx)
    } else {
	return ""
    }
}


# FIX
set userstatus available
set curuserstatus available

set userstatusdesc "Online"

set statusdesc(available) Online
set statusdesc(chat) "Free for chat"
set statusdesc(away) Away
set statusdesc(xa) "Extended Away"
set statusdesc(dnd) "Do not disturb"
set statusdesc(invisible) Invisible
set statusdesc(unavailable) Offline


trace variable userstatus w change_our_presence

proc change_our_presence {name1 name2 op} {
    global userstatus curuserstatus statusdesc userstatusdesc chat::opened

    if {![cequal $userstatus $curuserstatus]} {
	set userstatusdesc $statusdesc($userstatus)
	
	switch -- $userstatus {
	    available {set command jlib::send_presence}
	    invisible {set command "jlib::send_presence -type $userstatus"}
	    default {
		set command "jlib::send_presence -show $userstatus"
	    }
	}
	debugmsg presence $command
	eval $command
	if {$userstatus != "invisible"} {
	    foreach group [array names chat::opened] {
		eval $command -to $group
	    }
	} else {
	    #FIX
	    foreach group [array names chat::opened] {
		jlib::send_presence -to $group/[get_our_groupchat_nick $group]
	    }
	}


	set curuserstatus $userstatus
    }
}


proc normalize_show {show} {
    set res $show
    
    switch -- $show {
	away        {}
	chat        {}
    	dnd         {}
	xa          {}
	unavailable {}
	stalker     {}
	default     {set res available}
    }
    return $res
}
