using System;
using System.Globalization;
using System.Xml.Serialization;
using Hncore.Pass.PaymentCenter.Domain;

namespace Hncore.Pass.PaymentCenter.WeiFuTong.SwipeCard
{
    /// <summary>
    /// 刷卡支付查询订单响应
    /// </summary>
    [XmlRoot("xml", Namespace = "")]
    public class WeiFuTongQueryOrderResponse : WeiFuTongResponseBase
    {
        // 以下字段在 status 为 0的时候有返回

        /// <summary>
        /// 门店编号，由平台分配
        /// </summary>
        [XmlElement("mch_id")]
        public string MchId { get; set; }

        /// <summary>
        /// 终端设备号
        /// </summary>
        [XmlElement("device_info")]
        public string DeviceInfo { get; set; }

        // 以下字段在 status 和 result_code 都为 0的时候有返回

        /// <summary>
        /// SUCCESS—支付成功
        /// REFUND—转入退款
        /// NOTPAY—未支付
        /// CLOSED—已关闭
        /// REVOKED/REVERSE—已撤销
        /// USERPAYING—用户支付中
        /// PAYERROR—支付失败(其他原因，如银行返回失败)
        /// </summary>
        [XmlElement("trade_state")]
        public string TradeState { get; set; }

        // 以下字段在 trade_state为 SUCCESS的时候有返回

        /// <summary>
        /// pay.wechat.micropay——微信刷卡支付
        /// pay.alipay.micropay——支付宝刷卡支付
        /// pay.jdpay.micropay——京东刷卡支付
        /// pay.qq.micropay——QQ钱包刷卡支付
        /// pay.shiming.micropay——会员卡支付
        /// pay.unionpay.micropay——银联支付
        /// pay.bestpay.micropay——翼支付
        /// </summary>
        [XmlElement("trade_type")]
        public string TradeType { get; set; }

        /// <summary>
        /// 受理商户appid
        /// </summary>
        [XmlElement("appid")]
        public string AppId { get; set; }

        /// <summary>
        /// 子商户appid
        /// </summary>
        [XmlElement("sub_appid")]
        public string SubAppId { get; set; }

        /// <summary>
        /// 用户在受理商户 appid 下的唯一标识
        /// </summary>
        [XmlElement("openid")]
        public string OpenId { get; set; }

        /// <summary>
        /// 用户在子商户appid下的唯一标识
        /// </summary>
        [XmlElement("sub_openid")]
        public string SubOpenId { get; set; }

        /// <summary>
        /// 用户是否关注公众账号，Y-关注，N-未关注，仅在公众账号类型支付有效
        /// </summary>
        [XmlElement("is_subscribe")]
        public string IsSubscribe { get; set; }

        /// <summary>
        /// 用户是否关注子公众账号，Y-关注，N-未关注，仅在公众账号类型支付有效
        /// </summary>
        [XmlElement("sub_is_subscribe")]
        public string SubIsSubscribe { get; set; }

        /// <summary>
        /// 平台交易号
        /// </summary>
        [XmlElement("transaction_id")]
        public string TransactionId { get; set; }

        /// <summary>
        /// 第三方订单号
        /// </summary>
        [XmlElement("out_transaction_id")]
        public string OutTransactionId { get; set; }

        /// <summary>
        /// 第三方商户单号，可在支持的商户扫码退款
        /// </summary>
        [XmlElement("third_order_no")]
        public string ThirdOrderNo { get; set; }

        /// <summary>
        /// 商户系统内部的定单号，32个字符内、可包含字母
        /// </summary>
        [XmlElement("out_trade_no")]
        public string OutTradeNo { get; set; }

        /// <summary>
        /// 总金额，以分为单位，不允许包含任何字、符号
        /// </summary>
        [XmlElement("total_fee")]
        public int TotalFee { get; set; }

        /// <summary>
        /// 现金支付金额【微信】 
        /// </summary>
        [XmlElement("cash_fee")]
        public int CashFee { get; set; }

        /// <summary>
        /// 开票金额 【支付宝】
        /// </summary>
        [XmlElement("invoice_amount")]
        public string InvoiceAmount { get; set; }

        /// <summary>
        /// 实收金额【支付宝】实收金额，单位为元，两位小数。该金额为本笔交易，商户账户能够实际收到的金额
        /// </summary>
        [XmlElement("receipt_amount")]
        public string ReceiptAmount { get; set; }

        /// <summary>
        /// 买家实付金额【支付宝】买家实付金额，单位为元，两位小数。该金额代表该笔交易买家实际支付的金额，不包含商户折扣等金额 
        /// </summary>
        [XmlElement("buyer_pay_amount")]
        public string BuyerPayAmount { get; set; }

        /// <summary>
        /// 集分宝付款金额【支付宝】
        /// </summary>
        [XmlElement("point_amount")]
        public string PointAmount { get; set; }

        /// <summary>
        /// 现金券金额【微信】
        /// </summary>
        [XmlElement("coupon_fee")]
        public string CouponFee { get; set; }

        /// <summary>
        /// 货币类型，符合 ISO 4217 标准的三位字母代码，默认人民币：CNY
        /// </summary>
        [XmlElement("fee_type")]
        public string FeeType { get; set; }

        /// <summary>
        /// 商家数据包，原样返回
        /// </summary>
        [XmlElement("attach")]
        public string Attach { get; set; }

        /// <summary>
        /// 付款银行
        /// </summary>
        [XmlElement("bank_type")]
        public string BankType { get; set; }

        /// <summary>
        /// 银行订单号，若为微信支付则为空
        /// </summary>
        [XmlElement("bank_billno")]
        public string BankBillno { get; set; }

        /// <summary>
        /// 支付完成时间，格式为yyyyMMddHHmmss，如2009年12月27日9点10分10秒表示为20091227091010。时区为GMT+8 beijing。该时间取自平台服务器
        /// </summary>
        [XmlElement("time_end")]
        public string TimeEnd { get; set; }

        /// <summary>
        /// 是否支付成功
        /// </summary>
        /// <returns></returns>
        public bool IsPaySuccess()
        {
            return !HasError() && (TradeState == "SUCCESS" || TradeState == "REFUND");
        }

        /// <summary>
        /// 是否为支付失败，未支付和支付中不认定为失败
        /// </summary>
        /// <returns></returns>
        public bool IsPayFailed()
        {
            return !HasError()
                   && TradeState != "SUCCESS"
                   && TradeState != "NOTPAY"
                   && TradeState != "USERPAYING"
                   && TradeState != "REFUND";
        }

        /// <summary>
        /// 获取支付成功时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetPaySuccessTime()
        {
            DateTime.TryParseExact(TimeEnd, "yyyyMMddHHmmss", null, DateTimeStyles.None, out var paySuccessTime);

            return paySuccessTime;
        }
    }
}