package com.sikey.wa04.business.infrastructure;

import com.eatthepath.pushy.apns.ApnsClient;
import com.eatthepath.pushy.apns.ApnsClientBuilder;
import com.eatthepath.pushy.apns.PushNotificationResponse;
import com.eatthepath.pushy.apns.auth.ApnsSigningKey;
import com.eatthepath.pushy.apns.util.ApnsPayloadBuilder;
import com.eatthepath.pushy.apns.util.SimpleApnsPayloadBuilder;
import com.eatthepath.pushy.apns.util.SimpleApnsPushNotification;
import com.eatthepath.pushy.apns.util.TokenUtil;
import org.checkerframework.checker.index.qual.SameLen;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

public class APNsTokenBasedCredentialsBuilderTests {

    @Test
    public void build() throws IOException, NoSuchAlgorithmException, InvalidKeyException, InterruptedException, TimeoutException {
        ClassLoader classLoader = APNsTokenBasedCredentialsBuilderTests.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("AuthKey_2H6M4TM2SW.p8");
        ApnsClient apnsClient = new ApnsClientBuilder()
                .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
                .setSigningKey(ApnsSigningKey.loadFromInputStream(inputStream, "YHJXY25X52", "2H6M4TM2SW"))
                .build();
        ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder().setAlertBody("Message received from Pushy!");
        String token = TokenUtil.sanitizeTokenString("037a2d8adbe8fdf85e199c46e16e2eed46acd57035bb3451f894b8623aa4e40a");
        SimpleApnsPushNotification notification = new SimpleApnsPushNotification(token, "com.sikey.kidswatch", payloadBuilder.build());

        PushNotificationResponse<SimpleApnsPushNotification> response;
        AtomicInteger retryNum = new AtomicInteger();
        final int maxRetry = 10;
        while (retryNum.get() < maxRetry) {
            try {
                response = proceed(apnsClient, notification);
                System.out.println(response.isAccepted());
                System.out.println(response);
                break;
            } catch (ExecutionException executionException) {
               // System.out.println("Failed to send push notification. ERROR: " + executionException.getMessage());
            }
            retryNum.getAndIncrement();
        }
        System.out.println("Retry number: " + retryNum.get());
    }

    private PushNotificationResponse<SimpleApnsPushNotification> proceed(ApnsClient apnsClient,
                                                                         SimpleApnsPushNotification notification)
            throws ExecutionException, InterruptedException, TimeoutException {
        return apnsClient.sendNotification(notification).get(10, TimeUnit.SECONDS);
    }
}
