package iot.lidee.framework.web.service;

import com.alibaba.fastjson2.JSONObject;
import iot.lidee.common.constant.CacheConstants;
import iot.lidee.common.constant.Constants;
import iot.lidee.common.constant.LideeConstant;
import iot.lidee.common.core.cache.CacheHelper;
import iot.lidee.common.core.domain.entity.SysDept;
import iot.lidee.common.core.domain.entity.SysUser;
import iot.lidee.common.core.domain.model.LideeUserDto;
import iot.lidee.common.core.domain.model.LoginUser;
import iot.lidee.common.core.redis.RedisCache;
import iot.lidee.common.enums.UserStatus;
import iot.lidee.common.exception.ServiceException;
import iot.lidee.common.exception.user.CaptchaException;
import iot.lidee.common.exception.user.CaptchaExpireException;
import iot.lidee.common.exception.user.UserPasswordNotMatchException;
import iot.lidee.common.utils.DateUtils;
import iot.lidee.common.utils.MessageUtils;
import iot.lidee.common.utils.ServletUtils;
import iot.lidee.common.utils.StringUtils;
import iot.lidee.common.utils.http.HttpUtils;
import iot.lidee.common.utils.ip.IpUtils;
import iot.lidee.framework.manager.AsyncManager;
import iot.lidee.framework.manager.factory.AsyncFactory;
import iot.lidee.framework.security.context.AuthenticationContextHolder;
import iot.lidee.system.service.ISysConfigService;
import iot.lidee.system.service.ISysDeptService;
import iot.lidee.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Slf4j
@Component
public class SysLoginService {
    @Autowired
    private TokenService tokenService;

    @Resource
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private UserDetailsServiceImpl userDetailsServiceImpl;

    @Resource
    private SysPasswordService passwordService;
    @Resource
    private ISysDeptService sysDeptService;

    @Resource
    private SysPermissionService permissionService;

    @Resource
    private CacheHelper cacheHelper;


    @Value("${cas.loginurl:}")
    private  String casLoginUrl;
    @Value("${cas.outlogouturl:}")
    private  String casoutlogouturl;

    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public String login(String username, String password, String code, String uuid, Integer sourceType, String language) {
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        // 验证码开关
        if (captchaEnabled) {
            validateCaptcha(username, code, uuid);
        }
        // 用户验证
        Authentication authentication = null;
        try {
            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
            AuthenticationContextHolder.setContext(authenticationToken);
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager.authenticate(authenticationToken);
        } catch (Exception e) {
            if (e instanceof BadCredentialsException) {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
                throw new UserPasswordNotMatchException();
            } else {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
                throw new ServiceException(e.getMessage());
            }
        } finally {
            AuthenticationContextHolder.clearContext();
        }
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // 移动端、小程序限制终端用户登录
        Long deptId = loginUser.getDeptId();
        if (null != sourceType && 1 == sourceType && null == deptId) {
            throw new ServiceException("web端只允许租户登录！");
        }
//        if (!"admin".equals(loginUser.getUsername()) && null != sourceType) {
//            Long deptId = loginUser.getDeptId();
//            if (1 == sourceType && null == deptId) {
//                throw new ServiceException("web端只允许租户登录！");
//            }
//            if (1 != sourceType && null != deptId) {
//                throw new ServiceException("只允许终端用户登录！");
//            }
//        }
        SysDept sysDept = sysDeptService.selectDeptById(deptId);
        recordLoginInfo(loginUser.getUserId());
        loginUser.setLanguage(language);
        if (null != sysDept) {
            loginUser.setDeptUserId(sysDept.getDeptUserId());
        }
        // 生成token
        return tokenService.createToken(loginUser);
    }

