import templatetools
import clr
clr.AddReference("Ansys.ACT.Interfaces")
from Ansys.ACT.Interfaces.Common import *
from Ansys.ACT.Interfaces.Analysis import *

#------------------------------------------------------------
#       Select Entity 
#------------------------------------------------------------

class SelectEntity:
    
    def __init__(self, ExtAPI, entity, prop):
        pass

    #------------------------------
    #       Callbacks 
    #------------------------------

    def onadd(self, entity, prop):
        prop.Value = -1

    def getvalue(self, entity, prop, val):
        if val == None: return None
        return self._EntityById(entity.Analysis, int(val))

    def onactivate(self, entity, prop):
        prop.Options.Clear()

        types = templatetools.ParseFilter(prop.Attributes["type_filter"])
        names = templatetools.ParseFilter(prop.Attributes["name_filter"])
        entities = self._Entities(entity.Analysis, types)

        for entity in entities:
            if names != None and not entity.Name in names : continue
            prop.Options.Add(str(entity.Id))

    def value2string(self, entity, prop, val):
        entity = self._EntityById(entity.Analysis, int(val))
        if entity == None: return ""
        return entity.Caption
        
    def isvalid(self, entity, prop):
        return prop.Value != None
    
    #------------------------------
    #       Private 
    #------------------------------

    def _Entities(self, analysis, type = None):
        entities = []
        if type == None:
            entities += list(analysis.LoadObjects)
            entities += list(analysis.ResultObjects)
        else:
            if 'load'   in type: entities += list(analysis.LoadObjects)
            if 'result' in type: entities += list(analysis.ResultObjects)
        return entities

    def _EntityById(self, analysis, id):
        for entity in self._Entities(analysis):
            if entity.Id == id :
                return entity
        return None

#------------------------------------------------------------
#       Select Coordinate System 
#------------------------------------------------------------

class SelectCoordinateSystem:

    def __init__(self, ExtAPI, entity, prop):
        self.ExtAPI = ExtAPI
        pass

    #------------------------------
    #       Callbacks 
    #------------------------------

    #def onadd(self, entity, prop):
    #    prop.Value = -1

    def getvalue(self,load,prop,val):
        if val == None: return None
        if not hasattr(load, 'Analysis') or load.Analysis==None:
            return self.ExtAPI.DataModel.CoordinateSystemById(int(val))
        else:
            return load.Analysis.CoordinateSystemById(int(val))

    def onactivate(self,load,prop):
        prop.Options.Clear()

        types = self._getTypes(templatetools.ParseFilter(prop.Attributes["type_filter"]))
        systems = []
        if not hasattr(load, 'Analysis') or load.Analysis==None:
            systems = self.ExtAPI.DataModel.CoordinateSystems
        else:
            systems = load.Analysis.CoordinateSystems

        for system in systems:
            if types != None and not system.CoordinateSystemType in types: continue
            prop.Options.Add(str(system.Id))

    def value2string(self,load,prop,val):
        system = None
        if val!=None:
            if not hasattr(load, 'Analysis') or load.Analysis==None:
                system = self.ExtAPI.DataModel.CoordinateSystemById(int(val))
            else:
                system = load.Analysis.CoordinateSystemById(int(val))
        if system == None: return ''
        return system.Name

    def isvalid(self,load, prop):
        return prop.Value != None

    #------------------------------
    #       Private 
    #------------------------------

    def _getTypes(self, strtypes):
        if strtypes == None: return None
        types = []
        for type in strtypes:
            if   type.lower() == 'cartesian' : types.append(CoordinateSystemTypeEnum.Cartesian)
            elif type.lower() == 'cylindrical' : types.append(CoordinateSystemTypeEnum.Cylindrical)
            elif type.lower() == 'spherical' : types.append(CoordinateSystemTypeEnum.Spherical)
        return types
       
   