BCards/src/BCards.Web/Services/ThemeService.cs
Ricardo Carneiro 27ae8b606e feat:
+login ms que permite contas corporativas ou não.
+Links para produtos de afiliados
2025-06-25 19:30:19 -03:00

338 lines
11 KiB
C#

using BCards.Web.Models;
using MongoDB.Driver;
using System.Text;
namespace BCards.Web.Services;
public class ThemeService : IThemeService
{
private readonly IMongoCollection<PageTheme> _themes;
public ThemeService(IMongoDatabase database)
{
_themes = database.GetCollection<PageTheme>("themes");
}
public async Task<List<PageTheme>> GetAvailableThemesAsync()
{
return await _themes.Find(x => x.IsActive).ToListAsync();
}
public async Task<PageTheme?> GetThemeByIdAsync(string themeId)
{
return await _themes.Find(x => x.Id == themeId && x.IsActive).FirstOrDefaultAsync();
}
public async Task<PageTheme?> GetThemeByNameAsync(string themeName)
{
var theme = await _themes.Find(x => x.Name.ToLower() == themeName.ToLower() && x.IsActive).FirstOrDefaultAsync();
return theme ?? GetDefaultTheme();
}
public Task<string> GenerateCustomCssAsync(PageTheme theme)
{
var css = $@"
:root {{
--primary-color: {theme.PrimaryColor};
--secondary-color: {theme.SecondaryColor};
--background-color: {theme.BackgroundColor};
--text-color: {theme.TextColor};
}}
.user-page {{
background-color: var(--background-color);
color: var(--text-color);
{(!string.IsNullOrEmpty(theme.BackgroundImage) ? $"background-image: url('{theme.BackgroundImage}');" : "")}
background-size: cover;
background-position: center;
background-attachment: fixed;
}}
.profile-card {{
background-color: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 2rem;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}}
.profile-image {{
width: 120px;
height: 120px;
border-radius: 50%;
border: 4px solid var(--primary-color);
object-fit: cover;
}}
.profile-name {{
color: var(--primary-color);
font-size: 2rem;
font-weight: 600;
margin-bottom: 0.5rem;
}}
.profile-bio {{
color: var(--text-color);
opacity: 0.8;
margin-bottom: 2rem;
}}
.link-button {{
background-color: var(--primary-color);
color: white;
border: none;
padding: 1rem 2rem;
border-radius: 50px;
text-decoration: none;
display: block;
margin-bottom: 1rem;
text-align: center;
font-weight: 500;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}}
.link-button:hover {{
background-color: var(--secondary-color);
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
color: white;
text-decoration: none;
}}
.link-title {{
font-size: 1.1rem;
margin-bottom: 0.25rem;
}}
.link-description {{
font-size: 0.9rem;
opacity: 0.9;
}}
@media (max-width: 768px) {{
.profile-card {{
padding: 1.5rem;
margin: 1rem;
}}
.profile-image {{
width: 100px;
height: 100px;
}}
.profile-name {{
font-size: 1.75rem;
}}
.link-button {{
padding: 0.875rem 1.5rem;
}}
}}
";
return Task.FromResult(css);
}
public async Task<string> GenerateThemeCSSAsync(PageTheme theme, UserPage page)
{
var css = new StringBuilder();
// CSS base com variáveis do tema
css.AppendLine($":root {{");
css.AppendLine($" --primary-color: {theme.PrimaryColor};");
css.AppendLine($" --secondary-color: {theme.SecondaryColor};");
css.AppendLine($" --background-color: {theme.BackgroundColor};");
css.AppendLine($" --text-color: {theme.TextColor};");
css.AppendLine($"}}");
// CSS específico por tema
switch (theme.Name?.ToLower())
{
case "minimalista":
css.AppendLine(GetMinimalistCSS());
break;
case "corporativo":
css.AppendLine(GetCorporateCSS());
break;
case "dark mode":
css.AppendLine(GetDarkCSS());
break;
case "natureza":
css.AppendLine(GetNatureCSS());
break;
case "vibrante":
css.AppendLine(GetVibrantCSS());
break;
default:
css.AppendLine(await GenerateCustomCssAsync(theme));
break;
}
return css.ToString();
}
private string GetMinimalistCSS() => @"
.profile-card {
background: white;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
border-radius: 12px;
}
.link-button {
background: var(--primary-color);
border-radius: 8px;
}
";
private string GetCorporateCSS() => @"
.user-page {
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
}
.profile-card {
background: white;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
border: 1px solid #e2e8f0;
}
.link-button {
background: var(--primary-color);
border-radius: 6px;
font-weight: 600;
}
";
private string GetDarkCSS() => @"
.user-page {
background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
}
.profile-card {
background: rgba(255,255,255,0.1);
backdrop-filter: blur(15px);
border: 1px solid rgba(255,255,255,0.2);
color: #f9fafb;
}
.link-button {
background: var(--primary-color);
box-shadow: 0 4px 15px rgba(0,0,0,0.3);
}
.profile-name, .profile-bio {
color: #f9fafb;
}
";
private string GetNatureCSS() => @"
.user-page {
background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
background-image: url('data:image/svg+xml,<svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 100 100""><defs><pattern id=""grain"" width=""100"" height=""100"" patternUnits=""userSpaceOnUse""><circle cx=""25"" cy=""25"" r=""1"" fill=""%23059669"" opacity=""0.1""/><circle cx=""75"" cy=""75"" r=""1"" fill=""%23059669"" opacity=""0.1""/></pattern></defs><rect width=""100"" height=""100"" fill=""url(%23grain)""/></svg>');
}
.profile-card {
background: rgba(255,255,255,0.9);
backdrop-filter: blur(10px);
border: 1px solid rgba(34, 197, 94, 0.2);
}
.link-button {
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
border-radius: 25px;
}
";
private string GetVibrantCSS() => @"
.user-page {
background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 50%, #fecaca 100%);
}
.profile-card {
background: rgba(255,255,255,0.95);
box-shadow: 0 8px 32px rgba(220, 38, 38, 0.2);
border: 2px solid rgba(220, 38, 38, 0.1);
}
.link-button {
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
border-radius: 30px;
transform: perspective(1000px) rotateX(0deg);
transition: all 0.3s ease;
}
.link-button:hover {
transform: perspective(1000px) rotateX(-5deg) translateY(-5px);
box-shadow: 0 15px 30px rgba(220, 38, 38, 0.3);
}
";
public async Task InitializeDefaultThemesAsync()
{
var existingThemes = await _themes.Find(x => x.IsActive).ToListAsync();
if (existingThemes.Any()) return;
var defaultThemes = new[]
{
new PageTheme
{
Name = "Minimalista",
PrimaryColor = "#2563eb",
SecondaryColor = "#1d4ed8",
BackgroundColor = "#ffffff",
TextColor = "#1f2937",
IsPremium = false,
CssTemplate = "minimal"
},
new PageTheme
{
Name = "Dark Mode",
PrimaryColor = "#10b981",
SecondaryColor = "#059669",
BackgroundColor = "#111827",
TextColor = "#f9fafb",
IsPremium = false,
CssTemplate = "dark"
},
new PageTheme
{
Name = "Natureza",
PrimaryColor = "#16a34a",
SecondaryColor = "#15803d",
BackgroundColor = "#f0fdf4",
TextColor = "#166534",
BackgroundImage = "/images/themes/nature-bg.jpg",
IsPremium = false,
CssTemplate = "nature"
},
new PageTheme
{
Name = "Corporativo",
PrimaryColor = "#1e40af",
SecondaryColor = "#1e3a8a",
BackgroundColor = "#f8fafc",
TextColor = "#0f172a",
IsPremium = false,
CssTemplate = "corporate"
},
new PageTheme
{
Name = "Vibrante",
PrimaryColor = "#dc2626",
SecondaryColor = "#b91c1c",
BackgroundColor = "#fef2f2",
TextColor = "#7f1d1d",
IsPremium = true,
CssTemplate = "vibrant"
}
};
foreach (var theme in defaultThemes)
{
await _themes.InsertOneAsync(theme);
}
}
public PageTheme GetDefaultTheme()
{
return new PageTheme
{
Name = "Padrão",
PrimaryColor = "#2563eb",
SecondaryColor = "#1d4ed8",
BackgroundColor = "#ffffff",
TextColor = "#1f2937",
IsPremium = false,
CssTemplate = "default"
};
}
}