@namespace Oqtane.Installer.Controls

@implements Oqtane.Interfaces.IDatabaseConfigControl

@inject IStringLocalizer<Installer> Localizer

@{
    foreach (var field in _connectionStringFields)
    {
        var fieldId = field.Name.ToLowerInvariant();
        if (field.Name != "IntegratedSecurity")
        {
            var isVisible = "";
            var fieldType = (field.Name == "Pwd") ? "password" : "text";
            if ((field.Name == "Uid" || field.Name == "Pwd") )
            {
                var intSecurityField = _connectionStringFields.Single(f => f.Name == "IntegratedSecurity");
                if (intSecurityField != null)
                {
                    isVisible = (Convert.ToBoolean(intSecurityField.Value)) ? "display: none;" : "";
                }
            }

            field.Value = field.Value.Replace("{{Date}}", DateTime.UtcNow.ToString("yyyyMMddHHmm"));
            
            if (IsInstaller)
            {
                <tr style="@isVisible">
                    <td>
                        <label class="control-label" style="font-weight: bold">@Localizer[$"{field.FriendlyName}:"]</label>
                    </td>
                    <td>
                        <input type="@fieldType" class="form-control" @bind="@field.Value" />
                    </td>
                </tr>
            }
            else
            {
                <tr style="@isVisible">
                    <td>
                        <Label For="@fieldId" HelpText="@field.HelpText" ResourceKey="@field.Name">@Localizer[$"{field.FriendlyName}:"]</Label>
                    </td>
                    <td>
                        <input id="@fieldId" type="@fieldType" class="form-control" @bind="@field.Value" />
                    </td>
                </tr>
            }        
        }
        else
        {
            if (IsInstaller)
            {
                <tr>
                    <td>
                        <label class="control-label" style="font-weight: bold">@Localizer[$"{field.FriendlyName}:"]</label>
                    </td>
                    <td>
                        <select class="custom-select" @bind="@field.Value">
                            <option value="true" selected>@Localizer["True"]</option>
                            <option value="false">@Localizer["False"]</option>
                        </select>
                    </td>
                </tr>
            }
            else
            {
                <tr>
                    <td>
                        <Label For="@fieldId" HelpText="@field.HelpText" ResourceKey="@field.Name">@Localizer[$"{field.FriendlyName}:"]</Label>
                    </td>
                    <td>
                        <select id="@fieldId" class="custom-select" @bind="@field.Value">
                            <option value="true" selected>@Localizer["True"]</option>
                            <option value="false">@Localizer["False"]</option>
                        </select>
                    </td>
                </tr>
            }
        }
    }
}

@code {
    
    [Parameter]
    public bool IsInstaller { get; set; }

    private readonly List<ConnectionStringField> _connectionStringFields = new()
    {
        new() {Name = "Server", FriendlyName = "Server", Value = ".", HelpText="Enter the database server"},
        new() {Name = "Database", FriendlyName = "Database", Value = "Oqtane-{{Date}}", HelpText="Enter the name of the database"},
        new() {Name = "IntegratedSecurity", FriendlyName = "Integrated Security", Value = "true", HelpText="Select if you want integrated security or not"},
        new() {Name = "Uid", FriendlyName = "User Id", Value = "", HelpText="Enter the username to use for the database"},
        new() {Name = "Pwd", FriendlyName = "Password", Value = "", HelpText="Enter the password to use for the database"}
    };

    public string GetConnectionString()
    {
        var connectionString = String.Empty;

        var server = _connectionStringFields[0].Value;
        var database = _connectionStringFields[1].Value;
        var integratedSecurity = Boolean.Parse(_connectionStringFields[2].Value);
        var userId = _connectionStringFields[3].Value;
        var password = _connectionStringFields[4].Value;

        if (!String.IsNullOrEmpty(server)  && !String.IsNullOrEmpty(database))
        {
            connectionString = $"Data Source={server};Initial Catalog={database};";
        }

        if (integratedSecurity)
        {
            connectionString += "Integrated Security=SSPI;";
        }
        else
        {
            if (!String.IsNullOrEmpty(userId) && !String.IsNullOrEmpty(password))
            {
                connectionString += $"User ID={userId};Password={password};";
            }
            else
            {
                connectionString = String.Empty;
            }
        }

        return connectionString;
    }

}