@using BackOffice.Services.PublicMessage
@using MudBlazor

<MudDialog>
    <TitleContent>
        <MudText Typo="Typo.h6">
            <MudIcon Icon="@GetTypeIcon(Message.Type)" Class="ml-2" />
            @Message.Title
        </MudText>
    </TitleContent>
    <DialogContent>
        <MudGrid>
            <!-- Header با اطلاعات کلی -->
            <MudItem xs="12">
                <MudPaper Class="pa-3" Elevation="1">
                    <MudGrid>
                        <MudItem xs="12" sm="4">
                            <MudChip T="string" Color="@GetTypeColor(Message.Type)" Size="Size.Small">
                                @GetTypeText(Message.Type)
                            </MudChip>
                        </MudItem>
                        <MudItem xs="12" sm="4">
                            <MudChip T="string" Color="@GetStatusColor(Message.Status)" Size="Size.Small">
                                @GetStatusText(Message.Status)
                            </MudChip>
                        </MudItem>
                        <MudItem xs="12" sm="4">
                            <MudRating SelectedValue="@Message.Priority" 
                                       ReadOnly="true" 
                                       MaxValue="5" 
                                       Size="Size.Small" />
                        </MudItem>
                    </MudGrid>
                </MudPaper>
            </MudItem>

            <!-- محتوا -->
            <MudItem xs="12">
                <MudPaper Class="pa-4" Elevation="1">
                    <MudText Typo="Typo.h6" GutterBottom="true">محتوای پیام</MudText>
                    <MudText Typo="Typo.body1" Style="white-space: pre-wrap;">@Message.Content</MudText>
                </MudPaper>
            </MudItem>

            <!-- تگ‌ها -->
            @if (Message.Tags?.Any() == true)
            {
                <MudItem xs="12">
                    <MudPaper Class="pa-3" Elevation="1">
                        <MudText Typo="Typo.subtitle2" GutterBottom="true">تگ‌ها:</MudText>
                        @foreach (var tag in Message.Tags)
                        {
                            <MudChip T="string" Size="Size.Small" Color="Color.Default">@tag</MudChip>
                        }
                    </MudPaper>
                </MudItem>
            }

            <!-- اطلاعات اضافی -->
            <MudItem xs="12">
                <MudPaper Class="pa-3" Elevation="1">
                    <MudGrid>
                        @if (!string.IsNullOrEmpty(Message.TargetAudience))
                        {
                            <MudItem xs="12" sm="6">
                                <MudText Typo="Typo.body2" Color="Color.Secondary">مخاطب هدف:</MudText>
                                <MudText Typo="Typo.body1">@Message.TargetAudience</MudText>
                            </MudItem>
                        }
                        <MudItem xs="12" sm="6">
                            <MudText Typo="Typo.body2" Color="Color.Secondary">قابل رد کردن:</MudText>
                            <MudText Typo="Typo.body1">@(Message.IsDismissible ? "بله" : "خیر")</MudText>
                        </MudItem>
                    </MudGrid>
                </MudPaper>
            </MudItem>

            <!-- اطلاعات تاریخ -->
            <MudItem xs="12">
                <MudPaper Class="pa-3" Elevation="1">
                    <MudGrid>
                        <MudItem xs="12" sm="3">
                            <MudText Typo="Typo.body2" Color="Color.Secondary">تاریخ ایجاد:</MudText>
                            <MudText Typo="Typo.body1">@(Message.Created?.MiladiToJalaliWithTime() ?? "-")</MudText>
                        </MudItem>
                        @if (Message.StartsAt.HasValue)
                        {
                            <MudItem xs="12" sm="3">
                                <MudText Typo="Typo.body2" Color="Color.Secondary">شروع نمایش:</MudText>
                                <MudText Typo="Typo.body1">@Message.StartsAt.Value.MiladiToJalaliWithTime()</MudText>
                            </MudItem>
                        }
                        @if (Message.PublishedAt.HasValue)
                        {
                            <MudItem xs="12" sm="3">
                                <MudText Typo="Typo.body2" Color="Color.Secondary">تاریخ انتشار:</MudText>
                                <MudText Typo="Typo.body1">@Message.PublishedAt.Value.MiladiToJalaliWithTime()</MudText>
                            </MudItem>
                        }
                        @if (Message.ExpiresAt.HasValue)
                        {
                            <MudItem xs="12" sm="3">
                                <MudText Typo="Typo.body2" Color="Color.Secondary">تاریخ انقضا:</MudText>
                                <MudText Typo="Typo.body1" Color="@(Message.ExpiresAt.Value < DateTime.Now ? Color.Error : Color.Default)">
                                    @Message.ExpiresAt.Value.MiladiToJalali()
                                    @if (Message.ExpiresAt.Value < DateTime.Now)
                                    {
                                        <MudChip T="string" Size="Size.Small" Color="Color.Error">منقضی شده</MudChip>
                                    }
                                </MudText>
                            </MudItem>
                        }
                    </MudGrid>
                </MudPaper>
            </MudItem>
        </MudGrid>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Close" Variant="Variant.Filled" Color="Color.Primary">بستن</MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = null!;
    [Parameter] public PublicMessageDetailsDto Message { get; set; } = null!;

    private void Close()
    {
        MudDialog.Close();
    }

    private Color GetTypeColor(MessageType type)
    {
        return type switch
        {
            MessageType.Announcement => Color.Primary,
            MessageType.News => Color.Info,
            MessageType.Alert => Color.Warning,
            MessageType.Promotion => Color.Success,
            _ => Color.Default
        };
    }

    private string GetTypeText(MessageType type)
    {
        return type switch
        {
            MessageType.Announcement => "اطلاعیه",
            MessageType.News => "خبر",
            MessageType.Alert => "هشدار",
            MessageType.Promotion => "تبلیغات",
            _ => "نامشخص"
        };
    }

    private string GetTypeIcon(MessageType type)
    {
        return type switch
        {
            MessageType.Announcement => Icons.Material.Filled.Announcement,
            MessageType.News => Icons.Material.Filled.Newspaper,
            MessageType.Alert => Icons.Material.Filled.Warning,
            MessageType.Promotion => Icons.Material.Filled.LocalOffer,
            _ => Icons.Material.Filled.Message
        };
    }

    private Color GetStatusColor(MessageStatus status)
    {
        return status switch
        {
            MessageStatus.Draft => Color.Default,
            MessageStatus.Published => Color.Success,
            MessageStatus.Archived => Color.Dark,
            _ => Color.Default
        };
    }

    private string GetStatusText(MessageStatus status)
    {
        return status switch
        {
            MessageStatus.Draft => "پیش‌نویس",
            MessageStatus.Published => "منتشر شده",
            MessageStatus.Archived => "بایگانی شده",
            _ => "نامشخص"
        };
    }
}
