package com.zhongzheng.controller.system;

import com.zhongzheng.common.constant.Constants;
import com.zhongzheng.common.core.domain.AjaxResult;
import com.zhongzheng.common.core.domain.entity.SysUser;
import com.zhongzheng.common.core.domain.entity.TopSysUser;
import com.zhongzheng.common.core.domain.entity.SysMenu;
import com.zhongzheng.common.core.domain.model.LoginBody;
import com.zhongzheng.common.core.domain.model.LoginUser;
import com.zhongzheng.common.core.domain.model.TopLoginUser;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.exception.user.UserPasswordNotMatchException;
import com.zhongzheng.common.utils.MessageUtils;
import com.zhongzheng.common.utils.ServletUtils;
import com.zhongzheng.framework.manager.AsyncManager;
import com.zhongzheng.framework.manager.factory.AsyncFactory;
import com.zhongzheng.framework.web.service.TokenService;
import com.zhongzheng.framework.web.service.TopSysLoginService;
import com.zhongzheng.framework.web.service.TopTokenService;
import com.zhongzheng.framework.web.service.SysPermissionService;
import com.zhongzheng.modules.system.service.ISysMenuService;
import com.zhongzheng.modules.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 登录验证
 *
 * @author zhongzheng
 */
@Api(tags ="登录用户操作管理")
@RestController
public class SuperSysLoginController
{
    @Autowired
    private TopSysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TopTokenService topTokenService;



    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService userService;
    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("登录用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        TopLoginUser loginUser = topTokenService.getLoginUser(ServletUtils.getRequest());
        TopSysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = new HashSet<String>();
        roles.add("admin");
        // 权限集合
        Set<String> permissions = new HashSet<String>();
        permissions.add("*:*:*");
        Map<String,Object> map = new HashMap<>();
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        return AjaxResult.success(map);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("路由菜单信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        TopLoginUser loginUser = topTokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        TopSysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 获取子系统令牌
     *
     * @return 获取子系统令牌
     */
    @ApiOperation("获取子系统令牌")
    @GetMapping("getChildToken")
    public AjaxResult getChildToken()
    {
        //普通系统用户
        SysUser user = userService.selectUserByUserName("admin");
        LoginUser loginUser = new LoginUser(user, permissionService.getMenuPermission(user));
        String token = tokenService.createToken(loginUser);
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
}
