$service_bgl360 = new ServiceBgl360($wpdb, $current_user->ID);
$authentication_bgl360 = new AuthenticationBgl360();
$resource_request_bgl360 = new ResourceRequestBgl360();
$time = new Time();
/**
 * Set up authentication data
 */
$authentication_bgl360->setRedirectUrlToAuthorizationPage("https://api-staging.bgl360.com.au/oauth/authorize?response_type=code&client_id=5dbf9b2c-981f-44e4-8212-d3b5c74795a1&scope=investment&redirect_uri=https://app.thesmsfacademy.com.au/bgl360-authenticate.php");
$authentication_bgl360->setMainUri('https://api-staging.bgl360.com.au/oauth/token');
$authentication_bgl360->setAuthorizationCode($authentication_bgl360->getAuthorizationCode());
$authentication_bgl360->setGrantType('authorization_code');
$authentication_bgl360->setScope('investment');
$authentication_bgl360->setClientId('5dbf9b2c-981f-44e4-8212-d3b5c74795a1');
$authentication_bgl360->setClientSecret('b5a0ff39-ef93-4bc7-b5de-e0ace2d7a6fc');
$authentication_bgl360->setRedirectUri('https://app.thesmsfacademy.com.au/bgl360-authenticate.php');
$authentication_bgl360->setAccessTokenUri($authentication_bgl360->getAccessTokenUri());
if ($service_bgl360->isExistAccessToken()) {
    // Get access token from database
    $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser();
    // If access token is expired then refresh user's current token
    if ($service_bgl360->isUserAccessTokenExpired($userCurrentAccessToken[0]['expires_in'])) {
        echo "Access Token is expired <br>";
        $authentication_bgl360->setRefreshTokenUrl($userCurrentAccessToken[0]['refresh_token']);
        $authentication_bgl360->setAccessToken($userCurrentAccessToken[0]['access_token']);
        $response = $authentication_bgl360->getRefreshTokenData();
        if ($response['error'] == 'invalid_token') {
            echo "Refresh token is already used to retrieved new access token <br>";
        } else {
            // Update access token, refresh token, expired_at and updated_at
            $service_bgl360->updateAccessToken(array('access_token' => $response['access_token'], 'refresh_token' => $response['refresh_token'], 'expired_at' => $time->getAccessTokenExpireDateTime(), 'updated_at' => $time->getCurrentDateTime()));
        }
use App\Time;
global $wpdb;
$service_bgl360 = new ServiceBgl360($wpdb, $current_user->ID);
$authentication_bgl360 = new AuthenticationBgl360();
$resource_request_bgl360 = new ResourceRequestBgl360();
$time = new Time();
//Authentication set up
$authentication_bgl360->setRedirectUrlToAuthorizationPage("https://api-staging.bgl360.com.au/oauth/authorize?response_type=code&client_id=5dbf9b2c-981f-44e4-8212-d3b5c74795a1&scope=investment&redirect_uri=https://app.thesmsfacademy.com.au/wp-bgl360-landing.php");
$authentication_bgl360->setMainUri('https://api-staging.bgl360.com.au/oauth/token');
$authentication_bgl360->setAuthorizationCode($authentication_bgl360->getAuthorizationCode());
$authentication_bgl360->setGrantType('authorization_code');
$authentication_bgl360->setScope('investment');
$authentication_bgl360->setClientId('5dbf9b2c-981f-44e4-8212-d3b5c74795a1');
$authentication_bgl360->setClientSecret('b5a0ff39-ef93-4bc7-b5de-e0ace2d7a6fc');
$authentication_bgl360->setRedirectUri('https://app.thesmsfacademy.com.au/wp-bgl360-landing.php');
$authentication_bgl360->setAccessTokenUri($authentication_bgl360->getAccessTokenUri());
echo "Access Token Uri " . $authentication_bgl360->getAccessTokenUri() . '<br>';
//Insert new access token to database print insert token status
//if($service_bgl360->insertNewAccessToken(array('access_token'=>'123123', 'refresh_token'=>'sadasdasd', 'expires_in'=>'123123', 'scope'=> 'investment'))){
//    echo "inserted <br>";
//} else {
//    echo "data failed to insert<br>";
//}
//
//$results = $wpdb->get_results( 'SELECT * FROM service_bgl360 WHERE user_id = 1', ARRAY_A  );
//print_r($results );
//check if access token for the user is exist
if ($service_bgl360->isExistAccessToken()) {
    echo "User id " . $current_user->ID . " has an access token <br>";
    //get access token from database
    $userCurrentAccessToken = $service_bgl360->getCurrentAccessTokenByUser();