using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Xps.Packaging;
using System.Threading;
using Platform.Presentation.Interfaces;

namespace Platform.Print
{
    /// <summary>
    /// PrintPreviewContent.xaml 的交互逻辑
    /// </summary>
    public partial class PrintPreviewContent
    {
        #region 字段
        /// <summary>
        /// 打印管理类
        /// </summary>
        private PrintManager _printManager;
        #endregion

        #region Prop
        #region IsEnableFocusControl
        public bool IsEnableFocusControl
        {
            get { return (bool)GetValue(IsEnableFocusControlProperty); }
            set { SetValue(IsEnableFocusControlProperty, value); }
        }

        public static readonly DependencyProperty IsEnableFocusControlProperty =
            DependencyProperty.Register("IsEnableFocusControl", typeof(bool), typeof(PrintPreviewContent), new UIPropertyMetadata(true));
        #endregion
        #endregion

        #region 方法
        /// <summary>
        /// 初始化打印窗体
        /// </summary>
        /// <param name="xpsFile">要解析的xps文件</param>
        /// <param name="device">打印设备</param>
        /// <param name="isReprint">是否添加补打标记</param>
        public PrintPreviewContent(FileInfo xpsFile, PrintDevice device, bool isReprint = false)
        {
            InitializeComponent();

            if (xpsFile == null)
                throw new ArgumentNullException("xpsFile");
            _printManager = new PrintManager(xpsFile, device, isReprint);
            Panel_Print.Width = _printManager.PrintElement.Width;
            Panel_Print.Height = _printManager.PrintElement.Height;
            Panel_Print1.Width = _printManager.PrintElement.Width;
            Panel_Print1.Height = _printManager.PrintElement.Height;
            //表单名称
            Text_Title.Text = _printManager.Title;
            //表单描述
            Text_Description.Text = _printManager.Description;
            //背景画刷
            Brush background = Brushes.White;
            if (!string.IsNullOrEmpty(_printManager.Subject))
            {
                //要求必须使用全局资源作为背景图像
                if (_printManager.Subject.StartsWith("ImageSource_"))
                {
                    Uri uri = null;
                    if (Uri.TryCreate(_printManager.Subject.Substring(12), UriKind.RelativeOrAbsolute, out uri))
                    {
                        background = new ImageBrush(new BitmapImage(uri));
                    }
                }
                else if (_printManager.Subject.StartsWith("StaticResource_"))
                {
                    background = Application.Current.TryFindResource(_printManager.Subject.Substring(15)) as Brush;
                }
            }
            if (!_printManager.IsNotingPreview)
            {
                Panel_Print.Background = background;
                _printManager.PrintElement.Background = Brushes.Transparent;
                Panel_Print1.Background = new VisualBrush(_printManager.PrintElement);
            }
            else
            {
                Panel_NotingPreview.Visibility = System.Windows.Visibility.Visible;
                Panel_NotingPreview.Background = background;
            }
        }

        /// <summary>
        /// 打印当前项
        /// </summary>
        public void Print()
        {
            _printManager.Print() ;
        }

        /// <summary>
        /// 是否已经打印
        /// </summary>
        public bool IsPrinted
        {
            get
            {
                return this._printManager.IsPrinted;
            }
        }
        #endregion
    }
}
