using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TellerSystem.Contract.ContractBase;
using TellerSystem.Communication;
using TellerSystem.Library.Ext.TradeExtension;
using Platform.Common.BasicFunctions;
using Platform.Presentation.PagesHelper;
using TellerSystem.Contract.Contracts;
using TellerSystem.Contract.Function;

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

        #region 事件
        /// <summary>
        /// 准备授权数据事件
        /// </summary>
        public event EventHandler<PreviewRecheckEventArgs> PreviewRecheck;
        /// <summary>
        /// 准备复核数据事件参数
        /// </summary>
        public class PreviewRecheckEventArgs : EventArgs
        {
            #region 构造
            public PreviewRecheckEventArgs(PageBase page)
            {
                Page = page;
            }
            #endregion

            #region 复核条件要素
            /// <summary>
            /// 账户
            /// </summary>
            public string Account { get; set; }
            /// <summary>
            /// 金额
            /// </summary>
            public decimal Amt { get; set; }
            #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 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
        }
        #endregion

        #region 逻辑
        /// <summary>
        /// 执行复核操作
        /// </summary>
        /// <returns></returns>
        public bool RaiseRecheck()
        {
            if (PreviewRecheck == null) return false;
            var args = new PreviewRecheckEventArgs(PageData);
            PreviewRecheck(PageData, args);
            //获取内部授权要素
            var recheckCondition = PageData.TradeAPIs.GetCheckElement(args);
            if (recheckCondition == null)
            {
                return false;
            }
            recheckCondition.LegalPerson = LoginAuthentication.Instance.GroupKinbrNo;
            CheckManager c = new CheckManager();
            c.AsksuperResultMessage += (result, dataBus) =>
                {
                    //TODO:具体处理方案
                    switch (result)
                    {
                        case AsksuperResultType.Success:
                            IRecheckContract irc = args.Page as IRecheckContract;
                            //执行提交部分授权检测
                            TradeModel trademodel = PageHelper.GetTradeModel(PageData);
                            //判断是否配置授权标志
                            if (trademodel.Metadata != null && ((Dictionary<string, string>)trademodel.Metadata).ContainsKey("isAuth"))
                            {
                                string isauth = string.Empty;
                                ((Dictionary<string, string>)trademodel.Metadata).TryGetValue("isAuth", out isauth);
                                //如果配置授权标志为true，则调用授权检查
                                if (isauth.Equals("true"))
                                {
                                    if (PageData is IAuthorizeContract)
                                    {
                                        var contract = PageData as IAuthorizeContract;
                                        if (contract == null)
                                        {
                                            args.Page.TradeAPIs.ShowInfoMessageBox("复核已通过，在下一步检查授权时出错，请检查");
                                            break;
                                        }
                                        if (contract.AuthorizeEntries == null || contract.AuthorizeEntries.Count == 0)
                                        {
                                            args.Page.TradeAPIs.ShowInfoMessageBox("复核已通过，在下一步检查授权时出错，请检查");
                                            break;
                                        }
                                        var entry = contract.AuthorizeEntries.Find(x => { return x.PowerCode == "0"; });
                                        if (entry == null)
                                        {
                                            args.Page.TradeAPIs.ShowInfoMessageBox("复核已通过，在下一步检查授权时出错，请检查");
                                            break;
                                        }
                                        entry.LoadData(irc.RecheckEntries[0].PageData, irc.RecheckEntries[0].MessageData);
                                        args.Page.TradeAPIs.ShowInfoMessageBox("复核已通过，将做下一步授权检查");
                                        if (!entry.RaiseAuthorize()) //需要授权
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                args.Page.TradeAPIs.ShowInfoMessageBox("复核已经通过，将提交交易");
                            }
                            var submit = (args.Page as ISubmitContract);
                            submit.SubmitEntry.LoadData(irc.RecheckEntries[0].PageData, irc.RecheckEntries[0].MessageData);
                            submit.SubmitEntry.RaiseSubmit();
                            break;
                        case AsksuperResultType.Failed:
                            args.Page.TradeAPIs.ShowInfoMessageBox("复核没有通过");
                            break;
                        case AsksuperResultType.Undo:
                            args.Page.TradeAPIs.ShowInfoMessageBox("复核没已取消");
                            break;
                        default:
                            break;
                    }
                };
            return c.DoSuper(PageData, recheckCondition, args.CustomizeMessage ?? MessageData, PowerCode, args.EnrollAccount, double.Parse(args.EnrollAmt.ToString()), args.EnrollAccountName, args.EnrollDescription);
        }
        #endregion
    }
}
