package com.hw.auth.controller;

import javax.servlet.http.HttpServletRequest;

import com.hw.common.core.constant.SecurityConstants;

import com.hw.mes.api.RemoteMesService;
import com.hw.mes.api.domain.MesBaseStationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hw.auth.form.LoginBody;
import com.hw.auth.form.RegisterBody;
import com.hw.auth.service.SysLoginService;
import com.hw.common.core.domain.R;
import com.hw.common.core.utils.JwtUtils;
import com.hw.common.core.utils.StringUtils;
import com.hw.common.security.auth.AuthUtil;
import com.hw.common.security.service.TokenService;
import com.hw.common.security.utils.SecurityUtils;
import com.hw.system.api.model.LoginUser;

import java.util.List;

/**
 * token 控制
 *
 * @author ruoyi
 */
@RestController
public class TokenController
{
    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private RemoteMesService remoteMesService;

    @PostMapping("login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        userInfo.setStationId(form.getStationId());
        userInfo.setStationCode(form.getStationCode());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }

    @DeleteMapping("logout")
    public R<?> logout(HttpServletRequest request)
    {
        String token = SecurityUtils.getToken(request);
        if (StringUtils.isNotEmpty(token))
        {
            String username = JwtUtils.getUserName(token);
            // 删除用户缓存记录
            AuthUtil.logoutByToken(token);
            // 记录用户退出日志
            sysLoginService.logout(username);
        }
        return R.ok();
    }

    @PostMapping("refresh")
    public R<?> refresh(HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (StringUtils.isNotNull(loginUser))
        {
            // 刷新令牌有效期
            tokenService.refreshToken(loginUser);
            return R.ok();
        }
        return R.ok();
    }

    @PostMapping("register")
    public R<?> register(@RequestBody RegisterBody registerBody)
    {
        // 用户注册
        sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
        return R.ok();
    }


    @PostMapping("pdaLogin")
    public R<?> pdaLogin(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }


    /**
     * 获取工位信息列表
     * @return
     */
    @GetMapping("getStations")
    public R<?> getStations()
    {
        R<List<MesBaseStationInfo>> resultR = remoteMesService.getStations(SecurityConstants.INNER);
        return R.ok(resultR.getData());
    }

}
