package com.izouma.nineth.service.pay;

import com.izouma.nineth.config.GeneralProperties;
import com.izouma.nineth.config.SysConstants;
import com.izouma.nineth.domain.Order;
import com.izouma.nineth.domain.PaymentInfo;
import com.izouma.nineth.domain.Project;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.ProjectRepo;
import com.izouma.nineth.service.SysConfigService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.hibernate.type.descriptor.java.LocaleTypeDescriptor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 辅助
 */
@AllArgsConstructor
@Service
public class PayHelper {

    public static final String GIFT_GOOGS_TITLES = "转赠GAS费";

    private GeneralProperties generalProperties;

    private ProjectRepo projectRepo;

    private SysConfigService sysConfigService;

    /**
     * 订单详情地址
     * @param order
     * @return
     */
    public String orderDetailUrl(Order order){
        String orderDetail = generalProperties.getHost() + "/";

        Project project = projectRepo.findById((long)order.getProjectId()).orElse(null);
        if (project != null) {
            String projectAddr = StringUtils.removeEnd(project.getFrontAddr(), "/");
            projectAddr = StringUtils.removeEnd(projectAddr, "/home");
            orderDetail = projectAddr + "/orderDetail?id=" + order.getId();
        }

        return orderDetail;
    }

    /**
     * 分析返回地址
     * @return
     */
    public String resolveReturnUrl(PaymentInfo paymentInfo){
        String returnUrl = generalProperties.getHost() + "/";

        Project project = projectRepo.findById((long)paymentInfo.getProjectId()).orElse(null);
        if (project != null) {
            String projectAddr = StringUtils.removeEnd(project.getFrontAddr(), "/");
            projectAddr = StringUtils.removeEnd(projectAddr, "/home");
            if(paymentInfo.getOrderId() != null){
                returnUrl = projectAddr + "/orderDetail?id=" + paymentInfo.getOrderId();
            }else{
                //默认返回首页
                returnUrl = projectAddr+ "/home";
            }
        }

        return returnUrl;
    }

    /**
     * 分析支付通知地址
     * @param orderNo
     * @return
     */
    public String resolvePayNotifyUrl(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/uniform/payNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 分析退款通知地址
     * @param orderNo
     * @return
     */
    public String resolveRefundNotifyUrl(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/uniform/refundNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 分析转账通知
     * @param tradeNo
     * @return
     */
    public String resolveTransferNotifyUrl(String tradeNo){
        String notifyUrl = generalProperties.getHost() + "/notify/uniform/transferNotify/" + tradeNo;
        return notifyUrl;
    }

    /**
     * 充值结果通知
     * @param orderNo
     * @return
     */
    public String resolveRechargeNotifyUrl(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/rechargeNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 续费结果通知
     * @param orderNo
     * @return
     */
    public String resolveRenewNotifyUrl(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/renewNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 提现结果通知
     * @param orderNo
     * @return
     */
    public String resolveWithdrawNotifyUrl(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/withdrawNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 解绑卡结果通知
     * @param orderNo
     * @return
     */
    public String resolveBindCardNotify(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/bindCardNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 连连账户结果通知
     * @param orderNo
     * @return
     */
    public String resolveLianlianAccountNotifyUrl(String orderNo){
        String notifyUrl = generalProperties.getHost() + "/notify/lianlianAccountNotify/" + orderNo;
        return notifyUrl;
    }

    /**
     * 账户销户结果通知
     * @return
     */
    public String resolveAccountCancelNotify(){
        String notifyUrl = generalProperties.getHost() + "/notify/lianlianAccountCancel";
        return notifyUrl;
    }


    /**
     * 分析返回主页
     * @param projectId
     * @return
     */
    public String resolveHome(int projectId){
        String homeUrl = generalProperties.getHost() + "/";

        Project project = projectRepo.findById((long)projectId).orElse(null);
        if (project != null) {
            String projectAddr = StringUtils.removeEnd(project.getFrontAddr(), "/");
            projectAddr = StringUtils.removeEnd(projectAddr, "/home");
            homeUrl = projectAddr+ "/home";
        }

        return homeUrl;
    }

    /**
     * 分析返回名称
     * @param projectId
     * @return
     */
    public String resolveName(int projectId){
        String wapName = "";
        Project project = projectRepo.findById((long)projectId).orElse(null);
        if (project != null) {
            //使用中文名称
            wapName = project.getChName();
        }
        return wapName;
    }


    /**
     * 订单失效时间间隔
     * @return
     */
    public int getExpireInterval(){
        int interval = sysConfigService.getInt(SysConstants.ORDER_EXPIRE_INTERVAL);
        if(interval <= 3){
            //限定最少3分钟，防止有问题
            interval = 3;
        }

        return interval;
    }


    /**
     * 获取订单失效时间
     * @param orderCreatedTime
     * @return
     */
    public LocalDateTime getExpireTime(LocalDateTime orderCreatedTime){
        //保留一分钟误差，所以减去一分钟,参考杉德通知时间频率：10秒、20秒、1分钟、5分钟
        int expireInterval = getExpireInterval() - 1;
        LocalDateTime expireTime = orderCreatedTime.plusMinutes(expireInterval);
        return expireTime;
    }


    /**
     * 检查订单创建时间
     * @param orderCreatedTime
     * @return
     */
    public LocalDateTime checkExpireTime(LocalDateTime orderCreatedTime){
        LocalDateTime expireTime = getExpireTime(orderCreatedTime);
        if(expireTime.compareTo(LocalDateTime.now()) <= 0){
            throw new BusinessException("订单即将失效，请重新下单");
        }
        return expireTime;
    }




}
