package com.keao.edu.user.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.Adapay;
import com.huifu.adapay.notify.MQTTCallbackHandler;
import com.keao.edu.thirdparty.adapay.ConfigInit;
import com.keao.edu.user.dao.SysConfigDao;
import com.keao.edu.user.entity.ExamRegistrationPayment;
import com.keao.edu.user.enums.TransStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;


@Service
public class NotifyCallback implements MQTTCallbackHandler {

    @Autowired
    private ExamRegistrationPaymentService examRegistrationPaymentService;
    @Autowired
    private SysConfigDao sysConfigDao;


    private static NotifyCallback notifyCallback;

    @PostConstruct
    public void init() {
        notifyCallback = this;
        String baseApiUrl = notifyCallback.sysConfigDao.findConfigValue("base_api_url");
        Adapay.setDeviceID(baseApiUrl);
    }

    /**
     * 用户接收并处理支付成功的异步消息
     *
     * @param payment 成功的支付对象
     * @throws Exception 异常
     */
    @Override
    public void paymentSuccessMessageArrived(String payment) throws Exception {
        System.out.println(String.format("receive paymentSuccess  msg=%s", payment));
        JSONObject dataObj = JSON.parseObject(payment);
        if (!dataObj.getString("app_id").equals(ConfigInit.appId)) {
            return;
        }
        String orderNo = dataObj.getString("order_no");
        ExamRegistrationPayment order = notifyCallback.examRegistrationPaymentService.getByOrderNo(orderNo);
        if (order == null || !order.getTransStatus().equals(TransStatusEnum.ING)) {
            return;
        }
        Map<String,String> rpMap = new HashMap<>();
        rpMap.put("channelType", dataObj.getString("pay_channel"));
        rpMap.put("transStatus","SUCCESS");
        rpMap.put("orderNo",orderNo);
        rpMap.put("transNo",order.getTransNo());
        notifyCallback.examRegistrationPaymentService.updateOrder(rpMap);
    }

    /**
     * 用户接收并处理支付失败的异步消息
     *
     * @param payment 失败的支付对象
     * @throws Exception 异常
     */
    @Override
    public void paymentFailedMessageArrived(String payment) throws Exception {
        System.out.println(String.format("receive paymentFailed msg=%s", payment));
        JSONObject dataObj = JSON.parseObject(payment);
        if (!dataObj.getString("app_id").equals(ConfigInit.appId)) {
            return;
        }
        String orderNo = dataObj.getString("order_no");
        ExamRegistrationPayment order = notifyCallback.examRegistrationPaymentService.getByOrderNo(orderNo);
        if (order == null || !order.getTransStatus().equals("ING")) {
            return;
        }
        Map<String,String> rpMap = new HashMap<>();
        rpMap.put("channelType", dataObj.getString("pay_channel"));
        rpMap.put("transStatus","FAILED");
        rpMap.put("orderNo",orderNo);
        rpMap.put("transNo",order.getTransNo());
        notifyCallback.examRegistrationPaymentService.updateOrder(rpMap);
    }


    /**
     * 用户接收并处理关闭支付交易成功的异步消息
     *
     * @param payment 关闭成功的支付对象
     * @throws Exception 异常
     */
    @Override
    public void paymentCloseSuccessMessageArrived(String payment) throws Exception {
        System.out.println(String.format("receive paymentCloseSuccess msg=%s", payment));
        JSONObject dataObj = JSON.parseObject(payment);
        if (!dataObj.getString("app_id").equals(ConfigInit.appId)) {
            return;
        }
        String orderNo = dataObj.getString("order_no");
        ExamRegistrationPayment order = notifyCallback.examRegistrationPaymentService.getByOrderNo(orderNo);
        if (order == null || !order.getTransStatus().equals(TransStatusEnum.ING)) {
            return;
        }
        Map<String,String> rpMap = new HashMap<>();
        rpMap.put("channelType", dataObj.getString("pay_channel"));
        rpMap.put("transStatus","FAILED");
        rpMap.put("orderNo",orderNo);
        rpMap.put("transNo",order.getTransNo());
        notifyCallback.examRegistrationPaymentService.updateOrder(rpMap);
    }

    /**
     * 用户接收并处理关闭支付交易失败的异步消息
     *
     * @param payment 关闭失败的支付对象
     * @throws Exception 异常
     */
    @Override
    public void paymentCloseFailedMessageArrived(String payment) throws Exception {
        System.out.println(String.format("receive paymentCloseFailed msg=%s", payment));
    }

    @Override
    public void paymentReverseFailedMessageArrived(String payment) throws Exception {

        System.out.println(String.format("receive payment ReverseFailed msg=%s", payment));
    }

    @Override
    public void paymentReverseSuccessMessageArrived(String payment) throws Exception {
        System.out.println(String.format("receive payment ReverseSuccess msg=%s", payment));
    }

    /**
     * 用户接收并处理退款成功的异步消息
     *
     * @param refund 成功的退款对象
     * @throws Exception 异常
     */
    @Override
    public void refundSuccessMessageArrived(String refund) throws Exception {
        System.out.println(String.format("receive refundSuccess msg=%s", refund));
    }

    /**
     * 用户接收并处理退款失败的异步消息
     *
     * @param refund 失败的退款对象
     * @throws Exception 异常
     */
    @Override
    public void refundFailedMessageArrived(String refund) throws Exception {
        System.out.println(String.format("receive refundFailed   msg=%s", refund));
    }

    @Override
    public void corpMemberSuccessMessageArrived(String corpMember) throws Exception {
        System.out.println(String.format("receive corpMember   msg=%s", corpMember));
    }

    @Override
    public void corpMemberFailedMessageArrived(String corpMember) throws Exception {
        System.out.println(String.format("receive corpMember  msg=%s", corpMember));
    }

    @Override
    public void unknowMessageArrived(String message) throws Exception {
        System.out.println(String.format("receive unknow  msg=%s", message));
    }

    @Override
    public void drawCashFailedMessageArrived(String drawCash) throws Exception {
        System.out.println(String.format("receive unknow  msg=%s", drawCash));

    }

    @Override
    public void drawCashSuccessedMessageArrived(String drawCash) throws Exception {

        System.out.println(String.format("receive unknow  msg=%s", drawCash));

    }

    @Override
    public void pagePaymentFailedMessageArrived(String arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void pagePaymentSuccessedMessageArrived(String arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    @Override
    public void connectionLost(String message) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("mqtt...........lost.............");

    }

    @Override
    public void connectSuccess() throws Exception {
        System.out.println("mqtt...........connect.............");
    }
}
