@page "/msgfile"
@using DevExpress.Blazor.Office
@using System.Text
@rendermode InteractiveServer


<div id="overviewDemoDropZone" class="card custom-drop-zone rounded-3 w-100 m-0">
    <span class="drop-file-icon mb-3"></span>
    <span class="drop-file-label">Drag and Drop File Here</span><span class="m-1">or</span>
    <DxButton Id="overviewDemoSelectButton"
              CssClass="m-1"
              RenderStyle="ButtonRenderStyle.Primary"
              Text="Select File" />
</div>
<DxFileInput @ref="fileInput"
             AcceptedFileTypes="@ALLOWED_FILE_TYPES"
             AllowedFileExtensions="@ALLOWED_FILE_TYPES"
             ShowFileList=false
             CssClass="w-100"
             ExternalDropZoneCssSelector="#overviewDemoDropZone"
             ExternalDropZoneDragOverCssClass="custom-drop-zone-hover"
             ExternalSelectButtonCssSelector="#overviewDemoSelectButton"
             FilesUploading="OnFilesUploading"
             AllowMultiFileUpload=true
             Visible=false
             MaxFileSize="2000000">
</DxFileInput>

<div>@((MarkupString)@markup)</div>
<div>
    <p>@LastError</p>
</div>
@code {
    readonly List<string> ALLOWED_FILE_TYPES = new List<string> { ".msg" };
    DxFileInput fileInput;
    string markup = "";
    string LastError = "";


    protected async Task OnFilesUploading(FilesUploadingEventArgs args)
    {
        LastError = "";
        try
        {
            using (MemoryStream stream = new MemoryStream())
            {
                IFileInputSelectedFile file = args.Files[0];
                Console.WriteLine($"Uploading {args.Files[0]}");
                await file.OpenReadStream(file.Size).CopyToAsync(stream);

                using (var msg = new MsgReader.Outlook.Storage.Message(stream))
                {
                    var from = msg.Sender;
                    var sentOn = msg.SentOn;
                    var recipientsTo = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.To, false, false);
                    var recipientsCc = msg.GetEmailRecipients(MsgReader.Outlook.RecipientType.Cc, false, false);
                    var subject = msg.Subject;
                    var htmlBody = msg.BodyHtml;
                    //txtGeadresseerders.Text = recipientsTo;
                    //txtVerzender.Text = from.DisplayName;
                    markup = msg.BodyHtml;
                    //richEditControl1.RtfText = msg.BodyRtf;
                    //MessageBox.Show(msg.BodyRtf);
                    // etc...
                    //await InvokeAsync(StateHasChanged);
                }
            }
        }
        catch (Exception e)
        {
            markup = "";
            LastError = $"{e.GetType().ToString()}: thrown with message: {e.Message}";
        }
        StateHasChanged();
    }

}