#
# 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-13   Original code (1.7)                    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 = "Hole-to-Hole Connections 1.7"

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

    my_toolProperty.WorkFlowInstructions = """<HTML><BODY><P STYLE=\"font-size:10;\"><SPAN>AuFast 1.6</SPAN></P>
    <P><SPAN> </SPAN></P>
    <P><SPAN>This script creates hole-to-hole fastener connections for surface-to-surface, surface-to-solid, and 
    solid-to-solid connections.  Slotted holes are supported in solids.  Hole-to-Point connections are also 
    supported.  Point-to-Point connections are always rigid connections because the hole diameter is unknown.</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>"""

    # 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))

    my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()

    # define pickFiterTools
    my_toolProperty.ShowPickChoice = True
    my_toolProperty.IsCustomTool   = True
    # setPickFilterTools()
    my_toolProperty.PickFilterList = setPickFilterTools()

    return my_toolProperty


def getCustomToolPropertyContent():

    my_Grid = WPFControls.Grid()

    nRows = 8
    nCols = 2
    createLayout(my_Grid, nRows, nCols)
    iRow = iCol = 0

    global nameTextBox
    nameTextBox = createDataBox(my_Grid, "Name: ", "", iRow, iCol)
    iRow += 1

    global fastenerTypeComboBox
    fastenerTypeComboBox = createComboBox2(my_Grid, "Fastener Type:", ["Flexible", "Bushing", "Rigid"], 0, iRow, iCol)
    iRow += 1

    global distributionTypeComboBox
    distributionTypeComboBox = createComboBox2(my_Grid, "Distribution:", ["Compliant", "Rigid"], 0, iRow, iCol)
    iRow += 1

    global materialComboBox
    global matlComboTextBox
    matlComboTextBlock = createTextBlock(my_Grid, "Material:", iRow, 0)
    materialComboBox   = createComboBox(my_Grid, iRow, 1)
    iRow += 1

    file_path = os.path.dirname(os.path.realpath(__file__))
    script_path= os.path.join(file_path, './auGetMaterials.py')
    apex_sdk.runScriptFunction(script_path, "main", callback=getMatls)

    global toleranceTextBox
    toleranceTextBox = createDataBox(my_Grid, "Tolerance:", "0.1", iRow, iCol)
    iRow += 1

    global maxDiameterTextBox
    maxDiameterTextBox = createDataBox(my_Grid, "Max Diameter:", "0.75", iRow, iCol)
    iRow += 1

    global attachWasherToggle, washerFactorTextBlock, washerFactorTextBox

    attachWasherToggle =  createCheckBox(my_Grid, "Attach Shell Mesh Washer", iRow, 0)
    iRow += 1

    attachWasherToggle.Checked   += attachWasherHandleCheck
    attachWasherToggle.Unchecked += attachWasherHandleUnCheck

    washerFactorTextBlock = createTextBlock(my_Grid, "Washer Factor (>1.0):", iRow, 0)
    washerFactorTextBox   = createTextBox(my_Grid,   "1.2",              iRow, 1)
    iRow += 1

    washerFactorTextBlock.Visibility = System.Windows.Visibility.Collapsed
    washerFactorTextBox.Visibility = System.Windows.Visibility.Collapsed

    return my_Grid


def attachWasherHandleCheck(sender, event):
    global washerFactorTextBlock, washerFactorTextBox

    washerFactorTextBlock.Visibility = System.Windows.Visibility.Visible
    washerFactorTextBox.Visibility = System.Windows.Visibility.Visible


def attachWasherHandleUnCheck(sender, event):
    global washerFactorTextBlock, washerFactorTextBox

    washerFactorTextBlock.Visibility = System.Windows.Visibility.Collapsed
    washerFactorTextBox.Visibility = System.Windows.Visibility.Collapsed


