/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You 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.
 */

var clutch = require('extern/clutch');
var templates =  require('./templates');

function authors_list(req, res) {
  var authors = [
    {name: 'pquerna', projects: [{name: "cast"}, {name: "blah"}, {name: "foo"}]},
    {name: 'tomaz', projects: [{name: "ck"}, {name: "moo"}, {name: "cow"}]},
    {name: 'russel_h', projects: [{name: "lovely"}, {name: "cats"}, {name: "eat"}, {name: "brains"}]}
    ];
  authors.sort(function(a_, b_) {
    var a = a_.name.toLowerCase();
    var b = b_.name.toLowerCase();
     if (a < b) {
       return -1;
     }
     if (a > b) {
      return 1;
    }
    return 0;
  });
  templates.render('authors.html', {authors: authors}, req, res);
}

exports.urls = clutch.route([
  ['GET /$', authors_list]
]);


/*
 * Licensed to Cloudkick, Inc ('Cloudkick') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Cloudkick licenses this file to You 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.
 */

var clutch = require('extern/clutch');
var templates =  require('./templates');
var db = require('util/db');

function authors_list(req, res) {
  db.get_packages_result_array('author', 'asc', function(err, results) {
    if (err) {
      res.writeHead(500, {'Content-Type': 'text/plain'});
      res.end('Err: '+ err);
      return;
    }

    var authors = {};

    results.forEach(function(elem) {
      elem.maintainers.forEach(function(author) {
        if (authors[author.email] === undefined) {
          authors[author.email] = {name: author.name, email: author.email, projects: []}
        }
        authors[author.email].projects.unshift(elem);
      })
    });

    var authors_arr = [];
    for (var email in authors) {
      authors_arr.unshift(authors[email]);
    }
    
    authors_arr.sort(function(a_, b_) {
      var a = a_.name.toLowerCase();
      var b = b_.name.toLowerCase();
       if (a < b) {
         return -1;
       }
       if (a > b) {
        return 1;
      }
      return 0;
    });

    templates.render('authors.html', {authors: authors_arr}, req, res);
  });
}

exports.urls = clutch.route([
  ['GET /$', authors_list]
]);


make authors page powered by mongo