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

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

namespace Microsoft.Oryx.SharedCodeGenerator.Outputs
{
    [OutputType("msbuild")]
    internal class MSBuildOutput : 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 + ".props");
        }

        public string GetContent()
        {
            StringBuilder body = new StringBuilder();
            var autoGeneratedMessage = Program.BuildAutogenDisclaimer(this.collection.SourcePath);
            body.AppendLine($"<!-- {autoGeneratedMessage} -->"); // Can't use AppendLine becuase it appends \r\n
            body.AppendLine();
            body.AppendLine("<Project>");
            body.AppendLine("\t<PropertyGroup>");
            foreach (var constant in this.collection.Constants)
            {
                var name = constant.Key.Replace(ConstantCollection.NameSeparator[0], '_').ToUpper();
                body.AppendLine($"\t\t<{name}>{constant.Value}</{name}>");
            }

            body.AppendLine("\t</PropertyGroup>");
            body.AppendLine("</Project>");
            return body.ToString();
        }
    }
}
