using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CTA.WebForms.FileInformationModel;
namespace CTA.WebForms.Services
{
public class HostPageService
{
private const string FileName = "_Host.cshtml";
private const string HostTemplate =
@"@page ""/""
@namespace {0}
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
{1}
{2}
@(await Html.RenderComponentAsync(RenderMode.ServerPrerendered))
";
private const string StyleSheetTemplate = " ";
public string Title { get; set; } = "Unknown_app_title";
public string HostNamespace { get; set; } = "Unknown_host_namespace";
private IEnumerable _styleSheetPaths;
public HostPageService()
{
_styleSheetPaths = new List();
}
public void AddStyleSheetPath(string path)
{
_styleSheetPaths = _styleSheetPaths.Append(path);
}
public FileInformation ConstructHostPageFile()
{
var relativePath = Path.Combine(Constants.RazorPageDirectoryName, FileName);
var styleSheetsString = ConstructStyleSheetsString();
var fileContents = string.Format(HostTemplate, HostNamespace, Title, styleSheetsString);
var fileBytes = Encoding.UTF8.GetBytes(fileContents);
return new FileInformation(relativePath, fileBytes);
}
private string ConstructStyleSheetsString()
{
var styleSheetLines = _styleSheetPaths.Select(path => string.Format(StyleSheetTemplate, path));
return string.Join(Environment.NewLine, styleSheetLines);
}
}
}