<#
# This script can be used to update the *.Designer file if a *.resx ressource file has been updated.
# However, it is recommended to use Visual Studio instead for editing ressources instead since it takes of that automatically and prodcues cleaner diffs.
#>
param(
    [ValidateSet("Engine","Rules")]
    [string] $project
)

function Get-StronglyTypeCsFileForResx
{
    param($xml, $ModuleName, $ClassName)

    # Example
    #
    # $ClassName = Full.Name.Of.The.ClassFoo
    # $shortClassName = ClassFoo
    # $namespaceName = Full.Name.Of.The

    $shortClassName = $ClassName
    $namespaceName = $null

    $lastIndexOfDot = $className.LastIndexOf(".")
    if ($lastIndexOfDot -ne -1)
    {
        $namespaceName = $className.Substring(0, $lastIndexOfDot)
        $shortClassName = $className.Substring($lastIndexOfDot + 1)
    }

$banner = @'
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a New-StronglyTypedCsFileForResx function.
//     To add or remove a member, edit your .ResX file then rerun Start-ResGen.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

{0}
'@

$namespace = @'
namespace {0} {{
{1}
}}
'@

$body = @'
using System;
using System.Reflection;

/// <summary>
///   A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]

internal class {0} {{

    private static global::System.Resources.ResourceManager resourceMan;

    private static global::System.Globalization.CultureInfo resourceCulture;

    [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
    internal {0}() {{
    }}

    /// <summary>
    ///   Returns the cached ResourceManager instance used by this class.
    /// </summary>
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    internal static global::System.Resources.ResourceManager ResourceManager {{
        get {{
            if (object.ReferenceEquals(resourceMan, null)) {{
                global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("{3}", typeof({0}).GetTypeInfo().Assembly);
                resourceMan = temp;
            }}
            return resourceMan;
        }}
    }}

    /// <summary>
    ///   Overrides the current thread's CurrentUICulture property for all
    ///   resource lookups using this strongly typed resource class.
    /// </summary>
    [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
    internal static global::System.Globalization.CultureInfo Culture {{
        get {{
            return resourceCulture;
        }}
        set {{
            resourceCulture = value;
        }}
    }}
    {2}
}}
'@

    $entry = @'

    /// <summary>
    ///   Looks up a localized string similar to {1}
    /// </summary>
    internal static string {0} {{
        get {{
            return ResourceManager.GetString("{0}", resourceCulture);
        }}
    }}
'@
    $entries = $xml.root.data | ForEach-Object {
        if ($_) {
            $val = $_.value.Replace("`n", "`n    ///")
            $name = $_.name.Replace(' ', '_')
            $entry -f $name,$val
        }
    } | Out-String

    $bodyCode = $body -f $shortClassName,$ModuleName,$entries,$ClassName

    if ($NamespaceName)
    {
        $bodyCode = $namespace -f $NamespaceName, $bodyCode
    }

    $resultCode = $banner -f $bodyCode

    return $resultCode -replace "`r`n?|`n","`r`n"
}

$projectRoot = $PWD
if (-not (Test-Path "$projectRoot/global.json"))
{
    throw "Not in solution root: $projectRoot"
}
$inputFilePath = Join-Path $projectRoot "$project/Strings.resx"
$outputFilePath = Join-Path $projectRoot "$project/Strings.Designer.cs"
$className = "Microsoft.Windows.PowerShell.ScriptAnalyzer"
if ($project -eq "Rules")
{
    $className += ".BuiltinRules"
}
$className += ".Strings"
$xml = [xml](Get-Content -raw $inputFilePath)
$genSource = Get-StronglyTypeCsFileForResx -xml $xml -ModuleName Foo -ClassName $className
Set-Content -Encoding Ascii -Path $outputFilePath -Value $genSource
