# 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 'erb'

require_relative 'Renderer'

module VoidMeter

	class HtmlRenderer < Renderer
		def initialize(projects, results, path)
			@projects = projects
			@results = results
			@path = path
			mkdir(path)
		end

		def render
			template = File.new('lib/VoidMeter.html').read
			renderer = ERB.new(template, nil, '>')

			File.open("#{@path}/VoidMeter.html", 'w') do |file|
				@projects.each do |project|
					puts "VoidMeter::HtmlRenderer#render: project.name == #{project.name}"
					file.puts(renderer.result(ModelResults.new(project, @results).get_binding))
				end
			end

			template = File.new('lib/VoidMeterProjectPanel.html').read
			renderer = ERB.new(template, nil, '>')

			File.open("#{@path}/VoidMeterProjectPanel.html", 'w') do |file|
				file.puts(renderer.result(ModelProjects.new(@projects).get_binding))
			end
		end

		class ModelResults
			attr_accessor :project, :results

			include ERB::Util

			def initialize(project, results)
				@project = project
				@results = results
			end

			def get_binding
				binding
			end

			def percent(total, number)
				((100.0 / total) * number).round(2)
			end
		end

		class ModelProjects
			attr_accessor :projects

			include ERB::Util

			def initialize(projects)
				@projects = projects
			end

			def get_binding
				binding
			end
		end
	end

end
