using C1.Xaml.PdfViewer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace PdfViewerSamples.DataModel
{
    public class PdfDocumentSource : Common.BindableBase
    {
        private double _originalPageWidth;
        private double _originalPageHeight;
        C1PdfViewer c1PdfViewer1 = null;
        private ObservableCollection<FrameworkElement> _pages = new ObservableCollection<FrameworkElement>();
        public ObservableCollection<FrameworkElement> Pages
        {
            get { return this._pages; }
            set 
            { 
                _pages = value;
                OnPropertyChanged("Pages");
            }
        }

        private double _zoom;
        public double Zoom
        {
            get { return _zoom; }
            set
            {
                SetProperty(ref _zoom, value, "Zoom");
                OnPropertyChanged("PageWidth");
                OnPropertyChanged("PageHeight");
            }
        }

        public double PageWidth
        {
            get { return _originalPageWidth * _zoom; }
        }

        public double PageHeight
        {
            get { return _originalPageHeight * _zoom; }
        }

        public void LoadDocument(string resourceName)
        {
            c1PdfViewer1 = new C1PdfViewer();
            c1PdfViewer1.RenderMode = C1.Xaml.PdfViewer.RenderMode.System;
            Assembly asm = typeof(PdfFlipView).GetTypeInfo().Assembly;
            Stream stream = asm.GetManifestResourceStream("PdfViewerSamples.Files." + resourceName);
            c1PdfViewer1.LoadDocument(stream);
            foreach (FrameworkElement fe in c1PdfViewer1.GetPages())
            {
                Pages.Add(fe);
            }

            // set page size
            this._originalPageWidth = _pages[0].ActualWidth;
            this._originalPageHeight = _pages[0].ActualHeight;
            this.Zoom = 0.7;
            c1PdfViewer1.CloseDocument();
        }

        public void ZoomIn()
        {
            if (this.Zoom < 0.9)
                this.Zoom += 0.1;
        }

        public void ZoomOut()
        {
            if (this.Zoom > 0.2)
                this.Zoom -= 0.1;
        }

        public PdfDocumentSource()
        {

        }

        public void Close()
        {
            _pages.Clear();
        }

    }
}
