using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TellerSystem.Contract.ContractBase;
using TellerSystem.Communication;
using TellerSystem.Library.Ext.Function;
using TellerSystem.Library.Ext.TradeExtension;
using TellerSystem.ServiceProxy.Ext.ServiceEntitys;
using System.Windows;
using TellerSystem.Contract.Contracts;
using TellerSystem.Contract.Function;

namespace TellerSystem.Contract.Entry
{
    /// <summary>
    /// 授权逻辑入口
    /// </summary>
    public class AuthorizeEntry : EntryBase
    {
        #region 构造
        /// <summary>
        /// 授权触发器，允许绑定权限编码
        /// </summary>
        /// <param name="powerCode"></param>
        public AuthorizeEntry(string powerCode = "0")
        {
            PowerCode = powerCode;
        }
        /// <summary>
        /// 权限编号
        /// </summary>
        public string PowerCode { get; private set; }
        #endregion

        #region 事件
        /// <summary>
        /// 准备授权数据事件
        /// </summary>
        public event EventHandler<PreviewAuthorizeEventArgs> PreviewAuthorize;

        /// <summary>
        /// 准备授权数据事件参数
        /// </summary>
        public class PreviewAuthorizeEventArgs : EventArgs
        {
            #region 构造
            public PreviewAuthorizeEventArgs()
            {
                this.CustomizePages = new List<byte[]>();
            }

            public PreviewAuthorizeEventArgs(PageBase page)
                : this()
            {
                Page = page;
            }
            #endregion

            #region 核心要素
            /// <summary>
            /// 借方账号
            /// </summary>
            public string DeAccount { get; set; }
            /// <summary>
            /// 贷方账号
            /// </summary>
            public string CrAccount { get; set; }
            /// <summary>
            /// 单笔金额
            /// </summary>
            public decimal SinAmt { get; set; }
            /// <summary>
            /// 现转标志：1-现金 2-转账
            /// </summary>
            public string CTSigns { get; set; }
            /// <summary>
            /// 操作类型
            /// </summary>
            public string OperationType { get; set; }

            /// <summary>
            /// 借方账户序号
            /// </summary>
            public string DeAccountSeqn { get; set; }

            #endregion

            #region 查询要素
            /// <summary>
            /// 登记账号
            /// </summary>
            public string EnrollAccount { get; set; }
            /// <summary>
            /// 登记账号户名
            /// </summary>
            public string EnrollAccountName { get; set; }
            /// <summary>
            /// 登记发生金额
            /// </summary>
            public decimal EnrollAmt { get; set; }
            /// <summary>
            /// 登记积数
            /// </summary>
            public decimal EnrollAccumulation { get; set; }
            /// <summary>
            /// 登记授权描述
            /// </summary>
            public string EnrollDescription { get; set; }
            #endregion

            #region 其他元素
            /// <summary>
            /// 检阅授权页面，若缺失则采集当前页面快照
            /// </summary>
            public List<byte[]> CustomizePages { get; private set; }
            /// <summary>
            /// 检阅授权报文，若缺失则根据系统环境决定
            /// </summary>
            public Message CustomizeMessage { get; set; }
            /// <summary>
            /// 当前页面元素
            /// </summary>
            public PageBase Page { get; private set; }
            #endregion
        }

        /// <summary>
        /// 远程授权成功后执行
        /// </summary>
        public event EventHandler RemoteAuthorized;
        #endregion

        #region 逻辑
        /// <summary>
        /// 执行授权操作
        /// </summary>
        /// <returns></returns>
        public bool RaiseAuthorize()
        {
            if (PreviewAuthorize == null) return false;
            var args = new PreviewAuthorizeEventArgs(PageData);
            PreviewAuthorize(PageData, args);
            //获取内部授权要素
            var openBranch = string.Empty;
            //TODO:20140422
            AuthConditionElement authCondition = PageData.TradeAPIs.GetAuthElement(out openBranch, args.DeAccount, args.CrAccount, args.SinAmt.ToString(), args.CTSigns, args.OperationType, args.DeAccountSeqn);
            var pages = new List<TellerSystem.ServiceProxy.Ext.ServiceEntitys.PageData>();
            if (args.CustomizePages.Count > 0)
            {
                for (int i = 0; i < args.CustomizePages.Count; i++)
                {
                    pages.Add(new ServiceProxy.Ext.ServiceEntitys.PageData { index = i + 1, datas = args.CustomizePages[i] });
                }
            }
            else
            {
                pages.Add(new ServiceProxy.Ext.ServiceEntitys.PageData { index = 1, datas = args.Page.SaveTradePage((FrameworkElement)args.Page.Content) });
            }
            var authManager = new AuthManager();
            //TODO:暂时写死，该部分效果需要优化
            authManager.AsksuperResultMessage += (result, dataBus) =>
            {
                //授权结果呈现
                switch (result)
                {
                    case AsksuperResultType.Success:
                        //args.Page.TradeAPIs.ShowInfoMessageBox("授权已经通过，将提交交易");
                        AuthDataHelper authdata = new AuthDataHelper(PageData);
                        //远程授权，需提交
                        if (authdata.AuthWay.Equals("remote") && RemoteAuthorized != null)
                        {
                            RemoteAuthorized(this, new EventArgs());
                        }
                        break;
                    case AsksuperResultType.Failed:
                        args.Page.TradeAPIs.ShowInfoMessageBox("授权没有通过");
                        break;
                    case AsksuperResultType.Undo:
                        args.Page.TradeAPIs.ShowInfoMessageBox("授权已撤销");
                        break;
                    default:
                        break;
                }
            };
            return authManager.DoAuth(PageData, authCondition, args.CustomizeMessage ?? MessageData, pages.ToArray(), PowerCode, args.EnrollAccount, args.EnrollAmt.ToString(), args.EnrollAccumulation.ToString(), args.EnrollAccountName, args.EnrollDescription, openBranch);
        }
        #endregion
    }
}
