package com.fdkankan.ucenter.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.ucenter.entity.*;
import com.fdkankan.ucenter.httpClient.vo.PayOrderVo;
import com.fdkankan.ucenter.pay.paypal.sdk.AutoPaypalVo;
import com.fdkankan.ucenter.pay.strategy.impl.DownloadOrderImpl;
import com.fdkankan.ucenter.pay.strategy.impl.EntityOrderImpl;
import com.fdkankan.ucenter.pay.strategy.impl.IncrementOrderImpl;
import com.fdkankan.ucenter.service.*;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Component
public class AutoOrderConsumer {


    @Autowired
    IUserIncrementService userIncrementService;
    @Autowired
    IMailTemplateService mailTemplateService;
    @Autowired
    IUserService userService;
    @Autowired
    IncrementOrderImpl incrementOrderImpl;
    @Autowired
    IIncrementAutoOrderService autoOrderService;


    @RabbitListener(
            queuesToDeclare = @Queue("${queue.pay.auto-paypal:pay-auto-paypal}")
            ,concurrency = "1"
    )
    public void consumerQueue(Channel channel, Message message)  {
        try {
            String messageId = message.getMessageProperties().getMessageId();
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
            log.info("autoPaypal-result-mq--messageId:{},msg:{}",messageId,msg);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

            AutoPaypalVo order = JSONObject.parseObject(msg, AutoPaypalVo.class);
            if(order == null){
                return;
            }
            IncrementAutoOrder incrementAutoOrder = autoOrderService.getByOrderSn(order.getSubscriptionOrderSn());
            if(incrementAutoOrder == null){
                incrementAutoOrder = autoOrderService.getDelOrder(order.getSubscriptionOrderSn());
                if(incrementAutoOrder != null){
                    autoOrderService.updateDelStatus(incrementAutoOrder.getId());
                }
            }
            if(incrementAutoOrder == null){
                return;
            }
            if(incrementAutoOrder.getSubscriptionId() == null){
                autoOrderService.subOrder(order.getSubscriptionOrderSn(),order.getSubscriptionId());
            }
            if(order.getPayType() == null || order.getPayType() == 5){
                order.setPayType(2);
            }
            switch (order.getEventType()){
                case "PAYMENT.SALE.COMPLETED" :     //每日扣款
                    //Enum: "completed" "partially_refunded" "pending" "refunded" "denied"
                    switch (order.getState()){
                        case "completed" :      //扣款完成
                            autoOrderService.paySuccess(incrementAutoOrder,order);
                            break;
                        case "partially_refunded" :
                            break;
                        case "pending" :        //等待
                            break;
                        case "refunded" :       //退还
                            break;
                        case "denied" :         //支付失败
                            autoOrderService.payError(incrementAutoOrder,order);
                            break;
                    }
                    break;
                case "BILLING.SUBSCRIPTION.CANCELLED" :     //取消订阅
                    autoOrderService.subscriptionCancelled(incrementAutoOrder,order);
                    break;
                case "BILLING.SUBSCRIPTION.PAYMENT.FAILED" :     //订阅付款失败
                    autoOrderService.payError(incrementAutoOrder,order);
                    break;
                case "BILLING.SUBSCRIPTION.SUSPENDED" :     //订阅暂停
                    List<UserIncrement> userIncrementList = userIncrementService.getByAutoOrderSn(order.getSubscriptionId());
                    for (UserIncrement userIncrement : userIncrementList) {
                        userIncrementService.cancelSubscription(userIncrement.getId());
                    }
                    break;
                default:
                    break;
            }


        }catch (Exception e){
            log.info("order-payResult----消费失败",e);
        }finally {

        }

    }

}
