# encoding: utf-8

# Copyright (C) 2011 Clemens Fuchslocher <clemens@vakuumverpackt.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

require_relative 'ChartRenderer'
require_relative 'Files'
require_relative 'HtmlRenderer'
require_relative 'JsonRenderer'
require_relative 'Meter'
require_relative 'Results'

module VoidMeter

	class Projects
		def initialize
			@projects = []
			@results = Results.new('VoidMeter.results')
		end

		def add_project(project)
			@projects.push(project)
		end

		def meter
			@projects.each do |project|
				meter = VoidMeter::Meter.new
				meter.meter!(project, @results)
			end
			@results.save
		end

		def render
			chart = VoidMeter::ChartRenderer.new(@projects, @results, 'results')
			chart.render

			html = VoidMeter::HtmlRenderer.new(@projects, @results, 'results')
			html.render

			json = VoidMeter::JsonRenderer.new(@results, 'results')
			json.render
		end

		def list
			@projects.each do |project|
				puts "#{project}\n\n"
				project.releases.each do |release|
					puts "#{release}\n\n"
				end
				puts
			end
		end
	end

	class Project
		attr_reader :name, :url, :files, :encoding, :releases

		def initialize(arguments)
			@name = arguments[:name] || ''
			@url = arguments[:url] || ''
			@files = arguments[:files] || Files.new
			@encoding = arguments[:encoding] || 'ascii'
			@releases = []
		end

		def add_release(arguments)
			files = arguments[:files] || @files
			encoding = arguments[:encoding] || @encoding
			@releases.push(Release.new(arguments[:version], arguments[:path], arguments[:url], files, encoding))
		end

		def to_s
			"#{name}\n#{url}\n#{files.to_s}\n#{encoding}"
		end
	end

	class Release
		attr_accessor :chart, :chart_trailing
		attr_reader :version, :path, :url, :files, :encoding

		def initialize(version, path, url, files, encoding)
			@version = version
			@path = path
			@url = url
			@files = files
			@encoding = encoding
		end

		def to_s
			"\t#{version}\n\t\t#{path}\n\t\t#{url}\n\t\t#{files.to_s}\n\t\t#{encoding}"
		end
	end

end
