#
# 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.
#
import cfutils

import os

import System
import Microsoft.Win32 as Win32


class CfAppTemplate:
    name = ""
    path = ""


class CfAppVersion:
    name = ""
    title = ""
    path = ""

    templates = []


def getRegistryValue2(path, name):
    value = None
    
    try:
        key = Win32.RegistryKey.OpenBaseKey(Win32.RegistryHive.LocalMachine, Win32.RegistryView.Registry32)

        currentVersionKey = key.OpenSubKey(path)
        if currentVersionKey is not None:
            value = currentVersionKey.GetValue(name)              
            currentVersionKey.Dispose()
            
        key.Dispose()
    except:
        pass

    return value


def getCAEfatigueVersions():

    versions = []

    try:
        key = Win32.RegistryKey.OpenBaseKey(Win32.RegistryHive.LocalMachine, Win32.RegistryView.Registry32)
        if key is not None:
            appKey = key.OpenSubKey("Software\\MSC.Software Corporation\\CAEfatigue")

            if appKey is not None:

                names = appKey.GetSubKeyNames()

                for name in names:
                    if name != "Latest":
                        app = CfAppVersion()
                        app.name = name 

                        pathKey = appKey.OpenSubKey(name)
                        if pathKey is not None:
                        
                            app.path = pathKey.GetValue("path")
                            app.title = pathKey.GetValue("ProductTitle")

                            versions.append(app)

                            app.templates = []

                            # Find templates
                            templateFiles = System.IO.Directory.GetFiles(app.path + os.sep + "Windows" + os.sep + "Resources" + os.sep + "DataTemplates")
                            for file in templateFiles:
                                t = CfAppTemplate()
                                t.name = System.IO.Path.GetFileNameWithoutExtension(file)
                                t.path = file

                                app.templates.append(t)

                            pathKey.Dispose()

                appKey.Dispose()
        
            key.Dispose()

    except System.Exception as e:
        pass
    except:
        pass

    return versions
