# ========================================================
# List all found WebIQ Designer workspaces with their details.
# This script does NOT perform any installation or uninstallation.
#
# 2026.02-02 - Adapted to changed workspace folder names
# 2023-01-17 - Added folder name to install request
# 2023-01-13 - First release
# 2024-01-26 - Converted from VBS to PowerShell (list.ps1).
#              Uses external tools.ps1 for utility functions.
# ========================================================

# --- Global Variables for the Main Script ---
# These variables will be globally available after tools.ps1 is imported.
# (e.g., $global:objFSO, $global:wshShell, $global:appData, $global:NOT_INSTALLED)

# Configuration strings/constants.
[string]$sAppInfo = "\.designer\app-info.json"  # Path to app-info.json relative to project root.
[string]$sSrcDir = "addon"                      # Source directory name for the addon (used for finding addon version).
# $global:NOT_INSTALLED will be available after tools.ps1 is imported.

# Project search and selection variables.
[System.Collections.ArrayList]$designerFilesArray = @() # PowerShell ArrayList for project paths.
[int]$iProjects = 0                                     # Total number of found WebIQ projects.
[int]$iProject = 0                                      # Loop counter for iterating through projects.

# Local variables for main script logic.
[string]$sDirCur           # Current script's directory.
[string]$sInstalled        # Status message for displaying info.
[string]$objPath           # Current project path in loop.
[string]$sTarget           # Full path to the WebIQ project folder.
[string]$sDir              # Full path to the addon folder within a project.
[array]$arWords            # Array of path segments.
[string]$sFile             # Path to app-info.json for the current project.
[string]$sFileWebIq        # Path to webiq.json for the current project.
[string]$sAddOnVersion     # Version of the AddOn for the current project.

Clear-Host
# --- Determine Script's Directory ---
# $PSScriptRoot is a built-in PowerShell variable for the script's directory.
$sDirCur = $PSScriptRoot + "\"

# --- Import Tools.ps1 ---
# This makes all functions and global variables from tools.ps1 available.
. (Join-Path $sDirCur "tools.ps1")

# --- Configuration settings ---
# $global:sAppInfo and $global:sSrcDir are already defined as global variables in tools.ps1 if needed there.
# If these values are only used in this script, they don't need to be global.
# Here, we pass them as parameters where necessary.

# printLN "Searching for WebIQ workspaces and gathering information..." -Color Cyan
# printLN "---------------------------------------------------------" -Color DarkGray

# --- Search for WebIQ Workspaces ---
# $designerFilesArray is defined as ArrayList to allow dynamic adding.
# Pass sAppInfo by value, $designerFilesArray by reference (PowerShell handles this for objects).
getWebIQWorkspaces -sAppInfo $sAppInfo -designerFilesArray $designerFilesArray
$iProjects = $designerFilesArray.Count # Get the count of populated projects.

If ($iProjects -eq 0) {
    $sInstalled = "No WebIQ projects found in workspace.`n" + `
                  "Please load a project in WebIQ Designer first."
    print $sInstalled -Color Yellow
    quitPrg
} Else {
    # In PowerShell, ArrayLists manage their size automatically, no need for ReDim Preserve or resizing.
    printLN "Found $($iProjects) WebIQ projects:" -Color Green

    $iProject = 0 # Reset project counter for looping.
    ForEach ($objPath in $designerFilesArray) {
        $sTarget = $objPath + "\" # Full path to the WebIQ project folder.
        $sDir = Join-Path $sTarget "addon" # Path to the potential addon folder. (Using literal "addon" here)
        $arWords = $sTarget.Split("\") # Split path for display.
        $sFile = Join-Path $sTarget $sAppInfo # Path to app-info.json for the current project.

        If (Test-Path $sFile -PathType Leaf) { # Double-check app-info.json existence.
            $iProject++
            $sFileWebIq = Join-Path $sTarget "webiq.json" # Path to webiq.json for project name.
            $sAddOnVersion = getAddonVersion -sDir $sDir # Get AddOn version for this project (from tools.ps1).

            printLN "---------------------------------------------------------" -Color DarkGray
            # HIER WURDE DIE KORREKTUR VORGENOMMEN: ${iProject}
            printLN "Project #${iProject}:" -Color White
            printLN "  Name:      $(getProjectName -sFile $sFileWebIq)" -Color Gray
            printLN "  Folder:    $(getProjectFolder -sFile $sFile)" -Color Gray
            printLN "  Workspace: $($arWords[-2])" -Color Gray
            if ($sAddOnVersion -eq $NOT_INSTALLED) {
              printLN "  AddOn:     $sAddOnVersion" -Color Red
            } else {
              printLN "  AddOn:     $sAddOnVersion" -Color Green
            }
        } Else {
            printLN "---------------------------------------------------------" -Color DarkGray
            printLN "Project #$($iProject + 1): WARNING - Invalid entry found at $objPath (missing $sAppInfo)." -Color Yellow
        }
    }
}

printLN "---------------------------------------------------------" -Color DarkGray
quitPrg # Exit cleanly (from tools.ps1).