var schema = require('./schema.js');

var exports = module.exports = {};

exports.findSimilar = function(start_song, radius) {

};

exports.getDashboard = function() {

};

// Find songs that are similar to a given song title
exports.similarSongs = function(songTitle) {
	return schema.models.song.find({
		title: songTitle
	}).exec()
	.then(function (songs) {
		var id = songs[0].song_id;
		return schema.models.similar_songs.find({
			song_id: id
		}, {
			_id: 0,
			song_id: 0
		}).exec()
	})
	.then(function (result) {
		var similar = result[0].similar_song_id;
		var song_ids = [];
		for (var i = 0; i < similar.length; i++) {
			song_ids.push(similar[i][0]);
		}
		return schema.models.song.find({
			song_id: {
				$in: song_ids
			}
		}).exec(function (err, songs) {
			var artistCounts = {};
			for (var i = 0; i < songs.length; i++) {
				var artist = songs[i].artist;
				if (songs[i].artist in artistCounts) {
					artistCounts[artist] = artistCounts[artist] + 1;
				} else {
					artistCounts[artist] = 1;
				}
			}
			return {
				artists: artistCounts,
				songs: songs
			};
		});
	})
};

// Find an artist's songs
exports.artistSongs = function(artistName) {
	return schema.models.song.find({
		artist: artistName
	}, {
		_id: 0,
		song_id: 0
	}).exec();
};

// Find an artist's common genres
exports.artistGenres = function(artistName) {
	return schema.models.song.find({
		artist: artistName
	}, {
		_id: 0,
		title: 0
	}).exec().then(function (result) {
		var ids = result.map(function(item) {
			return item.song_id;
		});
		console.log(ids);
		return schema.models.genre.find({
			song_id: {
				$in: ids
			}
		}).exec();
	})
};

// Find what a song has been tagged as
exports.songTags = function(songTitle) {
	return schema.models.song.find({
		title: songTitle
	}).exec().then(function (err, result) {
		var id = result[0].song_id;
		return schema.models.tag.find({
			song_id: id
		}, {
			_id: 0,
			song_id: 0
		}).exec();
	});
};

// Find the songs that have been played the most
exports.mostPopularSongs = function(number) {
	return schema.models.user_data.aggregate([
        {
            $group: {
                _id: "$song_id",
                count: { $sum: 1 }
            }
        },
		{
			$sort: {count: -1}
		},
		{
			$limit: number
		},
		{
			$lookup: {
				from: "songs",
				localField: "_id",
				foreignField: "song_id",
				as: "song"
			}
		},
		{
			$project: {
				_id: 0,
				song_id: 1,
				'song.name': 1,
				count: 1
			}
		}
    ]).exec(function (err, users) {
		if (err) throw err;
		return users;
    });
};

// Find songs that have been covered many times
exports.commonlyCoveredSongs = function() {
	return schema.models.covers.aggregate([
        {
            $group: {
                _id: "$song_id",
                count: { $sum: 1 }
            }
        },
		{
			$sort: {count: -1}
		}
    ]).exec(function (err, users) {
		return users;
    });
};

// Find songs that have the most words in common
exports.findCommon = function(toCount) {
    var songWords = schema.models.song_word_count.aggregate([
        {
            $group: {
                _id: "$song_id",
                words: {
                    $addToSet: "$word"
                }
            }
        }
    ]);
    // songWords maps song id to the most popular words contained in that song
    var mostCommon = [];
    var songPairs = songWords.forEach(function(song1) {
    	songWords.forEach(function(song2) {
			var words1 = song1.words;
			var words2 = song2.words;
			var intersection = words1.filter(function(word) {
				return words2.indexOf(word) > -1;
			});
			mostCommon.push({
				song1: song1._id,
				song2: song2._id,
				commonWords: intersection.length
			});
    	});
    });
    return songPairs.sort(function(a, b) {
    	return a.count - b.count;
    })
};

// Find songs according to a filter
exports.filterSongs = function(filter) {
    return schema.models.song.find(filter, {
        _id: 0,
        title: 1
    }, function(err, result) {
        if (err) throw err;
        return result
    });
};

// Autocomplete songs by name
exports.autocompleteSongs = function(song_name) {
    return schema.models.song.find({
        title:  new RegExp(song_name, 'i')
    }, {
        _id: 0,
        title: 1
    }).limit(10).exec(function(err, result) {
		return result;
	});
};

exports.genres = function() {
    return schema.models.genre.find().lean().exec(function (err, users) {
        return users;
    });
};

@@ -4257,32 +4257,45 @@
 ,%0A        title:
+ 1,%0A%09%09artist:
  1%0A    %7D).limit(
@@ -4342,16 +4342,93 @@
 n result
+.map(function (item) %7B%0A%09%09%09item.title = item.title + %22 - %22 + item.artist;%0A%09%09%7D)
 ;%0A%09%7D);%0A%7D

add artist to autocomplete