@page "/settings"
@using TradingBot.Services
@using TradingBot.Models
@inject SettingsService SettingsService
@implements IDisposable
@rendermode InteractiveServer

<PageTitle>Impostazioni - TradingBot</PageTitle>

<div class="settings-page">
    <div class="page-header">
        <h1>Impostazioni</h1>
        <p class="subtitle">Configura le impostazioni globali del trading bot</p>
    </div>

    <div class="settings-section">
        <h2>Generale</h2>
        <div class="settings-group">
            <div class="setting-item">
                <div class="setting-info">
                    <div class="setting-label">Modalità Simulazione</div>
                    <div class="setting-description">Utilizza dati simulati invece di dati reali di mercato</div>
                </div>
                <label class="toggle-switch">
                    <input type="checkbox" checked="@settings.SimulationMode" @onchange="(e) => UpdateSetting(nameof(AppSettings.SimulationMode), (bool)e.Value!)" disabled />
                    <span class="toggle-slider"></span>
                </label>
            </div>
            
            <div class="setting-item">
                <div class="setting-info">
                    <div class="setting-label">Notifiche Desktop</div>
                    <div class="setting-description">Ricevi notifiche per operazioni importanti</div>
                </div>
                <label class="toggle-switch">
                    <input type="checkbox" checked="@settings.DesktopNotifications" @onchange="(e) => UpdateSetting(nameof(AppSettings.DesktopNotifications), (bool)e.Value!)" />
                    <span class="toggle-slider"></span>
                </label>
            </div>
        </div>
    </div>

    <div class="settings-section">
        <h2>Trading</h2>
        <div class="settings-group">
            <div class="setting-item">
                <div class="setting-info">
                    <div class="setting-label">Auto-Start Bot</div>
                    <div class="setting-description">Avvia automaticamente il bot all'apertura dell'applicazione</div>
                </div>
                <label class="toggle-switch">
                    <input type="checkbox" checked="@settings.AutoStartBot" @onchange="(e) => UpdateSetting(nameof(AppSettings.AutoStartBot), (bool)e.Value!)" />
                    <span class="toggle-slider"></span>
                </label>
            </div>
            
            <div class="setting-item">
                <div class="setting-info">
                    <div class="setting-label">Conferma Operazioni Manuali</div>
                    <div class="setting-description">Richiedi conferma prima di eseguire operazioni manuali</div>
                </div>
                <label class="toggle-switch">
                    <input type="checkbox" checked="@settings.ConfirmManualTrades" @onchange="(e) => UpdateSetting(nameof(AppSettings.ConfirmManualTrades), (bool)e.Value!)" />
                    <span class="toggle-slider"></span>
                </label>
            </div>
        </div>
    </div>

    <div class="settings-section">
        <h2>Avanzate</h2>
        <div class="settings-group">
            <div class="setting-item">
                <div class="setting-info">
                    <div class="setting-label">Intervallo Aggiornamento</div>
                    <div class="setting-description">Frequenza di aggiornamento dei dati di mercato</div>
                </div>
                <select class="setting-select" value="@settings.UpdateIntervalSeconds" @onchange="(e) => UpdateSetting(nameof(AppSettings.UpdateIntervalSeconds), int.Parse(e.Value!.ToString()!))">
                    <option value="2">2 secondi</option>
                    <option value="3">3 secondi</option>
                    <option value="5">5 secondi</option>
                    <option value="10">10 secondi</option>
                </select>
            </div>
            
            <div class="setting-item">
                <div class="setting-info">
                    <div class="setting-label">Log Level</div>
                    <div class="setting-description">Livello di dettaglio dei log di sistema</div>
                </div>
                <select class="setting-select" value="@settings.LogLevel" @onchange="(e) => UpdateSetting(nameof(AppSettings.LogLevel), e.Value!.ToString()!)">
                    <option value="Error">Error</option>
                    <option value="Warning">Warning</option>
                    <option value="Info">Info</option>
                    <option value="Debug">Debug</option>
                </select>
            </div>
        </div>
    </div>

    <div class="settings-actions">
        <button class="btn-secondary" @onclick="ResetToDefaults">
            <span class="bi bi-arrow-counterclockwise"></span>
            Reset Predefiniti
        </button>
        <button class="btn-primary" @onclick="SaveSettings">
            <span class="bi bi-check-lg"></span>
            Salva Modifiche
        </button>
    </div>

    @if (showNotification)
    {
        <div class="notification success">
            <span class="bi bi-check-circle-fill"></span>
            Impostazioni salvate con successo!
        </div>
    }
</div>

@code {
    private AppSettings settings = new();
    private bool showNotification = false;

    protected override void OnInitialized()
    {
        settings = SettingsService.GetSettings();
        SettingsService.OnSettingsChanged += HandleSettingsChanged;
    }

    private void UpdateSetting<T>(string propertyName, T value)
    {
        SettingsService.UpdateSetting(propertyName, value);
        settings = SettingsService.GetSettings();
        ShowNotification();
    }

    private void SaveSettings()
    {
        SettingsService.UpdateSettings(settings);
        ShowNotification();
    }

    private void ResetToDefaults()
    {
        SettingsService.ResetToDefaults();
        settings = SettingsService.GetSettings();
        ShowNotification();
    }

    private async void ShowNotification()
    {
        showNotification = true;
        StateHasChanged();
        await Task.Delay(3000);
        showNotification = false;
        StateHasChanged();
    }

    private void HandleSettingsChanged()
    {
        settings = SettingsService.GetSettings();
        InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        SettingsService.OnSettingsChanged -= HandleSettingsChanged;
    }
}
