package com.qxgmat.controller.admin;

import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.nuliji.tools.exception.AuthException;
import com.qxgmat.data.dao.entity.Manager;
import com.qxgmat.data.relation.entity.ManagerRelation;
import com.qxgmat.dto.admin.request.LoginDto;
import com.qxgmat.dto.admin.response.LoginUserDto;
import com.qxgmat.help.CaptchaHelp;
import com.qxgmat.help.ShiroHelp;
import com.qxgmat.service.inline.ManagerRoleService;
import com.qxgmat.service.ManagerService;
import com.qxgmat.service.inline.ManagerLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by GaoJie on 2017/11/3.
 */
@RestController("AdminAuthController")
@RequestMapping("/admin/auth")
@Api(tags = "验证接口", produces = MediaType.APPLICATION_JSON_VALUE)
public class AuthController {
    @Autowired
    private CaptchaHelp captchaHelp;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private ManagerRoleService managerRoleService;

    @Autowired
    private ShiroHelp shiroHelp;

    @Autowired
    private ManagerLogService managerLogService;

    @RequestMapping(value = "/token", method = RequestMethod.POST)
    @ApiOperation(value = "验证token", httpMethod = "POST")
    public Response<LoginUserDto> token(HttpSession session, HttpServletRequest request) {
        Manager manager = shiroHelp.getLoginManager();
        if (manager == null) {
            throw new AuthException("未登录");
        }
        ManagerRelation managerRelation = Transform.convert(manager, ManagerRelation.class);
        managerRelation.setRole(managerRoleService.get(managerRelation.getRoleId()));
        managerLogService.log(request);
        return ResponseHelp.success(Transform.convert(managerRelation, LoginUserDto.class));
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录", httpMethod = "POST")
    public Response<LoginUserDto> login(@RequestBody @Validated LoginDto loginDto, HttpSession session, HttpServletRequest request) {
        shiroHelp.getSession().login(shiroHelp.manager(loginDto.getUsername(), loginDto.getPassword(), loginDto.getRemember()));
        Manager manager = shiroHelp.getLoginManager();
        ManagerRelation managerRelation = Transform.convert(manager, ManagerRelation.class);
        managerRelation.setRole(managerRoleService.get(managerRelation.getRoleId()));
        managerLogService.log(request);
        return ResponseHelp.success(Transform.convert(managerRelation, LoginUserDto.class));
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ApiOperation(value = "登出", httpMethod = "POST")
    public Response<Boolean> logout(HttpSession session, HttpServletRequest request) {
        shiroHelp.logout();
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    @ApiOperation(value = "刷新", httpMethod = "POST")
    public Response<LoginUserDto> refresh(HttpSession session) {
        shiroHelp.refresh();
        Manager manager = (Manager)shiroHelp.getLoginManager();
        if(manager == null) return ResponseHelp.success(new LoginUserDto());
        ManagerRelation managerRelation = Transform.convert(manager, ManagerRelation.class);
        managerRelation.setRole(managerRoleService.get(managerRelation.getRoleId()));
        return ResponseHelp.success(Transform.convert(managerRelation, LoginUserDto.class));
    }
}

