using BaseModel.Models;
using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;

namespace Wechat.PayAPI
{
    public class MicroPay
    {
        public static NLog.Logger Logger = NLog.LogManager.LoadConfiguration("nlog.xml").GetLogger("Main");
        /**
        * 刷卡支付完整业务流程逻辑
        * @param body 商品描述
        * @param total_fee 总金额
        * @param bill_number 商户订单号
        * @param auth_code 支付授权码
        * @param trade_status 支付授权码
        * @throws WxPayException
        * @return 刷卡支付结果
        */
        public static async Task<WxPayData> Run(ElectronicOrderModel order)
        {
            //string body, string total_fee, string bill_number, string auth_code

            //set the default trade_status to PAYERROR
            order.TradeStatus = TradeStatus.PAYERROR;

            WxPayData data = new WxPayData();
            data.SetValue("auth_code", order.AuthCode);//授权码
            data.SetValue("body", order.Title);//商品描述
            data.SetValue("total_fee", Convert.ToInt32(order.NetAmount * 100));//总金额

            data.SetValue("out_trade_no", order.BillNumber);//商户订单号
            
            WxPayData result = await WxPayApi.Micropay(data, (WxPayConfig)order.Config, 20); //提交被扫支付，接收返回结果

		    //如果提交被扫支付接口调用失败，则抛异常
            if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL")
		    {
                string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToString() : "";
                Log.Error("MicroPay", "Micropay API interface call failure, result : " + result.ToXml());
                throw new WxPayException("Micropay API interface call failure, return_msg : " + returnMsg);
		    }

		    //签名验证
            result.CheckSign((WxPayConfig)order.Config);
            Log.Debug("MicroPay", "Micropay response check sign success");

            //刷卡支付直接成功
            if(result.GetValue("return_code").ToString() == "SUCCESS" &&
		        result.GetValue("result_code").ToString() == "SUCCESS")
            {
                Log.Debug("MicroPay", "Micropay business success, result : " + result.ToXml());
                order.TradeStatus = TradeStatus.SUCCESS;
                return result;
            }

            /******************************************************************
             * 剩下的都是接口调用成功，业务失败的情况
             * ****************************************************************/
		    //1）业务结果明确失败
		    if(result.GetValue("err_code").ToString() != "USERPAYING" && 
		    result.GetValue("err_code").ToString() != "SYSTEMERROR")
		    {
                Log.Error("MicroPay", "micropay API interface call success, business failure, result : " + result.ToXml());
                return result;
		    }

            //2）不能确定是否失败，需查单
            //用商户订单号去查单
            string out_trade_no = data.GetValue("out_trade_no").ToString();

		    //确认支付是否成功,每隔一段时间查询一次订单，共查询10次
		    int queryTimes = 10;//查询次数计数器
		    while(queryTimes-- > 0)
		    {
			    WxPayDataWithResult queryResult = await Query(out_trade_no, (WxPayConfig)order.Config);

                result = queryResult.PayData;
			    //如果需要继续查询，则等待2s后继续
			    if(queryResult.Status == 2)
                {
                    await Task.Delay(2000);
				    continue;
			    }
                //查询成功,返回订单查询接口返回的数据
                else if(queryResult.Status == 1)
                {
                    Log.Debug("MicroPay", "Mircopay success, return order query result : " + queryResult.PayData.ToXml());
                    order.TradeStatus = TradeStatus.SUCCESS;
                    return queryResult.PayData;
                }
                //订单交易失败，直接返回刷卡支付接口返回的结果，失败原因会在err_code中描述
                else
                {
                    Log.Error("MicroPay", "Micropay failure, return micropay result : " + result.ToXml());
                    //return result;
			    }
		    }
            return result;

            ////确认失败，则撤销订单
            //Log.Error("MicroPay", "Micropay failure, Reverse order is processing...");
            //if(!Cancel(out_trade_no))
            //{
            //    Log.Error("MicroPay", "Reverse order failure");
            //    throw new WxPayException("Reverse order failure！");
            //}
        }

