import clr
clr.AddReference("Ansys.ACT.Core")
import Ansys
from Ansys.ACT.Core.UI import *
from Ansys.ACT.Interfaces import *
from Ansys.ACT.Core.XmlDataModel import *
from System.Collections.Generic import List

class TabularDataEditor:
    DialogName = "TabularDataDialog"
    ComponentName = "TabularDataEditorDialog"

    def __init__(self, api, step, prop):
        self.dialog = None
        self.prop = prop
        self.propClone = None
        self.step = step

    def buttonClicked(self, sender, args):
        tabularDataComp = self.dialog.GetComponent(TabularDataEditor.ComponentName)

        # no "our" dialog
        if self.prop != tabularDataComp.GetPropertyTable():
            return

        if args.ButtonName == "Cancel":
            self.prop.Cancel()
            self.prop.CopyFrom(self.propClone, True)
            # we change the property so we have to refresh the compoenent with the "old" table
            tabularDataComp.SetPropertyTable(self.prop)
            tabularDataComp.Refresh()
        elif args.ButtonName == "Apply":
            # update properties and finish buttons states
            self.prop.Apply()
            self.step.NotifyChange()
        self.dialog.Hide()

    def createTabularDialog(self, panel, title, width, height):
        dialog = panel.CreateDialog(TabularDataEditor.DialogName, title, width, height)

        layoutDef = LayoutDefinition()
        layoutDef.Extension = None

        layout = Ansys.ACT.Core.XmlDataModel.UI.Layout()
        layoutDef.Layout = layout
        layout.Name = "TabularDataDialogLayout"
        layout.Components = List[Ansys.ACT.Core.XmlDataModel.UI.Component]()

        component = Ansys.ACT.Core.XmlDataModel.UI.Component()
        component.Name = TabularDataEditor.ComponentName
        component.TopAttachment = ""
        component.TopOffset = 0
        component.BottomAttachment = "Buttons"
        component.BottomOffset = 0
        component.LeftAttachment = ""
        component.LeftOffset = 0
        component.RightAttachment = ""
        component.RightOffset = 0
        component.WidthType = Ansys.ACT.Interfaces.UserInterface.ComponentLengthType.Percentage
        component.WidthValue = 100
        component.HeightType = Ansys.ACT.Interfaces.UserInterface.ComponentLengthType.Percentage
        component.HeightValue = 100
        component.ComponentType = "tabularDataComponent"
        layout.Components.Add(component)

        component = Ansys.ACT.Core.XmlDataModel.UI.Component()
        component.Name = "Buttons"
        component.TopAttachment = TabularDataEditor.ComponentName
        component.TopOffset = 0
        component.BottomAttachment = ""
        component.BottomOffset = 0
        component.LeftAttachment = ""
        component.LeftOffset = 0
        component.RightAttachment = ""
        component.RightOffset = 0
        component.WidthType = Ansys.ACT.Interfaces.UserInterface.ComponentLengthType.Percentage
        component.WidthValue = 100
        component.HeightType = Ansys.ACT.Interfaces.UserInterface.ComponentLengthType.FitToContent
        component.ComponentType = "buttonsComponent"
        layout.Components.Add(component)

        dialog.SetLayout(layoutDef)

        buttonComp = dialog.GetComponent("Buttons")
        buttonComp.AddButton("Apply", "Apply", Ansys.ACT.Interfaces.UserInterface.ButtonPositionType.Center)
        buttonComp.AddButton("Cancel", "Cancel", Ansys.ACT.Interfaces.UserInterface.ButtonPositionType.Center)

        return dialog

    def onshow(self, step, prop):
        self.dialog = step.UserInterface.GetComponent(TabularDataEditor.DialogName)
        if self.dialog is None:
            self.dialog = self.createTabularDialog(step.UserInterface.Panel, "TabularData", 100, 100)

        buttonComp = self.dialog.GetComponent("Buttons")
        buttonComp.ButtonClicked += self.buttonClicked

    def onhide(self, step, prop):
        self.dialog = None

    def onactivate(self, step, prop):
        # we try to create the smaller dialogBox possible to not use unnecessary space
        columnWidth = 150
        selectionColumnWidth = 30
        thDialogWidth = columnWidth * prop.Properties.Count + selectionColumnWidth + 2 # 2*1 is border
        maxDialogWidth = 500
        minDialogWidth = 150
        dialogWidth = max(min(thDialogWidth, maxDialogWidth), minDialogWidth)

        self.dialog.GetComponent(TabularDataEditor.ComponentName).SetPropertyTable(prop)
        self.dialog.GetComponent(TabularDataEditor.ComponentName).Refresh()
        self.dialog.SetDatas("TabularData", dialogWidth, dialogWidth)
        self.dialog.Show()

        # we save all value to take it back if we press the cancel button
        self.propClone = prop.Clone()

    def value2string(self, step, prop, value):
        return "Tabular Data"
