package com.zhongshu.card.server.core.service.payment;

import com.github.microservice.auth.client.content.ResultState;
import com.github.microservice.auth.client.service.UserService;
import com.zhongshu.card.client.model.payment.PaymentSettingMaxParam;
import com.zhongshu.card.client.model.payment.PaymentSettingModel;
import com.zhongshu.card.client.model.payment.PaymentSettingPasswordParam;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.server.core.dao.payment.PaymentSettingDao;
import com.zhongshu.card.server.core.domain.payment.PaymentSetting;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import com.zhongshu.card.server.core.util.ValidateResult;
import com.zhongshu.card.server.core.util.ValidateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;

/**
 * @author TRX
 * @date 2024/6/26
 */
@Slf4j
@Service
@Primary
public class PaymentSettingService extends SuperService {

    @Autowired
    PaymentSettingDao paymentSettingDao;

    @Autowired
    UserService userService;

    /**
     * 设置支付密码
     *
     * @param param
     * @return
     */
    public ResultContent settingPayPassword(PaymentSettingPasswordParam param) {
        String userId = getCurrentUserId();
        Assert.hasText(param.getLogInPassWord(), "登录密码不能为空");
        Assert.hasText(param.getPassWord(), "密码不能为空");
        com.github.microservice.auth.client.content.ResultContent<Void> resultContent = userService.checkLoginPassword(userId, param.getConfirmPassWord());
        if (resultContent.getState() == ResultState.Fail) {
            return ResultContent.buildFail(resultContent.getMsg());
        }
        if (!param.getPassWord().equals(param.getConfirmPassWord())) {
            return ResultContent.buildFail("密码和确认密码不相等");
        }
        ValidateResult validateResult = ValidateUtils.validatePassWord(param.getPassWord());
        if (!validateResult.isSuccess()) {
            return ResultContent.buildFail(validateResult.getMsg());
        }
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        PaymentSetting paymentSetting = paymentSettingDao.findTopByUserId(userId);
        if (ObjectUtils.isEmpty(paymentSetting)) {
            paymentSetting = new PaymentSetting();
            paymentSetting.setUserId(userId);
            paymentSetting.setUserAccount(getCurrentUserAccount());
        }
        String psd = makePassWord(userId, param.getPassWord());
        paymentSetting.setPayPassWord(passwordEncoder.encode(psd));

        paymentSettingDao.save(paymentSetting);
        return ResultContent.buildSuccess();
    }

    /**
     * 置空支付密码
     *
     * @return
     */
    public ResultContent resetPayPassword(String logInPassWord) {
        String userId = getCurrentUserId();
        com.github.microservice.auth.client.content.ResultContent<Void> resultContent = userService.checkLoginPassword(userId, logInPassWord);
        if (resultContent.getState() == ResultState.Fail) {
            return ResultContent.buildFail(resultContent.getMsg());
        }

        PaymentSetting paymentSetting = paymentSettingDao.findTopByUserId(userId);
        if (ObjectUtils.isEmpty(paymentSetting)) {
            paymentSetting = new PaymentSetting();
            paymentSetting.setUserId(userId);
            paymentSetting.setUserAccount(getCurrentUserAccount());
        }
        paymentSetting.setPayPassWord("");
        paymentSettingDao.save(paymentSetting);
        return ResultContent.buildSuccess();
    }

    /**
     * 验证支付密码
     *
     * @param userId
     * @param password
     * @return
     */
    public boolean checkPayPassword(String userId, String password) {
        boolean b = true;
        PaymentSetting paymentSetting = paymentSettingDao.findTopByUserId(userId);
        if (ObjectUtils.isNotEmpty(paymentSetting)) {
            if (StringUtils.isNotEmpty(paymentSetting.getPayPassWord())) {
                PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                String psd = makePassWord(userId, password);
                if (passwordEncoder.matches(paymentSetting.getPayPassWord(), psd)) {
                    return true;
                }
            }
        }
        return b;
    }

    /**
     * 组装密码
     *
     * @param userId
     * @param password
     * @return
     */
    private String makePassWord(String userId, String password) {
        return userId + password;
    }

    public ResultContent settingMaxPay(PaymentSettingMaxParam param) {
        String userId = getCurrentUserId();
        BigDecimal maxPaymentAmount = param.getMaxPaymentAmount();
        if (maxPaymentAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return ResultContent.buildFail("金额不能小于o");
        }
        PaymentSetting paymentSetting = paymentSettingDao.findTopByUserId(userId);
        if (ObjectUtils.isEmpty(paymentSetting)) {
            paymentSetting = new PaymentSetting();
            paymentSetting.setUserId(userId);
            paymentSetting.setUserAccount(getCurrentUserAccount());
        }
        paymentSetting.setMaxPaymentAmount(maxPaymentAmount);
        paymentSettingDao.save(paymentSetting);
        return ResultContent.buildSuccess();
    }

    public ResultContent<PaymentSettingModel> getSettingDetail() {
        String userId = getCurrentUserId();
        return ResultContent.buildSuccess(paymentSettingDao.findTopByUserId(userId));
    }

    public PaymentSettingModel getByUserId(String userId) {
        PaymentSetting paymentSetting = paymentSettingDao.findTopByUserId(userId);
        return toModel(paymentSetting);
    }

    public PaymentSettingModel toModel(PaymentSetting entity) {
        PaymentSettingModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new PaymentSettingModel();
            BeanUtils.copyProperties(entity, model);
            if (StringUtils.isNotEmpty(entity.getPayPassWord())) {
                model.setIsSettingPassword(Boolean.TRUE);
            }
        }
        return model;
    }

}
