package io.quarkus.quartz.runtime;

import java.sql.Connection;
import java.sql.SQLException;

import javax.enterprise.util.AnnotationLiteral;
import javax.sql.DataSource;

import org.quartz.utils.PoolingConnectionProvider;

import io.agroal.api.AgroalDataSource;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;

public class QuarkusQuartzConnectionPoolProvider implements PoolingConnectionProvider {
    private AgroalDataSource dataSource;
    private static String dataSourceName;

    public QuarkusQuartzConnectionPoolProvider() {
        final ArcContainer container = Arc.container();
        final InstanceHandle<AgroalDataSource> instanceHandle;
        final boolean useDefaultDataSource = "QUARKUS_QUARTZ_DEFAULT_DATASOURCE".equals(dataSourceName);
        if (useDefaultDataSource) {
            instanceHandle = container.instance(AgroalDataSource.class);
        } else {
            instanceHandle = container.instance(AgroalDataSource.class, new DataSourceLiteral(dataSourceName));
        }
        if (instanceHandle.isAvailable()) {
            this.dataSource = instanceHandle.get();
        } else {
            String message = String.format(
                    "JDBC Store configured but '%s' datasource is missing. You can configure your datasource by following the guide available at: https://quarkus.io/guides/datasource",
                    useDefaultDataSource ? "default" : dataSourceName);
            throw new IllegalStateException(message);
        }
    }

    @Override
    public DataSource getDataSource() {
        return dataSource;
    }

    @Override
    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void shutdown() {
        // Do nothing as the connection will be closed inside the Agroal extension
    }

    @Override
    public void initialize() {

    }

    static void setDataSourceName(String dataSourceName) {
        QuarkusQuartzConnectionPoolProvider.dataSourceName = dataSourceName;
    }

    private static class DataSourceLiteral extends AnnotationLiteral<io.quarkus.agroal.DataSource>
            implements io.quarkus.agroal.DataSource {

        private String name;

        public DataSourceLiteral(String name) {
            this.name = name;
        }

        @Override
        public String value() {
            return name;
        }

    }
}

@@ -92,56 +92,8 @@
 n;%0A%0A
-import javax.enterprise.util.AnnotationLiteral;%0A
 impo
@@ -209,16 +209,71 @@
 Source;%0A
+import io.quarkus.agroal.DataSource.DataSourceLiteral;%0A
 import i
@@ -2089,373 +2089,7 @@
    %7D
-%0A%0A    private static class DataSourceLiteral extends AnnotationLiteral%3Cio.quarkus.agroal.DataSource%3E%0A            implements io.quarkus.agroal.DataSource %7B%0A%0A        private String name;%0A%0A        public DataSourceLiteral(String name) %7B%0A            this.name = name;%0A        %7D%0A%0A        @Override%0A        public String value() %7B%0A            return name;%0A        %7D%0A%0A    %7D
 %0A%7D%0A

use existing DataSourceLiteral in quartz extension