import tag_header,struct

class MESH:
    NUMBER_OF_NEXT_MESH_TAGS=2
    NUMBER_OF_CUTSCENE_MOVIE_TAGS=3
    NUMBER_OF_STORYLINE_STRING_TAGS=4
    NUMBER_OF_SCREEN_COLLECTION_TAGS=3
    def __init__(self,dFile,dOffset,localFile, filePath):
        self.fPath=filePath
        self.tagHeader=tag_header.Tag_Header(dFile,dOffset)
        if localFile>0:
            self.isLocal=True
        else:
            dFile.seek(self.tagHeader.getOffset())
            self.isLocal=False
        
        self.landscape_collection_tag=struct.unpack(">L", dFile.read(4))[0]
        self.media_tag=struct.unpack(">L", dFile.read(4))[0]
        
        self.submesh_width=struct.unpack(">h", dFile.read(2))[0] 
        self.submesh_height=struct.unpack(">h", dFile.read(2))[0]
        self.mesh_offset=struct.unpack(">l", dFile.read(4))[0] 
        self.mesh_size=struct.unpack(">l", dFile.read(4))[0]
        self.mesh=struct.unpack(">l", dFile.read(4))[0]
     
        self.data_offset=struct.unpack(">l", dFile.read(4))[0] 
        self.data_size=struct.unpack(">l", dFile.read(4))[0]
        self.data=struct.unpack(">l", dFile.read(4))[0]
         
        self.marker_palette_entries=struct.unpack(">l", dFile.read(4))[0] 
        self.marker_palette_offset=struct.unpack(">l", dFile.read(4))[0]  
        self.marker_palette_size=struct.unpack(">l", dFile.read(4))[0] 
        self.marker_palette=struct.unpack(">l", dFile.read(4))[0] 
         
        self.marker_count=struct.unpack(">l", dFile.read(4))[0] 
        self.markers_offset=struct.unpack(">l", dFile.read(4))[0] 
        self.markers_size=struct.unpack(">l", dFile.read(4))[0]
        self.markers=struct.unpack(">l", dFile.read(4))[0]
     
        self.mesh_lighting_tag=struct.unpack(">L", dFile.read(4))[0] # shadow intensity, ambient light, etc.
        self.connector_tag=struct.unpack(">L", dFile.read(4))[0] # for fences
        self.flags=struct.unpack(">L", dFile.read(4))[0]
        self.particle_system_tag=struct.unpack(">L", dFile.read(4))[0]
        self.team_count=struct.unpack(">l", dFile.read(4))[0]
        #     
        self.dark_fraction=struct.unpack(">h", dFile.read(2))[0]
        self.light_fraction=struct.unpack(">h", dFile.read(2))[0]
        self.dark_color=dFile.read(8) #     struct rgb_color dark_color;
        self.light_color=dFile.read(8) #     struct rgb_color light_color;
        self.transition_point=struct.unpack(">L", dFile.read(4))[0]
        # 
        self.ceiling_height=struct.unpack(">h", dFile.read(2))[0]
        self.unused1=struct.unpack(">h", dFile.read(2))[0]
        # 
        #     // this is not a rectangle; these are the buffers on each edge of the map, in cells
        self.edge_of_mesh_buffer_zones=dFile.read(8) #     rectangle2d edge_of_mesh_buffer_zones;
        #     
        self.global_ambient_sound_tag=struct.unpack(">L", dFile.read(4))[0]
        # 
        self.map_action_count=struct.unpack(">l", dFile.read(4))[0]
        self.map_actions_offset=struct.unpack(">l", dFile.read(4))[0] 
        self.map_action_buffer_size=struct.unpack(">l", dFile.read(4))[0]
        # 
        self.map_description_string_list_tag=struct.unpack(">L", dFile.read(4))[0]
        self.postgame_collection_tag=struct.unpack(">L", dFile.read(4))[0]
        self.pregame_collection_tag=struct.unpack(">L", dFile.read(4))[0]
        self.overhead_map_collection_tag=struct.unpack(">L", dFile.read(4))[0]
        self.next_mesh_tags=[]
        for i in range(self.NUMBER_OF_NEXT_MESH_TAGS):
            self.next_mesh_tags.append(struct.unpack(">L", dFile.read(4))[0])
        self.cutscene_movie_tags=[]
        for i in range(self.NUMBER_OF_CUTSCENE_MOVIE_TAGS):
            self.cutscene_movie_tags.append(struct.unpack(">L", dFile.read(4))[0])
        self.storyline_string_tags=[]
        for i in range(self.NUMBER_OF_STORYLINE_STRING_TAGS):
            self.storyline_string_tags.append(struct.unpack(">L", dFile.read(4))[0])
        self.media_coverage_region_offset=struct.unpack(">L", dFile.read(4))[0]
        #     
        self.media_coverage_region_size=struct.unpack(">L", dFile.read(4))[0]
        self.media_coverage_region=struct.unpack(">h", dFile.read(2))[0]
        #     
        self.mesh_LOD_data_offset=struct.unpack(">L", dFile.read(4))[0]
        self.mesh_LOD_data_size=struct.unpack(">L", dFile.read(4))[0]
        self.mesh_LOD_data=struct.unpack(">L", dFile.read(4))[0]
        # 
        self.global_tint_color=dFile.read(8) #     struct rgb_color global_tint_color;
        self.global_tint_fraction=struct.unpack(">h", dFile.read(2))[0]
        self.pad=struct.unpack(">L", dFile.read(4))[0]
        #     
        self.wind_tag=struct.unpack(">L", dFile.read(4))[0]
        self.screen_collection_tags=[]
        for i in range(self.NUMBER_OF_SCREEN_COLLECTION_TAGS):
            self.screen_collection_tags.append(struct.unpack(">L", dFile.read(4))[0])
        #     
        self.blood_color=dFile.read(8) #     struct rgb_color blood_color;
        # 
        self.picture_caption_string_list_tag=struct.unpack(">L", dFile.read(4))[0]
        self.narration_sound_tag=struct.unpack(">L", dFile.read(4))[0]
        self.win_ambient_sound_tag=struct.unpack(">L", dFile.read(4))[0]
        self.loss_ambient_sound_tag=struct.unpack(">L", dFile.read(4))[0]
        # 
        #   For Creative's Environmental Audio Extensions
        self.reverb_environment=struct.unpack(">L", dFile.read(4))[0] 
        self.reverb_volume=struct.unpack(">f", dFile.read(4))[0]
        self.reverb_decay_time=struct.unpack(">f", dFile.read(4))[0]
        self.reverb_damping=struct.unpack(">f", dFile.read(4))[0]
        
        #   array of connectors (i.e. fences)
        self.connector_count=struct.unpack(">l", dFile.read(4))[0] 
        self.connectors_offset=struct.unpack(">l", dFile.read(4))[0]  
        self.connectors_size=struct.unpack(">l", dFile.read(4))[0] 
        self.connectors=struct.unpack(">l", dFile.read(4))[0]  #What is this?     struct saved_connector_data *connectors;
     
        self.cutscene_names=[]
        for i in range(self.NUMBER_OF_CUTSCENE_MOVIE_TAGS):
            self.cutscene_names.append(dFile.read(64))
     
        self.hints_string_list_tag=struct.unpack(">L", dFile.read(4))[0]
        # 
        self.fog_color=dFile.read(8) #     struct rgb_color fog_color;
        self.fog_density=struct.unpack(">f", dFile.read(4))[0]
        
        self.difficulty_level_override_string_list_tag=struct.unpack(">L", dFile.read(4))[0]
        self.team_names_override_string_list_tag=struct.unpack(">L", dFile.read(4))[0]
     
        self.plugin_name=dFile.read(32)
        self.extra_flags=struct.unpack(">L", dFile.read(4))[0]
        self.unused=dFile.read(442)
        
        self.connector_type=struct.unpack(">h", dFile.read(2))[0]
        self.map_description_string_index=struct.unpack(">h", dFile.read(2))[0]
        self.overhead_map_collection_index=struct.unpack(">h", dFile.read(2))[0]
        self.landscape_collection_index=struct.unpack(">h", dFile.read(2))[0]
        self.global_ambient_sound_index=struct.unpack(">h", dFile.read(2))[0]
        self.media_type=struct.unpack(">h", dFile.read(2))[0]
        self.hints_string_list_index=struct.unpack(">h", dFile.read(2))[0]
         
        #     byte editor_data[MESH_HEADER_EDITOR_DATA_SIZE];
        dFile.close
    
    def getLinkedTags(self):
        tmpList=[]
        if tag_header.tagCheck(self.landscape_collection_tag) != "None":
            tmpList.append((self.landscape_collection_tag, '.256'))
        if tag_header.tagCheck(self.media_tag) != "None":
            tmpList.append((self.media_tag, 'medi'))
        if tag_header.tagCheck(self.mesh_lighting_tag) != "None":
            tmpList.append((self.mesh_lighting_tag, 'meli'))
        if tag_header.tagCheck(self.connector_tag) != "None":
            tmpList.append((self.connector_tag, 'conn'))
        if tag_header.tagCheck(self.particle_system_tag) != "None":
            tmpList.append((self.particle_system_tag, 'part'))
        if tag_header.tagCheck(self.map_description_string_list_tag) != "None":
            tmpList.append((self.map_description_string_list_tag, 'stli'))
        if tag_header.tagCheck(self.postgame_collection_tag) != "None":
            tmpList.append((self.postgame_collection_tag, '.256'))
        if tag_header.tagCheck(self.pregame_collection_tag) != "None":
            tmpList.append((self.pregame_collection_tag, '.256'))
        if tag_header.tagCheck(self.overhead_map_collection_tag) != "None":
            tmpList.append((self.overhead_map_collection_tag, '.256'))
        for i in range(self.NUMBER_OF_NEXT_MESH_TAGS):    
            if tag_header.tagCheck(self.next_mesh_tags[i]) != "None":
                tmpList.append((self.next_mesh_tags[i], 'mesh'))
        for i in range(self.NUMBER_OF_CUTSCENE_MOVIE_TAGS):
            if tag_header.tagCheck(self.cutscene_movie_tags[i]) != "None":
                tmpList.append((self.cutscene_movie_tags[i], 'cuts'))
        for i in range(self.NUMBER_OF_STORYLINE_STRING_TAGS):
            if tag_header.tagCheck(self.storyline_string_tags[i]) != "None":
                tmpList.append((self.storyline_string_tags[i], 'text'))
        if tag_header.tagCheck(self.wind_tag) != "None":
            tmpList.append((self.wind_tag, 'wind'))
        for i in range(self.NUMBER_OF_SCREEN_COLLECTION_TAGS):
            if tag_header.tagCheck(self.screen_collection_tags[i]) != "None":
                tmpList.append((self.screen_collection_tags[i], '.256'))
        if tag_header.tagCheck(self.picture_caption_string_list_tag) != "None":
            tmpList.append((self.picture_caption_string_list_tag, 'stli'))
        if tag_header.tagCheck(self.narration_sound_tag) != "None":
            tmpList.append((self.narration_sound_tag, 'soun'))
        if tag_header.tagCheck(self.win_ambient_sound_tag) != "None":
            tmpList.append((self.win_ambient_sound_tag, 'soun'))
        if tag_header.tagCheck(self.loss_ambient_sound_tag) != "None":
            tmpList.append((self.loss_ambient_sound_tag, 'soun'))
        if tag_header.tagCheck(self.hints_string_list_tag) != "None":
            tmpList.append((self.hints_string_list_tag, 'stli'))
        if tag_header.tagCheck(self.difficulty_level_override_string_list_tag) != "None":
            tmpList.append((self.difficulty_level_override_string_list_tag, 'stli'))
        if tag_header.tagCheck(self.team_names_override_string_list_tag) != "None":
            tmpList.append((self.team_names_override_string_list_tag, 'stli'))
        return tmpList
    
