"""
Converts RSB files to png files and an acompanying JSON file with extra meta data
information for file format from here:
https://github.com/AlexKimov/RSE-file-formats/wiki/RSB-File-Format
https://github.com/AlexKimov/RSE-file-formats/blob/master/010Editor-templates/RSB.bt
RSB Version 0 required looking into the bt file directly as the wiki is not complete
Texture and surface data extraction to JSON file is not complete
"""

from FileUtilities import DirectoryProcessor
from FileUtilities import JSONMetaInfo, OBJModelWriter
from RainbowFileReaders import SOBModelReader
from RainbowFileReaders.MathHelpers import is_vector_normal
from Settings import load_settings

def convert_SOB(filename):
    """ Reads an SOB file and then writes to OBJ format """
    print("Processing: " + filename)

    modelFile = SOBModelReader.SOBModelFile()
    modelFile.read_file(filename)

    meta = JSONMetaInfo.JSONMetaInfo()
    meta.add_info("filecontents", modelFile)
    meta.add_info("filename", filename)
    newFilename = filename + ".JSON"
    meta.writeJSON(newFilename)

    countBadNormals = 0
    countGoodNormals = 0
    for geoObj in modelFile.geometryObjects:
        for vertexParam in geoObj.vertexParams:
            if is_vector_normal(vertexParam.normal):
                countGoodNormals += 1
            else:
                countBadNormals += 1

    print("Num bad normals: " + str(countBadNormals))
    print("Num good normals: " + str(countGoodNormals))

    write_OBJ(filename + ".obj", modelFile)

    print("===============================================")

def write_OBJ(filename, SOBObject):
    """Writes the given Geometry Object to an OBJ file """
    writer = OBJModelWriter.OBJModelWriter()
    writer.open_file(filename)
    for geoObject in SOBObject.geometryObjects:
        writer.begin_new_object(geoObject.nameString)
        #write vertices
        for i in range(len(geoObject.vertices)):
            vertex = geoObject.vertices[i]
            writer.write_vertex(vertex)
        #write vertex parameters
        for i in range(len(geoObject.vertexParams)):
            normal = geoObject.vertexParams[i].normal
            writer.write_normal(normal)
            UV = geoObject.vertexParams[i].UV
            writer.write_texture_coordinate(UV)
        #write face definitions
        for face in geoObject.faces:
            writer.write_face(face.vertexIndices,face.paramIndices, face.paramIndices)
    writer.close_file()

def main():
    """Main function that converts test data files"""
    settings = load_settings()

    fp = DirectoryProcessor.DirectoryProcessor()
    fp.paths.append(settings["gamePath"])
    fp.fileExt = ".SOB"

    fp.processFunction = convert_SOB

    fp.run(mode=settings["runMode"])

if __name__ == "__main__":
    main()
