package com.sonic.lion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.sonic.lion.dao.PayConfigDao;
import com.sonic.lion.domain.entity.PayConfig;
import com.sonic.lion.domain.output.EnabledPayChannelOutput;
import com.sonic.lion.enums.PayChannel;
import com.sonic.lion.service.PayConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 支付配置
 */
@Service
public class PayConfigServiceImpl implements PayConfigService {

    @Autowired
    private PayConfigDao payConfigDao;

    @Override
    public String getByKey(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        LambdaQueryWrapper<PayConfig> queryWrapper = Wrappers.<PayConfig>lambdaQuery()
                .eq(PayConfig::getKey, key);
        return payConfigDao.selectOne(queryWrapper).getValue();
    }

    @Override
    public String getGoogleFeeRatio(){
        return getByKey("googleFeeRatio");
    }

    @Override
    public String getIOSFeeRatio() {
        return getByKey("iosFeeRatio");
    }

    @Override
    public List<String> getEnabledPayChannel() {
        return Lists.newArrayList(getByKey("enabledPayChannel").split(","));
    }

    @Override
    public List<String> getAllPayChannelDebug() {
       return Lists.newArrayList(PayChannel.BUFF.name(), PayChannel.STRIPE.name());
    }

    @Override
    public List<String> getEnabledPayOutChannel() {
        if(StringUtils.isBlank(getByKey("getEnabledPayOutChannel"))){
            return new ArrayList<>();
        }
        return Arrays.asList(getByKey("getEnabledPayOutChannel").split(","));
    }

    @Override
    public boolean paymentChannelSwitchCheckPass(Long currentUserId, PayChannel payChannel) {
        List<String> configList = getEnabledPayChannel();
        //当前发起提现的渠道在配置中
        if(!CollectionUtils.isEmpty(configList) && configList.contains(payChannel.name())) {
            return true;
        }
        //判断当前访问用户是否是特殊测试账号
        List<String> debugUserIds = getDebugPayUserIds();
        if(!CollectionUtils.isEmpty(debugUserIds) && debugUserIds.contains(currentUserId.toString())) {
            return true;
        }
        return false;
    }

    @Override
    public boolean payoutChannelSwitchCheckPass(Long currentUserId, PayChannel payChannel) {
        List<String> configList = getEnabledPayOutChannel();
        //当前发起提现的渠道在配置中
        if(!CollectionUtils.isEmpty(configList) && configList.contains(payChannel.name())) {
            return true;
        }
        //判断当前访问用户是否是特殊测试账号
        List<String> debugUserIds = getDebugPayUserIds();
        if(!CollectionUtils.isEmpty(debugUserIds) && debugUserIds.contains(currentUserId.toString())) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> getDebugPayUserIds() {
        return Arrays.asList(getByKey("debugPayUserIds").split(","));
    }

    @Override
    public boolean enableFirstOrderReward() {
        return Boolean.parseBoolean(getByKey("enableFirstOrderReward"));
    }


    @Override
    public boolean enableEveryFridayReward() {
        return Boolean.parseBoolean(getByKey("enableEveryFridayReward"));
    }

    @Override
    public boolean enableWithdrawFeeReduction() {
        return Boolean.parseBoolean(getByKey("enableWithdrawFeeReduction"));
    }

    @Override
    public LocalDateTime withdrawFeeReductionEndTime() {
        String withdrawFeeReductionEndTime = getByKey("withdrawFeeReductionEndTime");
        if (StringUtils.isNotEmpty(withdrawFeeReductionEndTime)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return LocalDateTime.parse(withdrawFeeReductionEndTime, formatter);
        }
        return null;
    }

    @Override
    public EnabledPayChannelOutput enabledPayChannel(Long userId) {
        List<String> debugUserIds = getDebugPayUserIds();
        List<String> channels;
        if (debugUserIds != null && debugUserIds.contains(userId.toString())) {
            channels = getAllPayChannelDebug();
        } else {
            channels = getEnabledPayChannel();
        }
        EnabledPayChannelOutput output = new EnabledPayChannelOutput();
        output.setChannels(channels);
        return output;
    }
}
