Factions = {
	entries = {},
	dummyFaction = {
		Name = "Unknown",
		DisplayName = "Unknown Faction",
		Acronym = "UNKNOWN",
		Logo = "unknown.png"
	}
}

function Factions:parseFactions(data)

	parse.readFileText(data, "data/tables")

	parse.requiredString("#Factions")

	while parse.optionalString("$Name:") do
		local entry = {}

		entry.Name = parse.getString()

		if parse.optionalString("$Display Name:") then
			entry.DisplayName = parse.getString()
		else
			entry.DisplayName = entry.Name
		end

		parse.requiredString("$Acronym:")
		entry.Acronym = parse.getString()

		if parse.optionalString("$Description:") then
			entry.Description = parse.getString()
		else
			entry.Description = ""
		end

		if parse.optionalString("$Use Intel Description:") then
			local bool = parse.getBoolean()
			if bool == true then
				entry.Description = tb.IntelEntries[entry.Name].Description
			end
		end

		--Warn if Acronym already exists for another faction
		for _, v in pairs(Factions.entries) do
			if v.Acronym == entry.Acronym then
				ba.error("Faction with Acronym '" .. entry.Acronym .. "' already exists. This needs to be fixed!")
			end
		end

		parse.requiredString("$Logo Image:")
		entry.Logo = parse.getString()

		if not cf.fileExists(entry.Logo, '', true) then
			ba.warning("Could not find faction logo file '" .. entry.Logo .. "'!")
		end

		entry.Species = {}
		while parse.optionalString("$Species:") do
			table.insert(entry.Species, parse.getString())
		end

		table.insert(Factions.entries, entry)
	end

	parse.requiredString("#End")

	parse.stop()

end

function Factions:getManufacturerFromCustomData(entry, ship)
	local m_string = ""

	if entry:hasCustomData() then
		m_string = entry.CustomData["manufacturer"]
	end

	if ship then
		if m_string == "" or m_string == nil then
			m_string = entry.ManufacturerString
		end
	end

	if m_string == "" or m_string == nil then
		local md = require("debugFunctions")
		md.print("FACTION HANDLER could not find manufacturer for '" .. entry.Name .."'. Using UNKNOWN instead!")
		m_string = "Unknown"
	end

	return m_string
end

function Factions:getFactionFromCustomData(entry, ship)
	local m_string = nil

	if entry:hasCustomData() then
		m_string = entry.CustomData["faction"]
	end

	if ship and (m_string == "" or m_string == nil) then
		local species = entry.Species.Name
		for _, v in pairs(Factions.entries) do
			for _, s in pairs(v.Species) do
				if species == s then
					return v.Acronym
				end
			end
		end
	end

	if m_string == "" or m_string == nil then
		local md = require("debugFunctions")
		md.print("FACTION HANDLER could not find faction for '" .. entry.Name .."'. Using UNKNOWN instead!")
		m_string = "Unknown"
	end

	return m_string
end


function Factions:checkFactionExists(name)

	for _, v in pairs(Factions.entries) do
		if v.Name == name then
			return true
		end
	end

	return false

end

function Factions:getFactionByName(name)

	for _, v in pairs(Factions.entries) do
		if v.Name == name then
			return v
		end
	end

	ba.warning("Could not find faction with name '" .. name .. "'!")

	return self.dummyFaction

end

function Factions:getFactionByAcronym(acronym)

	for _, v in pairs(Factions.entries) do
		if v.Acronym == acronym then
			return v
		end
	end

	ba.warning("Could not find faction with acronym '" .. acronym .. "'!")

	return self.dummyFaction

end

function Factions:getFactionAcryonym(name)

	for _, v in pairs(Factions.entries) do
		if v.Name == name then
			return v.Acronym
		end
	end

	ba.warning("Could not find faction with name '" .. name .. "'!")

	return self.dummyFaction.Acronym

end

function Factions:createLuaEnum()

	local enum = mn.LuaEnums["Factions"]

	for _, v in pairs(Factions.entries) do
		enum:addEnumItem(v.Name)
	end

end

function Factions:verifyFactionListings()

	for i=1, #tb.ShipClasses do

		local entry = tb.ShipClasses[i]
		local m_string = self:getManufacturerFromCustomData(entry, true)

		if not m_string == "" and not m_string == nil then
			if not self:checkFactionExists(m_string) then
				ba.warning("Could not find Manufacturer named '" .. m_string .. "' in Factions.tbl")
			end
		end

		local f_string = self:getFactionFromCustomData(entry, true)

		if not f_string == "" and not f_string == nil then
			if not self:checkFactionExists(f_string) then
				ba.warning("Could not find Faction named '" .. f_string .. "' in Factions.tbl")
			end
		end
	end

	for i=1, #tb.WeaponClasses do

		local entry = tb.WeaponClasses[i]
		local m_string = self:getManufacturerFromCustomData(entry)

		if not m_string == "" and not m_string == nil then
			if not self:checkFactionExists(m_string) then
				ba.warning("Could not find Manufacturer named '" .. m_string .. "' in Factions.tbl")
			end
		end

		local f_string = self:getFactionFromCustomData(entry)

		if not f_string == "" and not f_string == nil then
			if not self:checkFactionExists(f_string) then
				ba.warning("Could not find Faction named '" .. f_string .. "' in Factions.tbl")
			end
		end
	end
end

function Factions:parseTables()
	ScpuiSystem:parseTable(Factions, Factions.parseFactions, "factions", "fctn")

	if #Factions.entries > 0 then
		Factions:createLuaEnum()
	end

	Factions:verifyFactionListings()
end

Factions:parseTables()