using System.Collections.Generic;
using Platform.Library;
using Platform.Print;
using TellerSystem.Contract.ContractBase;
using TellerSystem.Contract.Contracts;
using TellerSystem.Library.Ext.Function;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using TellerSystem.Contract.Function;
using TellerSystem.ServiceProxy.Ext.ServiceEntitys;
using Platform.Common.RunningParameters;
using TellerSystem.ServiceProxy.Ext.ServiceHelper;
using System.Windows;
using TellerSystem.Library.Ext.TradeExtension;
using TellerSystem.PrintTemplate.Extension;
using TellerSystem.PrintTemplate.PrintTemplateData;
using System;
using Platform.Common.LogSystem;
using TellerSystem.Communication;
using System.Text;
using TellerSystem.Communication.SocketListener;
using System.Linq;
using TellerSystem.ServiceProxy.Ext.TradeBusinessEntitys;
using TellerSystem.ServiceProxy.Ext;
using System.Diagnostics;

namespace TellerSystem.Contract.Entry
{
    /// <summary>
    /// 打印逻辑入口
    /// </summary>
    public abstract class PrintflowEntry : EntryBase
    {
        #region 抽象方法
        /// <summary>
        /// 创建打印列表，抽象成交易自身定制
        /// </summary>
        /// <returns></returns>
        public abstract List<PrintContainer> GeneratePrintList();

        /// <summary>
        /// 预处理过程
        /// </summary>
        /// <returns></returns>
        public abstract bool PreviewPrintflow();
        #endregion

        protected override bool OnLoadData(bool changed)
        {
            //触发无纸化操作
            return PageData.TradeAPIs.RaisePaperless(MessageData);
        }

        #region 控制
        /// <summary>
        /// 触发打印流程
        /// </summary>
        /// <param name="stash"></param>
        /// <returns></returns>
        internal bool RaisePrintflow(bool stash = true)
        {
            var flow = this;
            var page = flow.PageData;
            var message = flow.MessageData;
            try
            {
                //stash数据
                if (stash)
                {
                    if (!StashPrintData())
                        return false;
                }
                //主流程执行,产生打印清单
                var printList = flow.GeneratePrintList();
                if (printList.Count == 0)
                    return true;
                return ContractManager.RaisePrint(page, message, printList);
            }
            catch (Exception ex)
            {
                page.TradeAPIs.ShowErrorMessageBox("RaisePrintflow:创建打印清单发生异常！\n" + ex.Message);
                PlatformLogger.SystemErrorInfo("RaisePrintflow:创建打印清单发生异常！", ex);
                return false;
            }
        }

        /// <summary>
        /// 存储页面打印信息，以系统流水作为key
        /// </summary>
        /// <returns></returns>
        protected bool StashPrintData()
        {
            if (!this.PageData.CheckLoginStatus()) return false;
            //触发存储，异步操作
            StashPage();
            //执行预处理
            return PreviewPrintflow();
        }

        /// <summary>
        /// 内部登记实现
        /// </summary>
        private void StashPage()
        {
            try
            {
                //开关检查
                if (!ServiceSettings.IsRecordPrintData)
                    return;
                //存入到总线自定义项中
                var id = PageData.TradeInfo.SystemSerialNumber;
                var message = MessageData;
                if (message != null)
                {
                    message.IsMainTrade = true;
                    message.SerialNumber = id;
                }
                //流程对象存入到总线，方便后续恢复使用
                PageData.TradeBus["PrintflowEntry"] = this.GetType().ToString();
                //上送数据
                var dll = Platform.Common.RunningParameters.PlatformSettings.PagesDirectory + @"\" + PageData.GetType().Assembly.GetName().Name + ".dll";
                var fullName = PageData.GetType().ToString();
                var printAttr = PageData.GetPrintAttributes();
                string json = printAttr.Serialize();
                string bus = Platform.ServiceProxy.CommonFunction.ChangeDicToStr((Dictionary<string, string>)PageData.GetTradeInfoData().TradeBus.Serialize());  //序列化总线数据
                //异步触发，防止产生性能影响
                new Action(() =>
                {
                    PrintManagerHandle.RecordPrintExt(id, dll, fullName, json, bus);
                }).BeginInvoke(null, null);
            }
            catch (Exception ex)
            {
                PlatformLogger.SystemErrorInfo("StashPage:保存页面信息发生异常！", ex);
            }
        }
        #endregion
    }
}
