package com.securitycontrol.auth.service;

import com.securitycontrol.common.core.constant.Constant;
import com.securitycontrol.common.core.constant.SecurityConstants;
import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.enums.UserStatus;
import com.securitycontrol.common.core.exception.ServiceException;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.system.api.RemoteUserService;
import com.securitycontrol.system.api.domain.SysUser;
import com.securitycontrol.system.api.domain.decision.SysMenu;
import com.securitycontrol.system.api.model.LoginUser;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 登录校验方法
 *
 * @author czc
 */
@Component
public class SysLoginService
{
    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private SysRecordLogService recordLogService;

    /**
     * 登录
     */
    public LoginUser login(String username, String password,String loginType,List<SysMenu> list)
    {
        // 用户名或密码为空 错误
        if (StringUtils.isAnyBlank(username, password))
        {
            recordLogService.recordLogininfor(username,null,"用户登录","用户/密码不能为空",1);
            throw new ServiceException("用户/密码不能为空",201);
        }
        // 查询用户信息
        Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);

        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
        {
            recordLogService.recordLogininfor(username,null,"用户登录","登录用户：" + username + " 不存在",1);
            throw new ServiceException("登录用户：" + username + " 不存在",201);
        }

        if (Result.FAIL == userResult.getCode())
        {
            recordLogService.recordLogininfor(username,null,"用户登录",userResult.getMsg(),1);
            throw new ServiceException(userResult.getMsg());
        }
        LoginUser userInfo = userResult.getData();
        SysUser user = userResult.getData().getSysUser();
        if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
        {
            recordLogService.recordLogininfor(username,null,"用户登录","对不起，您的账号：" + username + " 已被删除",1);
            throw new ServiceException("对不起，您的账号：" + username + " 已被删除",201);
        }
        if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        {
            recordLogService.recordLogininfor(username,null,"用户登录","对不起，您的账号：" + username + " 已锁定",1);
            throw new ServiceException("对不起，您的账号：" + username + " 已锁定",201);
        }
        if(StringUtils.isEmpty(loginType) && !user.getLoginType().contains(Constant.BACK_LOGIN)){
            throw new ServiceException("对不起，您的账号：" + username + " 无登录后台权限",201);
        }
        if(StringUtils.isNotEmpty(loginType) && !user.getLoginType().contains(Constant.SC_SCREEN)){
            throw new ServiceException("对不起，您的账号：" + username + " 无登录省侧大屏权限",201);
        }
        if(StringUtils.isEmpty(loginType) && !user.getLoginType().contains(Constant.SG_SCREEN)){
            throw new ServiceException("对不起，您的账号：" + username + " 无登录施工大屏权限",201);
        }
        if(StringUtils.isEmpty(loginType) && !user.getLoginType().contains(Constant.APP_LOGIN)){
            throw new ServiceException("对不起，您的账号：" + username + " 无登录APP权限",201);
        }
        passwordService.validate(user, password);
        Result<List<SysMenu>> menu = remoteUserService.getAllMenuList(user.getUserId()+"", SecurityConstants.INNER);
        list = handleMenuList(menu.getData(),loginType);
        user.setJumpType(loginType);
        userInfo.getSysUser().setMenus(list);
        recordLogService.recordLogininfor(username,null, "用户登录", null,1);
        remoteUserService.updateUserLogin(user.getUserId(),SecurityConstants.INNER);
        return userInfo;
    }

    public void logout(String loginName)
    {
        recordLogService.recordLogininfor(loginName,null, "用户登出", null,2);
    }

    public static List<SysMenu> handleMenuList(List<SysMenu> list,String loginType){
        if(CollectionUtils.isEmpty(list)){
            return list;
        }
        // 施工大屏菜单权限
        if (Objects.equals(loginType, Constant.SG_SCREEN) || Objects.equals(loginType, Constant.SC_SCREEN)) {
            return list;
        } else {
            // 后台菜单权限
            return list.stream().filter(e -> !Objects.equals(e.getMenuName(),Constant.SG_SCREEN_NAME)).collect(Collectors.toList());
        }
    }

}
