// --------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
// --------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Microsoft.Oryx.SharedCodeGenerator.Outputs
{
    [OutputType("powershell")]
    internal class PowershellOutput : IOutputFile
    {
        private ConstantCollection collection;
        private string directory;
        private string fileNamePrefix;

        public void Initialize(ConstantCollection constantCollection, Dictionary<string, string> typeInfo)
        {
            this.collection = constantCollection;
            this.directory = typeInfo["directory"];
            this.fileNamePrefix = typeInfo["file-name-prefix"];
        }

        public string GetPath()
        {
            var name = this.collection.Name.Camelize();
            name = char.ToLowerInvariant(name[0]) + name.Substring(1);
            return Path.Combine(this.directory, this.fileNamePrefix + name + ".ps1");
        }

        public string GetContent()
        {
            StringBuilder body = new StringBuilder();
            var autoGeneratedMessage = Program.BuildAutogenDisclaimer(this.collection.SourcePath);
            body.AppendLine($"# {autoGeneratedMessage}");
            body.AppendLine();
            if (this.collection.StringConstants?.Any() ?? false)
            {
                foreach (var constant in this.collection.StringConstants)
                {
                    var name = constant.Key.Replace(ConstantCollection.NameSeparator[0], '_').ToUpper();
                    var value = constant.Value.WrapValueInQuotes();
                    body.AppendLine($"${name}={value}");
                }
            }

            return body.ToString();
        }
    }
}
