using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Platform.Presentation.PageFunctions;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using Platform.Common.RunningParameters;
using TellerSystem.Library.Ext.Function;
using Platform.Presentation.FocusManagers;
using TellerSystem.ServiceProxy.Ext;
using TellerSystem.Contract.Entry;
using Platform.Common.LogSystem;
using TellerSystem.PrintTemplate.Extension;
using TellerSystem.ServiceProxy.Ext.ServiceHelper;

namespace TellerSystem.Contract.ContractBase
{
    /// <summary>
    /// 页面基类，填充通用的业务接口API
    /// 注意:该对象需要支持序列化/反序列化
    /// </summary>
    public abstract class PageBase : TradePage, ITradeHelper
    {
        #region InitPage
        /// <summary>
        /// 银行标签
        /// </summary>
        public string BankFlag
        {
            get
            {
                return ServiceSettings.BankFlag;
            }
        }

        /// <summary>
        /// 密封
        /// </summary>
        protected sealed override void OnTradePageLoaded()
        {
            var flag = false;
            try
            {
                base.OnTradePageLoaded();
                //加载页面初始化
                flag = InitPage();
                if (flag)
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        if (FocusManager.GetEnableFocusControl(this))
                        {
                            //若焦点控件不存在，则认为初始化焦点失败
                            var fm = FocusManager.GetManagerInstance(this);
                            if (fm != null && fm.CurrentFocusedElement == null)
                            {
                                fm.Refresh();
                                fm.ReSetFocus();
                            }
                            //将交易打开的时间登记到bus
                            try {
                                if (string.IsNullOrEmpty(TradeBus["__StartTime__"]))
                                    this.TradeBus["__StartTime__"] = DateTime.Now.ToString("yyyyMMddHHmmss");
                            }
                            catch (Exception ex)
                            {
                                TradeHandle.WriteImportantLog("获取交易打开时间异常", ex.Message, "StartTime");
                                return;
                            }
                        }
                    }), System.Windows.Threading.DispatcherPriority.SystemIdle, null);
            }
            catch (Exception e)
            {
                Platform.Common.LogSystem.PlatformLogger.TradeErrorInfo("调用InitPage失败！", e, TradeModel.Code);
            }
            if (!flag)
            {
                this.OwnerContainer.ClosePage();
                //this.Dispatcher.BeginInvoke(new Action(() => { this.OwnerContainer.ClosePage(); }), System.Windows.Threading.DispatcherPriority.Loaded);
            }
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        /// <returns></returns>
        public virtual bool InitPage()
        {
            return true;
        }

        public class PagePrintedEventArgs : EventArgs
        {
            public PagePrintedEventArgs(PrintAttributes attr)
            {
                PrintAttr = attr;
            }
            public PrintAttributes PrintAttr { get; private set; }
        }

        public event EventHandler<PagePrintedEventArgs> PagePrinted;

        public void Log2PagePrinted(string log)
        {
            try
            {
                PlatformLogger.SystemInfo("Log2PagePrinted:start 【" + log + "】=>" + this.PagePrinted);
                if (this.PagePrinted == null) return;
                var list = this.PagePrinted.GetInvocationList();
                if (list == null) return;
                PlatformLogger.SystemInfo("Log2PagePrinted:list length=>" + list.Length + ",data=>"
                    + string.Join(",", list.ToList().Where(x => x != null).Select(d => d.Method.ToString()).ToList()));
            }
            catch { }
        }

        /// <summary>
        /// 触发打印后事件
        /// </summary>
        public void OnPagePrinted(PrintAttributes attr)
        {
            PlatformLogger.SystemInfo("OnPagePrinted:触发事件=>" + this.PagePrinted);
            if (this.PagePrinted != null)
            {
                Log2PagePrinted("OnPagePrinted.start");
                this.PagePrinted(this, new PagePrintedEventArgs(attr));
                Log2PagePrinted("OnPagePrinted.end");
            }
        }
        #endregion

        #region 访问接口
        #region 业务总线TradeBus
        private TradeBusHelper _tradeBus;
        /// <summary>
        /// 业务总线
        /// </summary>
        public TradeBusHelper TradeBus
        {
            get
            {
                if (_tradeBus == null)
                    _tradeBus = new TradeBusHelper(this);
                return _tradeBus;
            }
        }
        #endregion

        #region 交易信息TradeInfo
        private TradeInfoHelper _tradeInfo;
        /// <summary>
        /// 交易信息
        /// </summary>
        public TradeInfoHelper TradeInfo
        {
            get
            {
                if (_tradeInfo == null)
                    _tradeInfo = new TradeInfoHelper(this);
                return _tradeInfo;
            }
        }
        #endregion

        #region 操作接口TradeAPIs

        private TradeAPIsHelper _tradeAPIs;
        /// <summary>
        /// 操作接口
        /// </summary>
        public TradeAPIsHelper TradeAPIs
        {
            get
            {
                if (_tradeAPIs == null)
                    _tradeAPIs = new TradeAPIsHelper(this);
                return _tradeAPIs;
            }
        }

        #endregion

        #region 资源集合TradeResources
        private TradeResourcesHelper _tradeResources;
        /// <summary>
        /// 资源集合
        /// </summary>
        public TradeResourcesHelper TradeResources
        {
            get
            {
                if (_tradeResources == null)
                    _tradeResources = new TradeResourcesHelper(this);
                return _tradeResources;
            }
        }

        #endregion

        #region Paperless
        private PaperlessEntry paperless;
        /// <summary>
        /// 无纸化对象
        /// </summary>
        public PaperlessEntry Paperless
        {
            get
            {
                if (paperless == null)
                    paperless = new PaperlessEntry();
                return paperless;
            }
        }
        #endregion

        #region CashCode
        private CashCodeEntry cashCode;
        /// <summary>
        /// 冠字号对象
        /// </summary>
        public CashCodeEntry CashCode
        {
            get
            {
                if (cashCode == null)
                    cashCode = new CashCodeEntry();
                return cashCode;
            }
        }
        #endregion
        #endregion

        #region 清理
        /// <summary>
        /// 清理缓存数据
        /// </summary>
        public void Clear()
        {
            this.paperless = null;
            this._tradeAPIs = null;
            this._tradeBus = null;
            this._tradeInfo = null;
            this._tradeResources = null;
        }
        #endregion
    }
}
