#
# Copyright © 2020 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#
# Author: Larry Pearce
# Revised by: Larry Pearce
# Revision History
# Date         Change                                 By
# 2020-10-12   Original code (1.0)                    Larry Pearce

import os
import apex_sdk


#.NET references
import System
import System.Windows.Controls as WPFControls

#setting pre-defined properties of tool_propertyContainer
def getUIContent():
    my_toolProperty = apex_sdk.ToolPropertyContainer()

    my_toolProperty.TitleText = "Label Short Free Edges 1.0"

    file_path = os.path.dirname(os.path.realpath(__file__))
    my_toolProperty.TitleImageUriString = os.path.join(os.path.dirname(file_path), r"res\auLabelFreeEdges.png")

    my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>Labels free geometry edges</SPAN></P>
    <P><SPAN> </SPAN></P>
    <P STYLE=\"font-size:10;\"><SPAN>This tool is useful when you import a solid that isn't a solid because of a short free edge that is difficult to see</SPAN></P></BODY></HTML>
    <P><SPAN> </SPAN></P>
    <P STYLE=\"font-size:10;\"><SPAN>Send questions, comments and Elvis sightings to larry.pearce@mscsoftware.com</SPAN></P></BODY></HTML>"""


    # handle apply button (green) click event
    my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))
    # handle exit button (red) click event
    my_toolProperty.ExitCommand = apex_sdk.ActionCommand(System.Action(HandleExitButton))

    # set property content
    my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

    #return my_toolProperty
    return my_toolProperty


def getCustomToolPropertyContent():
    my_Grid = WPFControls.Grid()
    createLayout(my_Grid, 4, 2)

    row = col = 0

    global tolTextBox
    #tolTextBox = createDataBox(my_Grid, "Tolerance:", "0.1", row, col)
    #row += 1

    labelEdgesBtn = WPFControls.Button()
    labelEdgesBtn.Content="Label Free Edges"
    labelEdgesBtn.Click+=HandleLabelEdgesBtn
    WPFControls.Grid.SetColumnSpan(labelEdgesBtn, 2)
    WPFControls.Grid.SetRow(labelEdgesBtn, row)
    WPFControls.Grid.SetColumn(labelEdgesBtn, col)
    my_Grid.Children.Add(labelEdgesBtn)
    row += 1

    clearLabelsBtn = WPFControls.Button()
    clearLabelsBtn.Content="Clear Labels"
    clearLabelsBtn.Click+=HandleClearLabelsBtn
    WPFControls.Grid.SetColumnSpan(clearLabelsBtn, 2)
    WPFControls.Grid.SetRow(clearLabelsBtn, row)
    WPFControls.Grid.SetColumn(clearLabelsBtn, col)
    my_Grid.Children.Add(clearLabelsBtn)

    return my_Grid


def createLayout(parent, numRows, numCols):
    for k in range(numRows):
        parent.RowDefinitions.Add(WPFControls.RowDefinition())
    for k in range(numCols):
        parent.ColumnDefinitions.Add(WPFControls.ColumnDefinition())


def createDataBox(parent, title, value, row, col):
    textBlock = WPFControls.TextBlock()
    textBlock.Text = title
    WPFControls.Grid.SetRow(textBlock, row)
    WPFControls.Grid.SetColumn(textBlock, col)

    textBox =WPFControls.TextBox()
    WPFControls.Grid.SetRow(textBox, row)
    WPFControls.Grid.SetColumn(textBox, col+1)
    textBox.Text=value

    parent.Children.Add(textBlock)
    parent.Children.Add(textBox)

    return textBox


# apply button (green) from tool header
@apex_sdk.errorhandler
def HandleApplyButton():
    dictionary = {}
    #dictionary["tol"]= tolTextBox.Text

    file_path = os.path.dirname(os.path.realpath(__file__))
    script_path= os.path.join(file_path, 'auLabelFreeEdges.py')
    apex_sdk.runScriptFunction(script_path, "main", dictionary)


# exit button (red) from tool header
@apex_sdk.errorhandler
def HandleExitButton():
    print("HandleExitButton")


@apex_sdk.errorhandler
def HandleClearLabelsBtn(sender, args):
    file_path = os.path.dirname(os.path.realpath(__file__))
    script_path = os.path.join(file_path, 'auClearGraphicsText.py')
    apex_sdk.runScript(script_path)


@apex_sdk.errorhandler
def HandleLabelEdgesBtn(sender, args):
    HandleApplyButton()

