/**
 * Jdigi
 *
 * Copyright 2014, Bob Jamison
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

var Digi  = require("./digi").Digi;


function Watcher(par) {
    "use strict";

    //This regex's groups are prefix, digit, suffix
    var prefix = "([A-Z]{1,2}|[0-9][A-Z]|[A-Z][0-9])";
    var digits = "([0-9])";
    var suffix = "(F[A-Z]{3}|[A-Z]{1,3})";  //note:  Fxxx is australian
    var call = prefix + digits + suffix;
    var spot = "[^a-zA-Z0-9]de\\s+(" + call + ")[^a-zA-Z0-9]";
    
    var buf = "";
    var calls = {};
    
    function announce(call) {
        par.status(call.ts.toUTCString() + " : " + call.freq + " : " + call.call);
    }
    
    function check(call) {
        var csn = call.call;
        if (csn in calls) {
            var curr = calls[csn];
            var diff = call.ts.getTime() - curr.ts.getTime();
            if (diff > 300000) { //5 mins
                curr.ts = call.ts;
                announce(call);
            }
        } else {
            calls[csn] = call;
            announce(call);
        }
    
    }
    
    function searchBuffer(str) {
        var rgx = new RegExp(spot, "ig");
        var calls = {};
        for (var res=rgx.exec(str) ; res !== null ; res=rgx.exec(str)) {
            var call = {
                call   : res[1].toLowerCase(),
                prefix : res[2].toLowerCase(),
                digit  : res[3],
                suffix : res[4].toLowerCase(),
                freq   : par.getFrequency(),
                ts     : new Date() //timestamp
            };
            check(call);
        }
        return calls;
    }
    
    this.update = function(str) {
        buf += str;
        searchBuffer(buf);
        var len = buf.length;
        if (len > 30) {
           buf = buf.substring(20, len);
        }
    
    };
}

module.exports.Watcher = Watcher;



@@ -1206,19 +1206,18 @@
     
-par.status(
+var msg = 
 call
@@ -1253,32 +1253,106 @@
 all.
-freq + %22 : %22 + call.call
+call + %22 : %22 +%0A            call.freq + %22 : %22 + call.mode + %22/%22 + call.rate;%0A        par.status(msg
 );%0A 
@@ -1935,24 +1935,146 @@
 xec(str)) %7B%0A
+            var mode = par.getMode();%0A            var name = mode.properties.name;%0A            var rate = mode.getRate();%0A
             
@@ -2301,24 +2301,86 @@
 requency(),%0A
+                mode   : name,%0A                rate   : rate,%0A
             

add more info to watcher msg