using System;
using System.Windows;
using Elwig.Helpers;

namespace Elwig.Windows {
    public partial class DocumentViewerWindow : Window {

        private TempFile? PdfFile = null;
        private string? PdfPath = null;

        public DocumentViewerWindow(string title, string path) {
            InitializeComponent();
            Title = $"{title} - {Title}";
            PdfPath = path;
            InitializeWebView();
        }

        public async void InitializeWebView() {
            WebView.CreationProperties = new() {
                UserDataFolder = App.TempPath,
            };
            await WebView.EnsureCoreWebView2Async();
            WebView.Source = new($"file://{PdfPath}#view=FitH");
        }

        public DocumentViewerWindow(string title, TempFile file) : this(title, file.FilePath) {
            PdfFile = file;
        }

        public void OnClosed(object sender, EventArgs evt) {
            WebView.Stop();
            WebView.Dispose();
            PdfFile?.Dispose();
            PdfFile = null;
            PdfPath = null;
        }
    }
}
