# vim:ts=4:autoindent

require 'Log'

class Special
    def initialize(dd)
        @dd = dd

        @sp_set = []

        @dd.on_event( self, :type => :chatter, :message => /^ragnarok, seen /i ){ |ev, md| rag_seen(ev, md) }
        @dd.on_event( self, :type => :privmsg, :message => /^parrot /i){ |ev, md| rag_parrot(ev, md) }
        @dd.on_event( self, :type => :privmsg, :message => /^parrotquiet (\d+) /i){ |ev, md| rag_quietparrot(ev, md) }
        @dd.on_event( self, :type => :privmsg, :message => "give me sweet whos on" ){ |ev| do_sweetwhoson(ev) }
        @dd.on_event( self, :type => :privmsg, :message => "give me sweet ops" ){ |ev| do_set(ev) }
        @dd.on_event( self, :type => :privmsg, :message => "give me sweet flush" ) do |ev|
            user = ev[:user]
            File.open("ips_to_accts.yaml", "w"){ |yf| YAML.dump(@dd.ips_to_accts, yf) }
            File.open("accts.yaml", "w"){ |yf| YAML.dump(@dd.accts, yf) }
            user.privmsg "~--> Flushed @accts, @ips_to_accts\r"
            user.privmsg "~--> Thanks\r"
        end

        @dd.on_event( self, :type => :logout ){ |ev| @sp_set.delete_if{ |u, set| u == ev[:user] } }
        @dd.on_event( self, :type => :admin, :kind => :accept ) do |ev|
            msg = [ "Accept P#{ev[:port]} #{ev[:host]} " ]
            ev[:accts].each do |acct, times|
                tacct = (acct == 1000 ? "m0e" : ("%03d" % acct))
                msg << "/#{tacct} #{times}x"
            end if !ev[:accts].nil?
            msg << "/unknown" if ev[:accts].nil?
            @sp_set.each do |u, value|
                u.privmsg "~--> #{Time.now.strftime("[%I:%M]")} #{msg.join}\r"
            end
        end

        @dd.on_event(self, :type => :admin) do |ev|
            msg = nil
            msg =   case ev[:kind]
                    when :assign:   "Assign P#{ev[:port]} to #{ev[:where]}"
                    when :shut:     "Shut P#{ev[:port]}"
                    when :reject:   "Reject Full #{ev[:host]}"
                    when :console:  "Console P#{ev[:port]}"
                    end

            @sp_set.each do |u, value|
                u.privmsg "~--> #{Time.now.strftime("[%I:%M]")} #{msg}\r"
            end if !msg.nil?
        end
    end

    def rag_parrot(ev, md)
        @dd.write("#0<T1:Ragnarok) #{md.post_match}\r")
    end

    def rag_quietparrot(ev, md)
        return if md == nil
        if md[1] == "0"
            @dd.users.each do |line, u|
                next if u.nil? || u.is_remote
                u.privmsg "~#{md.post_match}\r"
            end
        else
            @dd.write("/P#{md[1]}~#{md.post_match}\r")
        end
    end

    def do_set(ev)
        user = ev[:user]
        setting = @sp_set.assoc(user)
        if setting.nil?
            @sp_set << (setting = [ user, {} ])
        end

        user.privmsg "~--> Oh Allright\r"
    end

    def do_sweetwhoson(ev)
        user = ev[:user]
        msg = [ "~--> Port List" ]
        @dd.ports.each_with_index do |pary, p|
            next if pary.nil?
            host = pary[0]
            u = pary[1]

            add = "^P#{"%02d" % p}:#{"%15s" % host} " + (u.nil? ? "in limbo" : u.llha)
            if msg.join.length + add.length > 220
                user.privmsg( msg.join + "\r" )
                msg = ["~--> Port List (continued)", add]
            else
                msg << add
            end
        end
        user.privmsg( msg.join + "^--> End\r" )
    end

    def rag_seen(ev, md)
        mdd = /^(\d\d\d)/.match(md.post_match)

        if mdd.nil?
            phrases = [ "The last time I saw XYZ, your mom was hanging them out to dry on the lawn.",
                        "I don't think anyone has ever seen XYZ.",
                        "Your XYZ aren't even big enough to be seen, douchefag.",
                        "WTF are you talking about, XYZ?",
                        "Are you STILL looking for those XYZ?" ]
            @dd.write("#0<T1:Ragnarok) #{phrases[rand(phrases.length)].gsub(/XYZ/, md.post_match)}\r")
            return
        end

        acct = mdd[1].to_i
        return if !(acct >= 0 && acct <= 899)

        isonnow = false
        @dd.users.each do |key, value|
            next if value.acct != acct
            isonnow = value
            break
        end
        if isonnow
            @dd.write("#0<T1:Ragnarok) #{isonnow.handle} is on right now, STUPID\r")
            return
        end

        if @dd.accts[acct].nil? || @dd.accts[acct][:last_seen].nil?
            @dd.write("#0<T1:Ragnarok) I have never seen #{"%03d" % acct}, sorry.\r")
            return
        end

        tsecs = (Time.now - @dd.accts[acct][:last_seen]).to_i
        hours = tsecs / 3600
        tsecs = tsecs - (hours * 3600)
        mins = tsecs / 60
        lastchatter = @dd.accts[acct][:last_chatter] if !@dd.accts[acct].nil? && !@dd.accts[acct][:last_chatter].nil?

        @dd.write("#0<T1:Ragnarok) #{"%03d" % acct} was on #{hours} hours, #{mins} minutes ago" +
            (lastchatter.nil? ? "" : ", saying \"#{lastchatter}\"") + ".\r")
    end
end