        /**
        * 统一下单后查询支付状态
        * @param body 商品描述
        * @param total_fee 总金额
        * @param bill_number 商户订单号
        * @param auth_code 支付授权码
        * @param trade_status 支付授权码
        * @throws WxPayException
        * @return 刷卡支付结果
        */
        public static async Task<WxPayData> RunQuery(ElectronicOrderModel order, int count, int interval)
        {
            //用商户订单号去查单
            string out_trade_no = order.BillNumber;
            WxPayData result = null;
            bool miniProgramPay = false;
            if (order.Optional.ContainsKey("MiniProgram"))
            {
                Logger.Info("miniProgramPay is true");
                miniProgramPay = true;
            } 
            else
            {
                Logger.Info("miniProgramPay is false");
            }

            //确认支付是否成功,每隔一段时间查询一次订单，共查询count次
            while (count-- > 0)
            {
                var queryResult = await Query(out_trade_no, (WxPayConfig)order.Config, miniProgramPay);

                //如果需要继续查询，则等待2s后继续
                if (queryResult.Status == 2)
                {
                    Logger.Info("Mircopay in progress, continue");
                    await Task.Delay(interval);
                    continue;
                }
                //查询成功,返回订单查询接口返回的数据
                else if (queryResult.Status == 1)
                {
                    Logger.Info("Mircopay success, return order query result : " + queryResult.PayData.ToXml());
                    return queryResult.PayData;
                }
                //订单交易失败，直接返回刷卡支付接口返回的结果，失败原因会在err_code中描述
                else
                {
                    Logger.Error("Micropay failure, return micropay result : " + queryResult.PayData.ToXml());
                    //return result;
                }
            }
            return result;

            ////确认失败，则撤销订单
            //Log.Error("MicroPay", "Micropay failure, Reverse order is processing...");
            //if(!Cancel(out_trade_no))
            //{
            //    Log.Error("MicroPay", "Reverse order failure");
            //    throw new WxPayException("Reverse order failure！");
            //}
        }

        /**
	    * 
	    * 查询订单情况
	    * @param string out_trade_no  商户订单号
	    * @param int succCode         查询订单结果：0表示订单不成功，1表示订单成功，2表示继续查询
	    * @return 订单查询接口返回的数据，参见协议接口
	    */
        public static async Task<WxPayDataWithResult> Query(string out_trade_no, WxPayConfig config, bool miniProgramPay = false)
	    {
            var wxPayDataWithResult = new WxPayDataWithResult();
            WxPayData queryOrderInput = new WxPayData();
            wxPayDataWithResult.PayData = queryOrderInput;

            queryOrderInput.SetValue("out_trade_no",out_trade_no);
		    WxPayData result = await WxPayApi.OrderQuery(queryOrderInput, config, miniProgramPay);
		
		    if(result.GetValue("return_code").ToString() == "SUCCESS" 
			    && result.GetValue("result_code").ToString() == "SUCCESS")
		    {
			    //支付成功
			    if(result.GetValue("trade_state").ToString() == "SUCCESS")
                {
                    wxPayDataWithResult.Status = 1;
			   	    return wxPayDataWithResult;
			    }
			    //用户支付中，需要继续查询
			    else if(result.GetValue("trade_state").ToString() == "USERPAYING")
                {
                    wxPayDataWithResult.Status = 2;
				    return wxPayDataWithResult;
			    }
		    }
		
		    //如果返回错误码为“此交易订单号不存在”则直接认定失败
            if (result.IsSet("err_code") && (result.GetValue("err_code").ToString() == "ORDERNOTEXIST"))
		    {
                wxPayDataWithResult.Status = 0;
		    } 
            else
            {
                //如果是系统错误，则后续继续
                wxPayDataWithResult.Status = 2;
		    }
		    return wxPayDataWithResult;
	    }
	

