package thyyxxk.webserver.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.service.externalhttp.WebSocketService;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author dj
 */
@Service("TokenService")
public class TokenService {
    private final WebSocketService socketService;

    @Autowired
    public TokenService(WebSocketService socketService) {
        this.socketService = socketService;
    }

    public String getToken(UserInfo userInfo) {
        Date start = new Date();
        long expireTimeStamp = System.currentTimeMillis() + 60 * 60 * 1000 * 240;
        Date expireTime = new Date(expireTimeStamp);
        return JWT.create()
                .withAudience(userInfo.getCode())
                .withIssuedAt(start)
                .withExpiresAt(expireTime)
                .sign(Algorithm.HMAC256(userInfo.getPassword()));
    }

    public String getWeComAddressBookToken() {
        return socketService.getWeComAddressBookToken();
    }

    public String getWeComClockInToken() {
        return socketService.getWeComClockInToken();
    }

    public String getWeComSendMsgToken() {
        return socketService.getWeComSendMsgToken();
    }

    public String getWeComSendFileToken() {
        return socketService.getWeComSendFileToken();
    }

    public String[] getLisDockSign() {
        return socketService.getLisDockSign();
    }

    public void setLisDockSign(String[] sign) {
        socketService.setLisDockSign(sign[0], sign[1]);
    }

    public String getThmzToken() {
        return socketService.getThmzToken();
    }
}
