# vim:ts=4:autoindent

require 'Log'

class Notify
    def initialize(dd)
        @dd = dd

        @user_settings = []

        @dd.on_event( self, :type => :privmsg, :message => /^(un)?set /i ){ |ev, md| do_set(ev, md) }
        @dd.on_event( self, :type => :logout ) do |ev|
            user = ev[:user]
            @user_settings.delete_if{ |u, set| u == user }
        end
        @dd.on_event(self, :type => :admin, :kind => :assign) do |ev|
            @user_settings.each do |u, value|
                u.privmsg "~--> #{Time.now.strftime("[%I:%M]")} Login on #{ev[:where]}\r" if value[:logins]
            end
        end
    end

    def do_set(ev, md)
        user = ev[:user]

        set = (md[1] == "un" ? false : true)

        setting = @user_settings.assoc(user)
        if setting.nil?
            @user_settings << (setting = [ user, {} ])
        end

        case md.post_match.downcase
        when "help"
            msg = "--> SET HELP^[un]set logins - show [hide] logins across stations\r"
        when "logins"
            setting[1][:logins] = set
            msg = "--> Logins Across Stations: #{set ? "On" : "Off"}\r"
        else
            msg = "--> Illegal command, try 'set help'\r"
        end
        user.privmsg "~#{msg}"
    end
end