@using AntDesign.Charts
@using ByteSizeLib

<Spin Spinning="Loading">
    <Pie Data="Data" Config="Config" JsConfig="@JsConfig" />
</Spin>

@code {
    object[] Data;

    bool Loading = true;

    string JsConfig = @"{
        meta: {
            value: {
                alias: 'Data Usage',
                formatter: (v) => Uploader.GetHumanFileSize(v, true, 1)
            }
        },
        label: {
            visible: true,
            type: 'outer-center'
        }
    }";

    PieConfig Config = new PieConfig
    {
        Radius = 0.8,
        AngleField = "value",
        ColorField = "type",
    };

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var settings = SettingService.GetSettings();
            var drives = DriveInfo.GetDrives();
            var root = Path.GetPathRoot(System.Reflection.Assembly.GetExecutingAssembly().Location);

            var totalStorageSize = drives.Where(d => d.IsReady && d.Name == root).Sum(d => d.TotalSize);
            var totalAvailableFreeSpace = drives.Where(d => d.IsReady && d.Name == root).Sum(d => d.AvailableFreeSpace);
            var totalUploadDirectorySize = new DirectoryInfo(settings.Archives.StoragePath).EnumerateFiles().Sum(f => f.Length);
            var totalSaveDirectorySize = new DirectoryInfo(settings.UserSaves.StoragePath).EnumerateFiles().Sum(f => f.Length);

            Data = new object[]
            {
            new {
                type = "Free",
                value = totalAvailableFreeSpace
            },
            new {
                type = "Games",
                value = totalUploadDirectorySize
            },
            new
            {
                type = "Saves",
                value = totalSaveDirectorySize
            },
            new
            {
                type = "Other",
                value = totalStorageSize - totalAvailableFreeSpace - totalUploadDirectorySize - totalSaveDirectorySize
            }
            };

            Loading = false;

            StateHasChanged();
        }
    }
}