package ink.wgink.module.oauth2.startup;

import ink.wgink.module.oauth2.manager.OAuth2ClientTokenManager;
import ink.wgink.module.oauth2.remote.IAccessTokenRemoteService;
import ink.wgink.properties.ApiPathProperties;
import ink.wgink.properties.oauth2.client.OAuth2ClientProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: OAuth2ClientStartUp
 * @Description: OAuth2Client启动
 * @Author: WangGeng
 * @Date: 2021/9/21 14:07
 * @Version: 1.0
 **/
@Component
public class OAuth2ClientStartUp implements ApplicationRunner {

    @Autowired
    private IAccessTokenRemoteService accessTokenRemoteService;
    @Autowired
    private OAuth2ClientProperties oAuth2ClientProperties;
    @Autowired
    private ApiPathProperties apiPathProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        OAuth2ClientTokenManager oAuth2ClientTokenManager = OAuth2ClientTokenManager.getInstance();
        oAuth2ClientTokenManager.setAccessTokenRemoteService(accessTokenRemoteService);
        oAuth2ClientTokenManager.setOAuth2ClientProperties(oAuth2ClientProperties);
        oAuth2ClientTokenManager.setApiPathProperties(apiPathProperties);
        refreshAccessToken();
    }

    @Scheduled(cron = "0 0/15 * * * ?")
    public void refreshAccessToken() {
        OAuth2ClientTokenManager.getInstance().getToken();
    }
}