    /**
     * 第三方验证后，调用登录方法
     *
     * @param username 用户名
     * @param password 密码
     * @return token
     */
    public String socialLogin(String username, String password, String language) {
        // 用户验证
        Authentication authentication = null;
        try {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } catch (Exception e) {
            if (e instanceof BadCredentialsException) {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
                throw new UserPasswordNotMatchException();
            } else {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
                throw new ServiceException(e.getMessage());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        recordLoginInfo(loginUser.getUserId());
        loginUser.setLanguage(language);
        // 生成token
        return tokenService.createToken(loginUser);
    }

    /**
     * 三方跳转登录认证方法
     *
     * @param username  系统用户名
     * @param encodePwd 系统用户密码
     * @return
     */
    public String redirectLogin(String username, String encodePwd, String language) {
//        UserDetails userDetails=userDetailsServiceImpl.loadUserByUsername(username);
        SysUser user = userService.selectUserByUserName(username);
        if (StringUtils.isNull(user)) {
            throw new ServiceException("登录用户：" + username + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        }
        // 重写验证方法
        passwordService.socialValidate(user, encodePwd);
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        UserDetails userDetails = userDetailsServiceImpl.createLoginUser(user);
        LoginUser loginUser = (LoginUser) userDetails;
        recordLoginInfo(loginUser.getUserId());
        loginUser.setLanguage(language);
        // 生成token
        return tokenService.createToken(loginUser);

    }

    /**
     * 组态分享页面生成token
     *
     * @param username 系统用户名
     * @return
     */
    public String shareUserLogin(String username, String language) {
        SysUser user = userService.selectUserByUserName(username);
        if (StringUtils.isNull(user)) {
            throw new ServiceException("分享用户：" + username + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            throw new ServiceException("对不起，您的分享账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            throw new ServiceException("对不起，您的分享账号：" + username + " 已停用");
        }
        // 重写验证方法
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        UserDetails userDetails = userDetailsServiceImpl.createLoginUser(user);
        LoginUser loginUser = (LoginUser) userDetails;
        recordLoginInfo(loginUser.getUserId());
        loginUser.setLanguage(language);
        loginUser.setNeverExpire(Boolean.TRUE);
        // 生成token
        return tokenService.createToken(loginUser);

    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid) {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha)) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
            throw new CaptchaException();
        }
    }

    /**
     * 记录登录信息
     *
     * @param userId 用户ID
     */
    public void recordLoginInfo(Long userId) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        sysUser.setLoginDate(DateUtils.getNowDate());
        userService.updateUserProfile(sysUser);
    }

    public String ssoLogin(String username, String password, String language) {
        SysUser user = userService.selectUserByUserName(username);
        if (StringUtils.isNull(user)) {
            throw new ServiceException("登录用户：" + username + " 不存在");
        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        }
        // 重写验证方法
        passwordService.ssoValidate(user, password);
        AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        UserDetails userDetails = userDetailsServiceImpl.createLoginUser(user);
        LoginUser loginUser = (LoginUser) userDetails;
        recordLoginInfo(loginUser.getUserId());
        loginUser.setLanguage(language);
        // 生成token
        return tokenService.createToken(loginUser);
    }

    public String logincas(LideeUserDto dto) {

        String loginName = validateCasToken(dto.getLoginName());

        if (StringUtils.isNull(loginName)) {
            throw new ServiceException("该用户不存在!");
        }

        SysUser accessUser = userService.selectUserByUserName(loginName);
        if (null == accessUser) {
            throw new ServiceException("该用户不存在!");
        }

        Long deptId = accessUser.getDeptId();
        SysDept sysDept = null;
        if (deptId != null) {
            sysDept = sysDeptService.selectDeptById(deptId);
        }

        LoginUser loginUser = new LoginUser(
                accessUser.getUserId(),
                deptId,
                null,
                accessUser,
                permissionService.getMenuPermission(accessUser)
        );

        if (sysDept != null) {
            loginUser.setDeptUserId(sysDept.getDeptUserId());
        }

        redisCache.setCacheObject(loginName + "_cas", dto.getLoginName());

        recordLoginInfo(loginUser.getUserId());

        AsyncManager.me().execute(AsyncFactory.recordLogininfor(
                accessUser.getUserName(),
                Constants.LOGIN_SUCCESS,
                MessageUtils.message("user.login.success")
        ));

        return tokenService.createToken(loginUser);
    }

    private String validateCasToken(String refreshToken) {
        String url = casLoginUrl + "?refreshToken=" + refreshToken;
        String response = HttpUtils.sendPost(url, refreshToken);

        if (StringUtils.isEmpty(response)) {
            return "";
        }

        try {
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (jsonObject != null && jsonObject.getInteger("code") == 0) {
                JSONObject data = jsonObject.getJSONObject("data");
                if (data != null) {
                    return data.getString("username");
                }
            }
        } catch (Exception e) {
            SysLoginService.log.error("CAS 验证失败", e);
        }

        return "";
    }

    public void outlogcas(LideeUserDto dto) {
        String token=  redisCache.getCacheObject(dto.getLoginName()+"_cas");
        String url = casoutlogouturl+"?refreshToken="+ token;
        String response = HttpUtils.sendPost(url, token);
    }
}
