using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using C1.Xaml.Pdf;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace PdfCreator
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        C1PdfDocument _pdf;
        CollectionViewSource _cvs = new CollectionViewSource();

        public MainPage()
        {
            this.InitializeComponent();

            // create a zip file to work with
            _pdf = new C1PdfDocument();

            // bind grid to entries collection
        }

        // refresh view when collection is modified
        void RefreshView()
        {
        }

        // open an existing zip file
        async void _btnOpen_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var picker = new Windows.Storage.Pickers.FileOpenPicker();
                //picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
                picker.FileTypeFilter.Add(".pdf");

                var file = await picker.PickSingleFileAsync();
                if (file != null)
                {
                    var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
                    //_pdf.Lo.Open(stream.AsStream());
                    RefreshView();
                }
            }
            catch (Exception x)
            {
                // snapped?
                System.Diagnostics.Debug.WriteLine(x.Message);
            }
        }

        // add files to zip
        async void _btnAdd_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                var picker = new Windows.Storage.Pickers.FileOpenPicker();
                //picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
                picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
                picker.FileTypeFilter.Add("*");

                var files = await picker.PickMultipleFilesAsync();
                if (files != null)
                {
                    foreach (var f in files)
                    {
                        //await _pdf.Entries.AddAsync(f);
                    }
                }
            }
            catch
            {
                // snapped?
            }
            RefreshView();
        }

        // remove selected entries from zip
        void _btnRemove_Click_1(object sender, RoutedEventArgs e)
        {
            RefreshView();
        }

        // show a preview of the selected entry
        void _btnView_Click_1(object sender, RoutedEventArgs e)
        {
        }

        // close the preview pane by hiding it
        void _btnClosePreview_Click_1(object sender, RoutedEventArgs e)
        {
            _preview.Visibility = Visibility.Collapsed;
        }
    }
}
