using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
using FrontOffice.Main.Utilities;

namespace FrontOffice.Main.Pages.Profile.Components;

/// <summary>
/// DEPRECATED: This component is no longer used. 
/// d3-org-chart is now used for tree visualization.
/// Kept for backward compatibility.
/// </summary>
public partial class OrganizationChartLevel
{
    [Parameter] public List<UserNode>? Nodes { get; set; }
    [Parameter] public int Level { get; set; } = 1;

    [CascadingParameter] private OrganizationChart? ParentChart { get; set; }

    [Inject] private AuthDialogService AuthDialogService { get; set; } = default!;

    private Size GetAvatarSize(int level)
    {
        return level switch
        {
            1 => Size.Large,
            2 => Size.Medium,
            _ => Size.Small
        };
    }

    private void ToggleNodeExpand(long userId)
    {
        // No longer needed - d3-org-chart handles this internally
    }

    private void SafeToggleNodeExpand(long userId)
    {
        // No longer needed - d3-org-chart handles this internally
    }

    private async Task CopyReferralCode(string referralCode)
    {
        try
        {
            await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", $"{Navigation.BaseUri}?ref={referralCode}");
            Snackbar.Add("کد دعوت کپی شد!", Severity.Success);
        }
        catch (Exception ex)
        {
            Snackbar.Add("خطا در کپی کردن کد دعوت.", Severity.Error);
        }
    }

    private async Task ShareReferralCode(string referralCode)
    {
        var shareText = $"کد دعوت من: {referralCode}";

        try
        {
            // Try to use Web Share API if available
            await JSRuntime.InvokeVoidAsync("navigator.share",
                new
                {
                    title = "کد دعوت",
                    text = shareText,
                    url = $"{Navigation.BaseUri}?ref={referralCode}"
                });
        }
        catch
        {
            // Fallback: copy to clipboard
            await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", $"{Navigation.BaseUri}?ref={referralCode}");
            Snackbar.Add("لینک دعوت در کلیپ‌بورد کپی شد.", Severity.Success);
        }
    }

    public class UserNode
    {
        public long Id { get; set; }
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public string? Mobile { get; set; }
        public string? ReferralCode { get; set; }
        public string? Avatar { get; set; }
        public long? PersonalPurchase { get; set; }
        public long? TeamPurchase { get; set; }
        public List<UserNode>? Children { get; set; }
        public bool IsExpanded { get; set; }
    }
}