class LinesReader(list):

    header     = None
    fields     = None
    sub_fields = None
    
    def __init__(self, file_name):
        self.len_header = len(self.header) if isinstance(self.header, list) else 0
        self.len_fields = len(self.fields) if isinstance(self.fields, list) else 0

        self.temp_entry = {}
        for n, line in enumerate(file(file_name, "r")):
            if line == "EOF\n": return

            if n < self.len_header:
                self.readHeader(line.rstrip("\n"), n)
            else:
                #self.readFields(line.rstrip("\n"),(n-len_header) % len_fields)
                self.readBody(line.rstrip("\n"),(n-self.len_header) % self.len_fields)
                
    def readHeader(self, line, header_cursor_position):
        setattr(self, self.header[header_cursor_position], line)

    def readBody(self, line, fields_cursor_position):
        self.temp_entry[self.fields[fields_cursor_position]] = self.getSubFields(line)

        if fields_cursor_position == self.len_fields-1:
            self.append(self.temp_entry)
            self.temp_entry = {}

    def getSubFields(self, line):
        if self.sub_fields == None or len(self.sub_fields) == 0:
            return line
        else:
            sub_fields = self.sub_fields
        
            diff_len = len(sub_fields) - len(line.split())
            if diff_len < 0:
                sub_fields = sub_fields[:-1] + [ sub_fields[-1]+str(n) for n in range(abs(diff_len)+1) ]
            
            sub_entry = {}
            for n, value in enumerate(line.split()):
                sub_entry[sub_fields[n]] = value
            return sub_entry
    
# -------------------------------------------------------------------------------------------

class SurfaceTableFileReader(LinesReader):

    header = ["file_version_number", "number_of_objects"]
    fields = ["first_face", "first_edge", "first_vertex"]

# -------------------------------------------------------------------------------------------

class FacesTableFileReader(LinesReader):
    
    header     = ["file_version_number", "number_of_faces"]
    fields     = ["face"]
    sub_fields = ["face_number", "edge_1", "edge_2", "edge_3", "vertex_1", "vertex_2", "vertex_3", "color"]

# -------------------------------------------------------------------------------------------

class VerticesTableFileReader(LinesReader):

    header     = ["file_version_number", "number_of_vertices"]
    fields     = ["vertex"]
    sub_fields = ["vertex_number", "x", "y", "z", "number_of_incident_edges", "edge"]

# -------------------------------------------------------------------------------------------

class EdgesTableFileReader(LinesReader):

    header     = ["file_version_number", "number_of_edges"]
    fields     = ["edge"]
    sub_fields = ["edge_number", "start_vertex", "end_vertex", "left_face", "right_face"
                  , "head_vertex_left_face_edge", "head_vertex_right_face_edge"
                  , "tail_vertex_left_face_edge", "tail_vertex_right_face_edge"]
    
# -------------------------------------------------------------------------------------------
    
class WedgeReader(object):
    pass

if __name__ == "__main__":
    print("Starting...")

    file_sur = "Cubo.sur"
    file_fac = "Cubo.fac"
    file_vtx = "Cubo.vtx"
    file_edg = "Cubo.edg"

    sur = SurfaceTableFileReader(file_sur)
    fac = FacesTableFileReader(file_fac)
    vtx = VerticesTableFileReader(file_vtx)
    edg = EdgesTableFileReader(file_edg)

    for file_loaded, obj in zip([file_sur, file_fac, file_vtx, file_edg], [sur, fac, vtx, edg]):
        print( "Loaded file '%s' ----> %d entries" % (file_loaded, len(obj)) )
    
