# Copyright (C) 2024 Michael Becker  <alcexhim@gmail.com>
# 
# This file is part of yaml2mcl.
# 
# yaml2mcl is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# yaml2mcl 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 yaml2mcl.  If not, see <https://www.gnu.org/licenses/>.

class Section:

	def __init__(self, name : str, data, count : int):
		self.name = name
		self.data = data
		self.count = count

	def get_length(self):
		return self.count * len(self.data)
	
	def write_data(self, f):
		pass

class GuidSection (Section):
	def __init__(self, name : str, data : list):

		Section.__init__(self, name, data, 16)

	def write_data(self, f):
		i = 0
		for guid in self.data:
			if guid is not None:
				f.write(guid.to_bytes())
			else:
				print("guid at " + str(i) + " is None")

				from framework import Guid
				f.write(Guid().to_bytes())

			i += 1

class InstancesSection (Section):
	def __init__(self, name : str, data : list):

		Section.__init__(self, name, data, 16)

	def write_data(self, f):

		for (gi, cgi, ckey, ikey) in self.data:
			f.write(int.to_bytes(cgi, 4, 'little'))
			f.write(int.to_bytes(gi, 4, 'little'))
			f.write(int.to_bytes(ckey, 4, 'little'))
			f.write(int.to_bytes(ikey, 4, 'little'))

class AttributesSection (Section):
	def __init__(self, name : str, data : list):

		Section.__init__(self, name, data, 12)

	def write_data(self, f):

		for (srci, atti, vali, edate) in self.data:
			f.write(int.to_bytes(srci, 4, 'little'))
			f.write(int.to_bytes(atti, 4, 'little'))
			f.write(int.to_bytes(vali, 4, 'little'))

class RelationshipsSection (Section):
	def __init__(self, name : str, data : list):

		Section.__init__(self, name, data, 12)

	def write_data(self, f):
		
		for (srci, reli, tgti, edate) in self.data:
			f.write(int.to_bytes(srci, 4, 'little'))
			f.write(int.to_bytes(reli, 4, 'little'))
			f.write(int.to_bytes(tgti, 4, 'little'))

class StringTableSection (Section):

	def __init__(self, name : str, data : list):

		Section.__init__(self, name, data, 0)

	def get_length(self):
		length = 0
		for item in self.data:
			length += len(item) + 1
		return length

	def write_data(self, f):
		for item in self.data:
			f.write(item.encode("utf-8"))
			f.write(int.to_bytes(0, 1, 'little'))

class ResourcesSection (Section):

	def __init__(self, name : str, data : list):

		Section.__init__(self, name, data, 0)

	def get_length(self):
		length = 0
		for item in self.data:
			length += len(item)
		return length

	def write_data(self, f):
		for rsrc in self.data:
			f.write(rsrc)

class DefinitionsSection(Section):
	def __init__(self, data: list):
		Section.__init__(self, 'Defs', data, 0)

	def get_length(self):
		length = 0
		for (k, v) in self.data:
			length += len(k) + 1 + len(v) + 1
		return length
	def write_data(self, f):
		for (k, v) in self.data:
			f.write(k.encode('utf-8'))
			f.write(int.to_bytes(0, 1))
			f.write(v.encode('utf-8'))
			f.write(int.to_bytes(0, 1))

class TemplatesSection(Section):
	def __init__(self, data: list):
		Section.__init__(self, 'Templates', data, 0)

	def get_length(self):
		length = 0
		for key in self.data:
			length += 8
			length += len(key)
			length += len(self.data[key])

		return length
	def write_data(self, f):
		for item in self.data:
			f.write(int.to_bytes(len(item), 4))
			f.write(int.to_bytes(len(self.data[item]), 4))

		for item in self.data:
			f.write(item.encode('utf-8'))
		
		for item in self.data:
			f.write(self.data[item].encode('utf-8'))