// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Service;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;

namespace SelmoStudio.Hmi.Server.Shared.Components.BaseComponents
{
    public partial class ExportComponent : ButtonBaseComponent
    {
        [Inject]
        protected IFileDownloadService _fileDownloadService { get; set; }

        [Parameter]
        public object Payload { get; set; }

        [Parameter]
        public string Filename { get; set; }

        [Parameter]
        public WellKnownFileFormat FileFormat { get; set; }

        protected override async Task OnParametersSetAsync()
        {
            ContentFlexDirection = WellKnownContentFlexDirection.Column;
            Disabled = Disabled || Payload == null || string.IsNullOrEmpty(Filename);

            await base.OnParametersSetAsync();
        }

        protected override async Task OnClick()
        {
            await _fileDownloadService.DownloadAsync(Filename, FileFormat, Payload);
        }
    }
}