def createCheckBox(parent, label, row, col):

    checkBox = WPFControls.CheckBox()
    checkBox.Content = label
    checkBox.Margin = System.Windows.Thickness(5.)

    WPFControls.Grid.SetRow(checkBox, row)
    WPFControls.Grid.SetColumn(checkBox, col)

    parent.Children.Add(checkBox)

    return checkBox


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 createComboBox(parent, row, col):
    comboBox = WPFControls.ComboBox()

    comboBox.Margin = System.Windows.Thickness(5.)

    WPFControls.Grid.SetRow(comboBox, row)
    WPFControls.Grid.SetColumn(comboBox, col)

    parent.Children.Add(comboBox)

    return comboBox


def updateComboBox(comboBox, comboItems):

    for comboItem in comboItems:
        item = WPFControls.ComboBoxItem()
        item.Content=comboItem
        comboBox.Items.Add(item)

    comboBox.SelectedIndex="0"


def createTextBlock(parent, text, row, col):
    textBlock = WPFControls.TextBlock()
    textBlock.Text = text

    textBlock.Margin = System.Windows.Thickness(5.)

    WPFControls.Grid.SetRow(textBlock, row)
    WPFControls.Grid.SetColumn(textBlock, col)
    parent.Children.Add(textBlock)

    return textBlock


def createTextBox(parent, value, row, col):
    textBox =WPFControls.TextBox()

    textBox.Margin = System.Windows.Thickness(5.)

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

    parent.Children.Add(textBox)

    return textBox


def createComboBox2(parent, title, labels, selected, row, col):
    textBlock = WPFControls.TextBlock()
    textBlock.Text = title

    textBlock.Margin = System.Windows.Thickness(5.)

    WPFControls.Grid.SetRow(textBlock, row)
    WPFControls.Grid.SetColumn(textBlock, col)

    parent.Children.Add(textBlock)

    comboBox = WPFControls.ComboBox()

    comboBox.Margin = System.Windows.Thickness(5.)

    for label in labels:
        item = WPFControls.ComboBoxItem()
        item.Content=label
        comboBox.Items.Add(item)

    WPFControls.Grid.SetRow(comboBox, row)
    WPFControls.Grid.SetColumn(comboBox, col+1)

    if selected > len(labels): selected = 0
    comboBox.SelectedIndex=str(selected)

    parent.Children.Add(comboBox)

    return comboBox


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

    textBlock.Margin = System.Windows.Thickness(5.)

    WPFControls.Grid.SetRow(textBlock, row)
    WPFControls.Grid.SetColumn(textBlock, col)

    textBox =WPFControls.TextBox()

    textBox.Margin = System.Windows.Thickness(5.)

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

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

    return textBox


def setPickFilterTools():
    pickChoices = System.Collections.Generic.List[System.String]()

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

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

    return pickChoices


@apex_sdk.errorhandler
def HandleApplyButton():
    # Create a Dictionary to store the user defined tool data
    dictionary = {}

    choices = materialComboBox.SelectedValue.ToString().split(":")
    material = choices[len(choices)-1].lstrip().rstrip()

    choices = fastenerTypeComboBox.SelectedValue.ToString().split(":")
    fastenerType = choices[len(choices)-1].lstrip().rstrip()

    choices = distributionTypeComboBox.SelectedValue.ToString().split(":")
    distributionType = choices[len(choices)-1].lstrip().rstrip()

    dictionary["attachWasher"] = attachWasherToggle.IsChecked

    dictionary["washerFactor"] = washerFactorTextBox.Text

    dictionary["name"]             = nameTextBox.Text
    dictionary["material"]         = material
    dictionary["fastenerType"]     = fastenerType
    dictionary["distributionType"] = distributionType
    dictionary["tolerance"]        = toleranceTextBox.Text
    dictionary["maxDiameter"]      = maxDiameterTextBox.Text
    dictionary["testMode"]         = False

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

    apex_sdk.runScriptFunction(script_path, "main", dictionary)


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


# This function receives data from the Script API function and updates the Tool UI
@apex_sdk.errorhandler
def getMatls():
    global matlComboBox

    ret_dict = apex_sdk.getScriptFunctionReturnData()

    matls = ret_dict["matlList"]

    updateComboBox(materialComboBox, matls)
