#
# 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.
#
# MSC Apex - User Defined Tools using Python Scripts
# Icon Radio Button UI Example
# 
import sys
import os
import apex_sdk
import clr

# .NET references
import System
import System.Windows.Controls as WPFControls
from Microsoft.Win32 import OpenFileDialog

# Define current file path of example
current_file_path = os.path.dirname(os.path.realpath(__file__))


# Set pre-defined properties of ToolPropertyContainer
def getUIContent():

   my_toolProperty = apex_sdk.ToolPropertyContainer()
   
   # Provide an icon and a name for the tool property panel
   my_toolProperty.TitleImageUriString = os.path.join(os.path.dirname(current_file_path), r"_Tools_Radio Button Icon\resources\_Icon_script.png")

   my_toolProperty.TitleText = "Radio Button Icon"

   # Define UI
   my_toolProperty.ToolPropertyContent = getCustomToolPropertyContent()
   
   # Handle apply button (green) click event
   my_toolProperty.AppliedCommand = apex_sdk.ActionCommand(System.Action(HandleApplyButton))
   
   return my_toolProperty


# Define Layout and Components
def getCustomToolPropertyContent():

   # Create a Grid
   my_Grid = WPFControls.Grid()

   # Add 1 Row and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())
  
   # Create a label for the icon radio button array
   meshTextBlock = WPFControls.TextBlock()
   meshTextBlock.Text = "Horizontal icon array"
   WPFControls.Grid.SetRow(meshTextBlock, 0)
   WPFControls.Grid.SetColumn(meshTextBlock, 0)

   # Create a horizontal array of three radio buttons
   # Create a horizontal WrapPanel   
   myWrapPanel = WPFControls.WrapPanel()
   myWrapPanel.Orientation=WPFControls.Orientation.Horizontal
   WPFControls.Grid.SetRow(myWrapPanel, 1)
   WPFControls.Grid.SetColumn(myWrapPanel, 0)
   
   # Create a List to hold the radio buttons
   # We'll use this later to make it easier to find which button is checked 
   global icon_radio_buttons
   icon_radio_buttons = [] 

   # Create a global variable to hold the name of the checked radio button
   global checked_button_name

   # First radio button - make this one checked
   icon_radio_buttons.append(WPFControls.RadioButton())
   icon_radio_buttons[-1].IsChecked = System.Nullable[System.Boolean](True)
   icon_radio_buttons[-1].Name = "Mixed"
   # Set checked_button_name to this radio button
   checked_button_name = icon_radio_buttons[-1].Name
   
   # Add a handler for the "Checked" and "Unchecked" events
   icon_radio_buttons[-1].Checked += CheckIconRadioButton
   icon_radio_buttons[-1].Unchecked += UncheckIconRadioButton
   
   # Assign an image to this button
   icon_radio_buttons[-1].Content = os.path.join(os.path.dirname(current_file_path), r"_Tools_Radio Button Icon\resources\mixed.png")
   icon_radio_buttons[-1].SetResourceReference(WPFControls.Control.StyleProperty, "IconOnlyRadioButton")

   # Add it to the WrapPanel     
   myWrapPanel.Children.Add(icon_radio_buttons[-1])
   
   # Second radio button
   icon_radio_buttons.append(WPFControls.RadioButton())
   # Assign a name to the radio button
   icon_radio_buttons[-1].Name = "Quad"

   # Add a handler for the "Checked" and "Unchecked" events
   icon_radio_buttons[-1].Checked += CheckIconRadioButton
   icon_radio_buttons[-1].Unchecked += UncheckIconRadioButton
    
   # Assign an image to this button
   icon_radio_buttons[-1].Content = os.path.join(os.path.dirname(current_file_path), r"_Tools_Radio Button Icon\resources\quad.png")
   icon_radio_buttons[-1].SetResourceReference(WPFControls.Control.StyleProperty, "IconOnlyRadioButton")
   # Make this icon semi-transparent since it is not checked
   icon_radio_buttons[-1].Opacity = 0.5
   # Add it to the WrapPanel        
   myWrapPanel.Children.Add(icon_radio_buttons[-1])
   
   # Third radio button
   icon_radio_buttons.append(WPFControls.RadioButton())
   # Assign a name to the radio button
   icon_radio_buttons[-1].Name = "Tria"
      
   # Add a handler for the "Checked" and "Unchecked" events
   icon_radio_buttons[-1].Checked += CheckIconRadioButton
   icon_radio_buttons[-1].Unchecked += UncheckIconRadioButton

   # Assign an image to this button
   icon_radio_buttons[-1].Content = os.path.join(os.path.dirname(current_file_path), r"_Tools_Radio Button Icon\resources\tria.png")
   icon_radio_buttons[-1].SetResourceReference(WPFControls.Control.StyleProperty, "IconOnlyRadioButton")
   # Make this icon semi-transparent since it is not checked
   icon_radio_buttons[-1].Opacity=0.5
   # Add it to the WrapPanel        
   myWrapPanel.Children.Add(icon_radio_buttons[-1])

   #Add the WrapPanel to the Grid
   my_Grid.Children.Add(meshTextBlock)   
   my_Grid.Children.Add(myWrapPanel)

   # Return the Grid   
   return my_Grid


# Checked handler
# This function is called each time a radio button changes from unchecked to checked
# We'll use this even to change the opacity of the image
@apex_sdk.errorhandler
def CheckIconRadioButton(sender, args):
    sender.Opacity = (1.0)
    checked_button_name = sender.Name


# Unchecked handler
# This function is called each time a radio button changes from checked to unchecked
# We'll use this even to change the opacity of the image
@apex_sdk.errorhandler
def UncheckIconRadioButton(sender, args):
    sender.Opacity = (0.5)


# Apply button handler (Green check mark)
# This function is called each time the Apply button is clicked
@apex_sdk.errorhandler
def HandleApplyButton():
   
   # Create a Dictionary to store the user defined tool data  
   dictionary = {}
 
   # Find the checked button in the radio_button_icons
   # And store it in the dictionary 
   # Note that this is somewhat safe since only one radio button can be checked
   checked_icon_radio_button = [x for x in icon_radio_buttons if x.IsChecked == True][0].Name
   dictionary["checked_radio_button_icon"] = checked_icon_radio_button
   
   # Call the Apex API script using apex_sdk.runScriptFunction()
   # argument 1 = the fully qualified path name of your API script
   # argument 2 is the name of the function in your API script that will receive the Dictionary
   # argument 3 is the Dictionary containing the UI data
   apex_sdk.runScriptFunction(os.path.join(current_file_path, r"radio_button_icon.py"),
                              "read_radio_button_icon",
                              dictionary)
