// FIXME: Need to figure out what to do about MAILER-DAEMON messages

var ClassifyCross = Class.create({
    initialize: function (constraints, input) {
        console.group ('initialize()');
        console.log ('constraints are %o', constraints);
        this.constraints = $H(constraints);
        var rows = $A(input);
        console.log ('rows are %o', rows);
        var data = new Hash;
        console.log ('data is %o', data);
        console.time ('Summing rows in hash');
        rows.each (function (r) {
                       var id = r.i + '-' + r.s;
                       // console.log ('id is %o', id);
                       var old = data.get (id) || 0;
                       // nconsole.log ('Old value is %o', old);
                       var sum = old + new Number (r.uncertain);
                       data.set (id, sum);
                   });
        console.timeEnd ('Summing rows in hash');
        console.log ('Resulting hash is %o', data);
        console.log ('Getting reference to pages');
        this.container = $('pages');
        console.log ('Retrieving crossdiv');
        var crossdiv = this.container.retrieve ('crossdiv');
        console.log ('Evaluating crossdiv %o', crossdiv);
        var html = crossdiv.evaluate (this.constraints);
        console.time ('Inserting crossdiv');
        this.container.insert (html);
        console.timeEnd ('Inserting crossdiv');
        console.log ('Get a reference to the div');
        var page = $(this.constraints.get ('id'));
        console.log ('Adding observers to %o', page);
        page.observe ('click', this.click.bind (this));
        page.observe ('ClassifyCross:search', this.search.bind (this));
        page.observe ('ClassifyCross:count', this.count.bind (this));
        console.log ('Retrieving crossrow');
        var crossrow = this.container.retrieve ('crossrow');
        console.log ('Rendering rows using %o', crossrow);
        console.time ('Sorting and rendering data');
        var html = data.keys ().sort (function (a, b) {
                                          return data.get (b) - data.get (a);
                                      }).map (function (key) {
                                                  var bits = key.split ('-');
                                                  return crossrow.evaluate ({ip: bits[0], sender: bits[1], value: data.get (key)});
                                              }).join ('');
        console.timeEnd ('Sorting and rendering data');
        console.time ('Inserting table in page');
        page.down ('tbody').insert (html);
        console.timeEnd ('Inserting table in page');
        console.log ('Adding tab');
        console.log ('Constructing our title');
        page.fire ('Classify:add', this.constraints.get ('title'));
        console.log ('Done adding tab');
        console.groupEnd ();
    },

    click: function (event) {
        console.group ('click()');
        var a = event.findElement ('a');
        if (a) {
            console.log ('Got element %o', a);
            if (a.match ('a.count')) {
                console.log ('Got count message');
                a.fire ('ClassifyCross:count');
            } else if (a.match ('a.search')) {
                console.log ('Got search message');
                a.fire ('ClassifyCross:search');
            }
            console.log ('Stopping propagation');
            event.stop();
        }
        console.groupEnd ();
    },

    count: function (event) {
        console.group ('count()');
        this.container.fire ('Classify:count', {confidence: this.constraints.get ('confidence'),
                                                link: event.findElement ('a'),
                                                type: event.findElement ('div').identify (),
                                                value: event.findElement ('td').down ('a').innerHTML});
        console.groupEnd ();
    },

    failure: function () {
        alert ("Failed to retrieve list");
    },

    search: function (event) {
        console.group ('search()');
        var row = event.findElement ('tr');
        var values = row.select ('a.search').pluck ('innerHTML');
        var ip = values[1].replace (/\/\d+$/, '');
        var sender = values[0];
        var rawid = 'ip-' + ip + '-sender-' + sender;
        var id = rawid.replace (/[@\.\/]/g, '-');
        var title = 'ip-' + ip + '-sender-' + sender;
        this.container.fire ('Classify:search', {confidence: this.constraints.get ('confidence'), id: id, ip: ip, sender: 'sender@' + sender, title: title});
        console.groupEnd ();
    }
});

