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

import com.github.microservice.auth.client.service.UserFaceService;
import com.github.microservice.auth.client.service.UserService;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.forgotPassword.ForgotPasswordParam;
import com.zhongshu.card.client.model.forgotPassword.ValidateUserModel;
import com.zhongshu.card.client.model.forgotPassword.ValidateUserParam;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.domain.org.UserAccount;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectCommonConfig;
import com.zhongshu.card.server.core.service.base.RedisService;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.projectAbout.ProjectCommonConfigService;
import com.zhongshu.card.server.core.util.CommonUtil;
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.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author TRX
 * @date 2025/3/26
 */
@Service
@Slf4j
public class ForgotPassWordService extends SuperService {

    @Autowired
    private UserCountDao userCountDao;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ProjectCommonConfigService projectCommonConfigService;

    @Autowired
    private UserService userService;

    /**
     * 人脸管理类
     */
    @Autowired
    private UserFaceService userFaceService;

    // 随机key存储的时间 10 分钟
    private final Long keyTime = 60 * 10L;

    /**
     * 通过手机号码和身份证验证用户是否存在
     * @param param
     * @return
     */
    public ResultContent<ValidateUserModel> verifyUserExitByPhone(ValidateUserParam param) {
        ValidateUserModel model = new ValidateUserModel();
        if (StringUtils.isEmpty(param.getPhone())) {
            return ResultContent.buildFail("phone is empty");
        }
        if (StringUtils.isEmpty(param.getCardNumber())) {
            return ResultContent.buildFail("cardNumber is empty");
        }
        UserAccount userAccount = userCountDao.findTopByPhoneAndCardNumber(param.getPhone(), param.getCardNumber());
        if (ObjectUtils.isEmpty(userAccount)) {
            return ResultContent.buildFail("用户不存在");
        }
        String uuid = CommonUtil.UUID();
        String userId = userAccount.getUserId();
        String name = CommonUtil.turnNameDesen(userAccount.getName());

        model.setId(uuid);
        model.setUserName(name);
        redisService.setValueSecond(uuid, userId, keyTime);
        return ResultContent.buildSuccess(model);
    }

    public ResultContent<ValidateUserModel> verifyUserExitByImage(MultipartFile file) {
        ValidateUserModel model = new ValidateUserModel();
        com.github.microservice.auth.client.content.ResultContent<String> resultContent = userFaceService.getMatchesKey(file);
        if (resultContent.isFailed()) {
            return ResultContent.buildFail(resultContent.getMsg());
        }

        String userId = resultContent.getContent();
        UserAccount userAccount = userCountDao.findTopByUserId(userId);
        if (ObjectUtils.isEmpty(userAccount)) {
            return ResultContent.buildFail("用户不存在");
        }
        String uuid = CommonUtil.UUID();
        String name = userAccount.getName();
        name = CommonUtil.turnNameDesen(name);
        model.setId(uuid);
        model.setUserName(name);
        redisService.setValueSecond(uuid, userId, keyTime);
        return ResultContent.buildSuccess(model);
    }


    public ResultContent resetPassword(ForgotPasswordParam param) {
        String id = param.getId();
        String password = param.getPassword();
        if (StringUtils.isEmpty(id)) {
            return ResultContent.buildFail("id is empty");
        }
        if (StringUtils.isEmpty(password)) {
            return ResultContent.buildFail("password is empty");
        }
        String userId = redisService.getValue(id);
        if (StringUtils.isEmpty(userId)) {
            return ResultContent.buildFail("操作超时，请重新再试！");
        }

        String projectOid = getCurrentProjectOid();
        ResultContent verifyResultContent = verifyPassWordIsStandard(projectOid, password);
        if (verifyResultContent.isFailed()) {
            return ResultContent.buildFail(verifyResultContent.getMsg());
        }

        com.github.microservice.auth.client.content.ResultContent<Void> resultContent = userService.updateLoginPassword(
                userId, password);
        if (resultContent.isFailed()) {
            return ResultContent.buildFail(resultContent.getMsg());
        }
        redisService.removeValue(id);
        return ResultContent.buildSuccess();
    }

    /**
     * 验证密码是否符合规范
     * @param projectOid
     * @param password
     * @return
     */

    public ResultContent verifyPassWordIsStandard(String projectOid, String password) {
        ProjectCommonConfig projectCommonConfig = null;
        if (StringUtils.isNotEmpty(projectOid)) {
            ResultContent<ProjectCommonConfig> resultContent = projectCommonConfigService.checkCommonConfig(projectOid);
            if (resultContent.isSuccess()) {
                projectCommonConfig = resultContent.getContent();
            }
        }
        if (projectCommonConfig != null) {
            ValidateResult passResult = ValidateUtils.validatePassWord(password, projectCommonConfig.getPassWordType());
            if (!passResult.isSuccess()) {
                return ResultContent.buildFail(passResult.getMsg());
            }
        } else {
            // 基本的密码验证
            ValidateResult passResult = ValidateUtils.validatePassWord(password);
            if (!passResult.isSuccess()) {
                return ResultContent.buildFail(passResult.getMsg());
            }
        }
        return ResultContent.buildSuccess();
    }
}
