package com.inspect.admin.controller;

import com.inspect.admin.form.LoginBody;
import com.inspect.admin.form.RegisterBody;
import com.inspect.admin.service.SysLoginService;
import com.inspect.base.core.constant.TokenConst;
import com.inspect.base.core.domain.Response;
import com.inspect.base.core.utils.JwtUtils;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.common.security.auth.AuthUtil;
import com.inspect.common.security.service.TokenService;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.base.model.LoginUser;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TokenController {
   @Autowired
   private TokenService tokenService;

   @Autowired
   private SysLoginService sysLoginService;

   @PostMapping({"login"})
   public Response<?> login(@RequestBody LoginBody form, HttpServletRequest request) {
      LoginUser userInfo = this.sysLoginService.login(form.getUsername(), form.getPassword(), request);
      return Response.ok(this.tokenService.createToken(userInfo));
   }

   @PostMapping({"secondLogin"})
   public Response<?> secondLogin(@RequestBody LoginBody form, HttpServletRequest request) {
      String ticket = this.sysLoginService.secondLogin(form.getUsername(), form.getPassword(), request);
      return Response.ok(ticket);
   }

   @PostMapping({"logout"})
   public Response<?> logout(HttpServletRequest request) {
      String token = SecurityUtils.getToken(request);
      if(StringUtils.isNotEmpty(token)) {
         String username = JwtUtils.getUserName(token);
         AuthUtil.logoutByToken(token);
         this.sysLoginService.logout(username);
      }

      return Response.ok();
   }

   @PostMapping({"refresh"})
   public Response<?> refresh(HttpServletRequest request) {
      LoginUser loginUser = this.tokenService.getLoginUser(request);
      if(StringUtils.isNotNull(loginUser)) {
         this.tokenService.refreshToken(loginUser);
         return Response.ok();
      } else {
         return Response.ok();
      }
   }

   public Response<?> register(@RequestBody RegisterBody registerBody) {
      this.sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
      return Response.ok();
   }

   @PostMapping({"getToken"})
   public Response<?> getToken(HttpServletRequest request) {
      LoginUser userInfo = this.sysLoginService.login("admin", "e6e061838856bf47e1de730719fb2609", request);
      Map<String, Object> map = this.tokenService.createToken(userInfo);
      Map<String, Object> mapApi = new HashMap();
      mapApi.put("key", "Authorization");
      mapApi.put("value", "Bearer " + map.get(TokenConst.ACCESS_TOKEN));
      return Response.ok(mapApi, "操作成功");
   }
}
