package com.jeequan.jeepay.thirdparty.channel.lklspay;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.PayOrder;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.rqrs.msg.ChannelRetMsg;
import com.jeequan.jeepay.thirdparty.channel.AbstractChannelNoticeService;
import com.jeequan.jeepay.thirdparty.channel.kqpay.KqpayKit;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LKlCardType;
import com.jeequan.jeepay.thirdparty.channel.lklspay.model.LklTradeStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.MutablePair;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 拉卡拉通知 回调接口实现类
 *
 * @author crystal
 */
@Service
@Slf4j
public class LklspayChannelNoticeService extends AbstractChannelNoticeService {

    @Override
    public String getIfCode() {
        return CS.IF_CODE.LKLSPAY;
    }

    @Override
    public MutablePair<String, Object> parseParams(HttpServletRequest request, String urlOrderId, NoticeTypeEnum noticeTypeEnum) {
        JSONObject reqParamJSON = getReqParamJSON();
        String parOrderId = reqParamJSON.getString("out_trade_no");
        return MutablePair.of(parOrderId, reqParamJSON);
    }

    @Override
    public ChannelRetMsg doNotice(HttpServletRequest request, Object params, PayOrder payOrder, MchAppConfigContext mchAppConfigContext, NoticeTypeEnum noticeTypeEnum) {
        JSONObject respJson = (JSONObject) JSONObject.toJSON(params);
        log.info("拉卡拉回调参数：{}",respJson);
        //验签成功后判断上游订单状态
        ResponseEntity okResponse = jsonResp(LklspayKit.ok());
        ChannelRetMsg result = new ChannelRetMsg();
        result.setResponseEntity(okResponse);
        result.setChannelBizData(respJson);
//        result.setChannelOriginResponse(respJson.toString());
        result.setPlatformMchOrderNo(respJson.getString("trade_no"));
        result.setChannelOrderId(respJson.getString("log_no"));
        result.setPlatformOrderNo(respJson.getString("acc_trade_no"));
        result.setChannelUserId(respJson.getString("user_id2"));
        LklTradeStatus tradeStatus = LklTradeStatus.getVal(respJson.getString("trade_status"));
        LKlCardType card_type = LKlCardType.getVal(respJson.getString("card_type"));
        String drType = card_type.getType();
        switch (card_type){
            case LKL_ACCOUNT:
            case ALI_OTHER:
                drType = CS.DrType.OTHER.getType();
                break;
            case DECP:
                drType = CS.DrType.DCEP.getType();
                break;
        }
        switch (tradeStatus){
            case FAIL:
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_FAIL);
                break;
            case UNKNOWN:
                result.setChannelState(ChannelRetMsg.ChannelState.UNKNOWN);
                break;
            default:
                result.setChannelState(ChannelRetMsg.ChannelState.CONFIRM_SUCCESS);
                break;
        }
        result.setDrType(drType);
        return result;
    }


    /**
     * 验证银盛支付通知参数
     * @return
     */
    public boolean verifyParams(JSONObject jsonParams, MchAppConfigContext mchAppConfigContext) {
        try {
            return KqpayKit.checkResultNotify(jsonParams,mchAppConfigContext);
        }catch (Exception e) {
            log.error("验证签名异常", e);
            return false;
        }
    }
}
