#
# 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                          Larry Pearce

import os
import apex_sdk

import System
import System.Windows.Controls as WPFControls


def getUIContent():

    my_toolProperty = apex_sdk.ToolPropertyContainer()
    my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

    my_toolProperty.TitleText = "Model Name 1.0"

    file_path = os.path.dirname(os.path.realpath(__file__))
    my_toolProperty.TitleImageUriString = file_path+"../res/modelName.png"

    my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>Displays Model Name</SPAN></P>
    <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>"""

    my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))

    #my_toolProperty.ShowPickChoice = True
    #my_toolProperty.PickFilterList = setPickFilterList()

    return my_toolProperty


def setPickFilterList():
    #Create an empty List of strings
    pickChoices = System.Collections.Generic.List[System.String]()

    #exclusive picking and visibility picking
    pickChoices.Add(apex_sdk.PickFilterTypes.ExclusivePicking)
    pickChoices.Add(apex_sdk.PickFilterTypes.VisibilityPicking)

    #Part
    pickChoices.Add(apex_sdk.PickFilterTypes.Assembly)
    pickChoices.Add(apex_sdk.PickFilterTypes.Part)

    #Return the pick filter list
    return pickChoices


def getCustomToolPropertyContent():

    my_Grid = WPFControls.Grid()

    createLayout(my_Grid, 8, 2)

    global nameTextBox
    global pathTextBox

    iRow = iCol = 0
    nameTextBox = createDataBox(my_Grid, "Model:", "", iRow, iCol)

    iRow += 1
    pathTextBox = createDataBox(my_Grid, "Path:", "", iRow, iCol)

    HandleApplyButton()

    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


@apex_sdk.errorhandler
def HandleApplyButton():

    file_path = os.path.dirname(os.path.realpath(__file__))
    script_path= os.path.join(file_path, 'auModelName.py')

    apex_sdk.runScriptFunction(script_path, "main", callback=UpdateNameTextBox)


#This function receives data from the Script API function and updates the Tool UI 
@apex_sdk.errorhandler
def UpdateNameTextBox():
    ret_dict = apex_sdk.getScriptFunctionReturnData()
    nameTextBox.Text = ret_dict["name"]
    pathTextBox.Text = ret_dict["path"]


