using System;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Threading;
using InABox.Wpf;
using InABox.WPF;

namespace InABox.DynamicGrid
{
    /// <summary>
    ///     Interaction logic for PrintPreview.xaml
    /// </summary>
    public partial class PDFPreview : ThemableWindow
    {
        private readonly string _filename = "";

        private bool bPrinted;
        private WaitCursor cursor;

        public PDFPreview(string filename)
        {
            InitializeComponent();
            cursor = new WaitCursor();
            _filename = filename;
        }

        private void Browser_LoadCompleted(object sender, NavigationEventArgs e)
        {
            //MessageBox.Show("Loaded");
            var timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 2);
            timer.Tick += (ot, et) =>
            {
                if (!bPrinted)
                {
                    cursor.Dispose();
                    cursor = null;
                    //MessageBox.Show("Printing!");
                    //IHTMLDocument2 doc = browser.Document as IHTMLDocument2;
                    //doc.execCommand("Print", true, null);
                    bPrinted = true;
                }
                else
                {
                    timer.IsEnabled = false;
                    //MessageBox.Show("Closing!");
                    Close();
                }
            };
            timer.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var html = string.Format(
                "<html><embed src=\"file:///{0}\" type=\"application/x-pdf\" title=\"Pdf Preview\" width=\"100%\" height=\"100%\" /></html>",
                _filename);
            browser.NavigateToString(html);
        }
    }
}