package com.citygis.web.controller.system;

import com.citygis.common.constant.Constants;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.common.core.domain.entity.Mail;
import com.citygis.common.core.domain.entity.SysMenu;
import com.citygis.common.core.domain.entity.SysUser;
import com.citygis.common.core.domain.model.LoginBody;
import com.citygis.common.utils.SecurityUtils;
import com.citygis.framework.web.service.SysLoginService;
import com.citygis.framework.web.service.SysPermissionService;
import com.citygis.system.service.ISysMenuService;
import com.citygis.web.service.SingleLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.citygis.common.utils.SecurityUtils.getUserId;

/**
 * 登录验证
 * 
 * @author citygis
 */
@Api(tags = "登录")
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Resource
    SingleLoginService singleLoginService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        Map<String, Object> login = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());

        ajax.put(Constants.TOKEN, String.valueOf(login.get("token")));
        ajax.put("userId", login.get("userId"));
        return ajax;
    }

    /**
     * 登录方法
     *
     * @param mail 登录信息
     * @return 结果
     */
    @ApiOperation(value = "邮箱登录")
    @PostMapping("/loginByMail")
    public AjaxResult loginByMail(@RequestBody Mail mail)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        Map<String, Object> login = loginService.login(mail);

        ajax.put(Constants.TOKEN, String.valueOf(login.get("token")));
        ajax.put("userId", login.get("userId"));
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 单点登录，页面初始化调用
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "单点登录")
    @GetMapping("/singleLogin")
    public void singleLogin(HttpServletRequest request,
                            HttpServletResponse response) throws IOException {
        singleLoginService.singleLogin(request, response);
    }

}