	    /**
	    * 
	    * 撤销订单，如果失败会重复调用10次
	    * @param string out_trade_no 商户订单号
	    * @param depth 调用次数，这里用递归深度表示
        * @return false表示撤销失败，true表示撤销成功
	    */
	    public static async Task<bool> Cancel(string out_trade_no, WxPayConfig config, X509Certificate2 certification, int depth = 0)
	    {
		    if(depth > 10)
            {
			    return false;
		    }
		
		    WxPayData reverseInput = new WxPayData();
		    reverseInput.SetValue("out_trade_no",out_trade_no);
		    WxPayData result = await WxPayApi.Reverse(reverseInput, config, certification);
		
		    //接口调用失败
		    if(result.GetValue("return_code").ToString() != "SUCCESS")
            {
			    return false;
		    }

            //如果结果为success且不需要重新调用撤销，则表示撤销成功
            if (result.GetValue("result_code").ToString() == "SUCCESS" && result.GetValue("recall").ToString() == "N")
            {
                return true;
            }
            else if (result.GetValue("recall").ToString() == "Y")
            {
                return await Cancel(out_trade_no, config, certification, ++depth);
            }
            //用户支付中，2s后再次发起撤销
            else if (result.GetValue("err_code").ToString() == "USERPAYING")
            {
                await Task.Delay(2 * 1000);
                return await Cancel(out_trade_no, config, certification,  ++depth);
            }
            return false;
	    }

        /**
        * 统一下单业务流程逻辑
        * @param body 商品描述
        * @param total_fee 总金额
        * @param bill_number 商户订单号
        * @param trade_status 支付授权码
        * @throws WxPayException
        * @return 统一下单返回二维码连接结果
        */
        public static async Task<WxPayData> UnifiedOrder(ElectronicOrderModel order)
        {
            //string body, string total_fee, string bill_number, string auth_code

            //set the default trade_status to PAYERROR
            order.TradeStatus = TradeStatus.PAYERROR;

            WxPayData data = new WxPayData();
            data.SetValue("body", order.Title);//商品描述
            data.SetValue("total_fee", Convert.ToInt32(order.NetAmount * 100));//总金额
            if (order.Optional.ContainsKey("mobilePayId"))
            {
                data.SetValue("trade_type", "JSAPI");//交易类型
                data.SetValue("openid", order.Optional["mobilePayId"]);
            } 
            else
            {
                data.SetValue("trade_type", "NATIVE");//交易类型
            }
           
            data.SetValue("out_trade_no", order.BillNumber);//商户订单号
            data.SetValue("product_id", "1");
            data.SetValue("time_expire", DateTime.Now.AddMinutes(1).ToString("yyyyMMddHHmmss"));

            WxPayData result = await WxPayApi.UnifiedOrder(data, (WxPayConfig)order.Config); //提交被扫支付，接收返回结果

            //如果提交被扫支付接口调用失败，则抛异常
            if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL")
            {
                string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToString() : "";
                Log.Error("UnifiedOrder", "UnifiedOrder API interface call failure, result : " + result.ToXml());
                throw new WxPayException("UnifiedOrder API interface call failure, return_msg : " + returnMsg);
            }

            //签名验证
            result.CheckSign((WxPayConfig)order.Config);
            Log.Debug("UnifiedOrder", "UnifiedOrder response check sign success");

            //统一下单成功
            if (result.GetValue("return_code").ToString() == "SUCCESS" &&
                result.GetValue("result_code").ToString() == "SUCCESS")
            {
                Log.Debug("UnifiedOrder", "UnifiedOrder business success, result : " + result.ToXml());
                order.TradeStatus = TradeStatus.SUCCESS;
                return result;
            }

            /******************************************************************
             * 剩下的都是接口调用成功，业务失败的情况
             * ****************************************************************/
            //1）业务结果明确失败
            if (result.GetValue("err_code").ToString() != "USERPAYING" &&
            result.GetValue("err_code").ToString() != "SYSTEMERROR")
            {
                Log.Error("UnifiedOrder", "UnifiedOrder API interface call success, business failure, result : " + result.ToXml());
                return result;
            }

            return result;
        }
    }
}