define(function(require) {
	var ko = require('knockout'),
		apiPullMod = require('apiPullMod'),
		d3 = require('d3'),
		dataObject = function (lane, creepScore) {
			this.name = lane;
			this.value = creepScore;
		};

		
	return {
		name: ko.observable('RyeBrush'),
		rankedGames: ko.observableArray(),
		rankedChart: function() {
			var that = this;
			var chart = d3.select('.chart');
			if (!that.rankedGames){
				return;
			} else {
				that.rankedGames.removeAll();
				apiPullMod.apiCaller('v1.4/summoner/by-name', that.name(), 'na')
				.then(function(response){
					return apiPullMod.apiCaller('v2.2/matchhistory',response[that.name().toLowerCase()].id, 'na', 'rankedQueues=RANKED_SOLO_5x5&');
				}).then(function(response){
					for(var aMatch in response.matches){
						that.rankedGames.push(new dataObject(response.matches[aMatch].participants[0].timeline.lane,response.matches[aMatch].participants[0].stats.minionsKilled));
					};
					return;
				}).then(function(){
					var margin = {top: 20, right: 30, bottom: 30, left: 40},
						width = 960 - margin.left - margin.right,
						height = 500 - margin.top - margin.bottom;
						
					var barWidth = width / that.rankedGames().length;

					var y = d3.scale.linear()
						.domain([0, d3.max(that.rankedGames(), function(d) {return d.value;})])
						.range([height, 0]);

					chart.attr("width", width + margin.left + margin.right)
						 .attr("height", height + margin.top + margin.bottom)
						.append("g")
						 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

					var bar = chart.selectAll('g')
						.data(that.rankedGames())
					  .enter().append('g')
						.attr("transform", function(d,i){return "translate(" + i * barWidth + ",0)";});
					
					var yAxis = d3.svg.axis()
						.scale(y)
						.orient("left");
						
					chart.append("g")
						.attr("class", "y axis")
						.call(yAxis);
						
					bar.append('rect')
						.attr('y', function(d) {return y(d.value);})
						.attr('height', function(d) {return height - y(d.value);})
						.attr('width', barWidth - 1);
						
					bar.append('text')
						.attr('x', barWidth / 2)
						.attr('y', function(d) {return y(d.value) + 3; })
						.attr('dy', '.75em')
						.text(function(d) {return d.name;});
				});
			}	
		}
	};
})