/*
 * Copyright 2006-2013 Andrew Williams.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rectang.rcbot.module;

import com.rectang.rcbot.*;

import java.util.*;

import org.headsupdev.irc.IRCConnection;
import org.headsupdev.irc.IRCUser;
import org.headsupdev.irc.IRCServiceManager;

/* watch what the bot says too? how?
 */

/**
 * A module for watching who is around
 *
 */
public class Seen extends RCBotCommand {

  public Seen(RCBot bot, IRCServiceManager manager) {
    super(bot, manager);
  }

  public String getId() {
    return "seen";
  }

  public String getTitle() {
    return "Watch users come and go and report when folk were present";
  }

  public Collection getCommands() {
    return NO_COMMANDS;
  }

  public void onSubCommand(String command, String channel, IRCUser user,
                           String message, IRCConnection conn) {
    
    String match = getStore(bot).getString(message);
    if (match == null || match.equals("")) {
      conn.sendMessage(channel, "I cannot remember seeing " + message);
    } else {
      String parts[] = match.split(":");

      long time = 0;
      try {
        time = Long.parseLong(parts[0]);
      } catch (NumberFormatException e) {
        /* report below that we cannot remember */
      }
      if (parts.length < 3 || time == 0) {
        conn.sendMessage(channel, "I have seen " + message + ", but I cannot remember when");
      } else {
        conn.sendMessage(channel, "I last saw " + message + " on " + parts[1] + " " +
          StringUtils.formatTimeOffset((System.currentTimeMillis() - time)
              / 1000) + " ago " + 
          match.substring(parts[0].length() + 1 + parts[1].length() + 1));
      }
    }
  }

  public String getHelp(String channel) {
    return "Seen is a module for finding out when people were last online.\n" +
        "  To see when someone was last around use \"seen username\".";
  }

  public void onMissingCommand( String command, String channel, IRCUser user, IRCConnection conn ) {
    /* cannot be called, we accept NO_COMMANDS */
  }
}

@@ -1388,13 +1388,8 @@
 ) %7B%0A
-    %0A
     
@@ -1433,24 +1433,24 @@
 g(message);%0A
+
     if (matc
@@ -1798,28 +1798,8 @@
 if (
-parts.length %3C 3 %7C%7C 
 time
@@ -1912,24 +1912,168 @@
    %7D else %7B%0A
+          String timeOffset = StringUtils.formatTimeOffset((System.currentTimeMillis() - time) / 1000);%0A          if (parts.length %3C 3) %7B%0A      
         conn
@@ -2101,16 +2101,17 @@
  %22I 
-last saw
+have seen
  %22 +
@@ -2150,193 +2150,507 @@
  %22 +
-%0A          StringUtils.formatTimeOffset((System.currentTimeMillis() - time)%0A              / 1000) + %22 ago %22 + %0A          match.substring(parts%5B0%5D.length() + 1 + parts%5B1%5D.length() + 1));
+ timeOffset + %22 ago%22);%0A          %7D else %7B%0A              String action = match.substring(parts%5B0%5D.length() + 1 + parts%5B1%5D.length() + 1);%0A              if (action.equals(%22joining%22)) %7B%0A                conn.sendMessage(channel, %22I last saw %22 + message + %22 joining %22 + parts%5B1%5D + %22 %22 + timeOffset + %22 ago%22);%0A              %7D else %7B%0A                conn.sendMessage(channel, %22I last saw %22 + message + %22 on %22 + parts%5B1%5D + %22 %22 + timeOffset + %22 ago %22 +%0A                    action);%0A              %7D%0A          %7D
 %0A   

tidy up responses to various seen situations