using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace PdfPassword
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            _btnCreate.Click += _btnCreate_Click;
            _btnOpen.Click += _btnOpen_Click;
        }

        // create a password-protected PDF file
        void _btnCreate_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new SaveFileDialog();
            dlg.Filter = "Pdf files (*.pdf)|*.pdf";
            if (dlg.ShowDialog().Value)
            {
                using (var stream = dlg.OpenFile())
                {
                    var pdf = new C1.Silverlight.Pdf.C1PdfDocument();
                    pdf.Security.UserPassword = "thePassword";
                    pdf.DrawElement(_contentGrid, pdf.PageRectangle, C1.Silverlight.Pdf.ContentAlignment.MiddleCenter, Stretch.Uniform);
                    pdf.Save(stream);
                    MessageBox.Show("Protected pdf saved. Password is 'thePassword'.");
                }
            }
        }

        // open an existing PDF file
        void _btnOpen_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "Pdf files (*.pdf)|*.pdf";
            if (dlg.ShowDialog().Value)
            {
                var ms = new System.IO.MemoryStream();
                using (var stream = dlg.File.OpenRead())
                {
                    stream.CopyTo(ms);
                }
                LoadProtectedDocument(ms, null);
            }
        }

        // loads password-protected Pdf documents.
        // calling this method with null for a password will open unprotected pdf files.
        // if the pdf file is password-protected, an exeption will be thrown and caught,
        // the user will be prompted for the actual password, and the method will call
        // itself recursively.
        void LoadProtectedDocument(System.IO.MemoryStream stream, string password)
        {
            try
            {
                stream.Position = 0;
                _viewer.LoadDocument(stream, password);
            }
            catch (Exception x)
            {
                if (x.Message.IndexOf("password") > -1)
                {
                    var msg = "This file seems to be password-protected.\r\nPlease provide the password and try again.";
                    C1.Silverlight.C1PromptBox.Show(msg, "Enter Password", (text, result) =>
                        {
                            if (result == MessageBoxResult.OK)
                            {
                                // try again using the password provided by the user
                                LoadProtectedDocument(stream, text);
                            }
                        });
                }
                else
                {
                    throw;
                }
            }
        }
    }
}
