require 'rexml/document'
$hash = {}
$objnames = {}
$filenames = []
$onames = []

$elemfiles = {}
$scassetfiles = []

$db_hidden_objects = []
$db_scenes = []

class HiddenObject
	attr_accessor :name, :file
	def initialize(hname, fname)
		@name = hname
		@file = fname
	end
	
	def to_s()
		return "HO: name=#{@name.to_s}, file=#{@file.to_s}"
	end
end

class Scene
	attr_accessor :name, :layers
	def initialize(sc_name)
		@name = sc_name
		@layers = []
	end
	
	def to_s()
		ret = "Scene: name=#{@name.to_s}\n Layers=\n"
		for layer in @layers do
			ret += "   #{layer.to_s}\n"
		end
		return ret
	end
end

class Layer
	attr_accessor :name, :file, :assoc_ho
	def initialize(lname, fname, hassoc)
		@name = lname
		@file = fname
		@assoc_ho = hassoc
	end
	
	def to_s()
		return "Layer: name=#{@name.to_s}\tfile=#{@file.to_s}\tho=#{@assoc_ho.to_s}"
	end
end

def readHObj()
	doc = REXML::Document.new(File.open("hiddenobjects/objects.xml"))
	doc.elements.each('hiddenobjects/hiddenobject'){ |ho|
		id = ho.attributes['file'].to_s
		hoid = ho.attributes['name'].to_s
		$db_hidden_objects << HiddenObject.new(hoid, id)
		if $objnames[hoid] == nil then
			$objnames[hoid] = 1
		else
			$objnames[hoid] += 1
		end
		if $onames.index(hoid) == nil then
			$onames << hoid
		end
		if $hash[id] == nil then
			$hash[id] = 1
		else
			$hash[id] += 1
		end
	}
end

def readSceneElements()
	setdoc = REXML::Document.new(File.open("scenes/Levels.set"))
	setdoc.elements.each('sceneset/scene'){|sc|
		scname = sc.attributes['name'].to_s
		
		curScene = Scene.new(scname)
		
		scFileName = "scenes/" + scname + ".scene"
		scdoc = REXML::Document.new(File.open(scFileName))
		scdoc.elements.each('scene/elements/element'){ |scelem|
			fdir = scname+"/"+scelem.attributes['file'].to_s
			scelName = scelem.attributes['name'].to_s
			scelFile = scelem.attributes['file'].to_s
			scelHO = scelem.attributes['ho'].to_s
			scel = Layer.new(scelName, scelFile, scelHO)
			curScene.layers << scel
			if $elemfiles[fdir] == nil then
				$elemfiles[fdir] = 1
			else
				$elemfiles[fdir] += 1
			end
		}
		
		$db_scenes << curScene
		Dir.foreach("./scenes/"+scname+"/"){|file|
			file = file.to_s
			if(file.match(/\.png$/).to_s == ".png" or file.match(/\.jpg$/).to_s == ".jpg") then
				$scassetfiles << (scname+"/"+file)
			end
		}
	}
end

Dir.foreach("./hiddenobjects/"){|file|
	#puts "HAHA"+file.to_s
	file = file.to_s
	if(file.match(/\.png$/).to_s == ".png") then
		file = file[0,file.length()-4]
		$filenames << file
	end
}

def writeOutput
	out = File.open("stats.txt","w")
	out2 = File.open("dup.txt","w")
	out.puts("(NOTE: each entry is removed off the .png extension.")
	out.puts("So for example, the line says: sc1_obj_1, it means the image hiddenobjects/sc1_obj_1.png is not used.")
	out.puts("----------------------------------------")
	$filenames.each{|file|
		if $hash[file] == nil then
			out.puts(file.to_s)
		end
	}
	$onames.each{|oname|
		if $objnames[oname] > 1 then
			out2.puts(oname.to_s+" : "+$objnames[oname].to_s)
		end
	}
	out.close()
	out2.close()
	
	out3 = File.open("scenes.txt","w")
	$scassetfiles.each{|file|
		if $elemfiles[file] == nil then
			out3.puts(file)
		end
	}
	out3.close()
end

def printHO
	for ho in $db_hidden_objects do
		puts ho.to_s
	end
end

def printScenes
	for scene in $db_scenes do
		puts scene.to_s
	end
end

readHObj
readSceneElements

writeOutput
puts "Please check stats.txt for unused HO"
puts "Please check dup.txt for duplicates in HO."
puts "Please check scenes.txt for unused scene assets."
print 'press the enter key to continue...'
i = gets.to_s

assocs = {}
$db_scenes.each{|scene|
	scene.layers.each{|layer|
		if(assocs[layer.assoc_ho] == nil) then
			assocs[layer.assoc_ho] = 1
		else
			assocs[layer.assoc_ho] += 1
		end
	}
}
toPrint = File.open("floating.txt","w")
for ho in $db_hidden_objects do
	if assocs[ho.name] == nil then
		toPrint.puts(ho.name)
	end
end
toPrint.close()