import System
import clr
import sys
from PropertyGroupEditor import *
from Ansys.ACT.Interfaces.UserObject import *

class TFTabularData(PGEditor):
    def __init__(self,api,load,prop):
        PGEditor.__init__(self,api,load,prop)
        self.steps = []
        self.WithAddLine = False
        self.WithDeleteLine = False
        
    def CreateUI(self):
        PGEditor.CreateUI(self)
    
    def InitLines(self):
        self.steps = self.load.Analysis.StepsEndTime
        irow = 0
        for step in self.steps:
            self.AddLine(irow)
            icol = 0
            for p in self.prop.AllDescendants:
                if p.IsGroup and p.Display!=PropertyDisplayEnum.Property:
                    continue
                if p.Name=="Step":
                    cell = self.ui.Grid.Cells[irow,icol]
                    cellDef = cell.Tag
                    cellDef.Value = irow+1
                elif p.Name=="Time":
                    cell = self.ui.Grid.Cells[irow,icol]
                    cellDef = cell.Tag
                    cellDef.Value = self.steps[irow]
                icol += 1
            self.RefreshLine(irow)
            irow += 1

    def isvalid(self,load,prop):
        self.steps = self.load.Analysis.StepsEndTime
        while prop.RowCount>self.steps.Count:
             prop.DeleteRow(prop.RowCount-1)
        if prop.RowCount!=self.steps.Count:
            return False
        if prop.ValidState==ValidStateEnum.StateUnknown:
            isv = True
            for i in range(prop.RowCount):
                prop.ActiveRow = i
                for p in prop.Properties.AllDescendants:
                    if p.Visible:
                        if p.IsGroup:
                            if p.Display==PropertyDisplayEnum.Property:
                                isv &= p.IsValid;
                        else:
                            isv &= p.IsValid;
            return isv
        else:
            if prop.ValidState==ValidStateEnum.StateValid:
                return True
            return False