package cz.senslog.connector.fetch.soilscount;

import cz.senslog.connector.fetch.api.ExecutableFetcher;
import cz.senslog.connector.model.api.AbstractModel;
import cz.senslog.connector.model.config.DefaultConfig;
import cz.senslog.connector.model.soilscount.SoilscountModel;
import cz.senslog.connector.tools.http.HttpClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import static cz.senslog.connector.tools.http.HttpClient.newHttpClient;

public class ConnectorFetchSoilscountProvider implements cz.senslog.connector.fetch.api.ConnectorFetchProvider {

    private static final Logger logger = LogManager.getLogger(ConnectorFetchSoilscountProvider.class);


    @Override
    public ExecutableFetcher<? extends AbstractModel> createExecutableFetcher(DefaultConfig defaultConfig) {
        logger.info("Initialization a new fetch provider {}.", ConnectorFetchSoilscountProvider.class.getSimpleName());

        logger.debug("Creating a new configuration.");
        SoilScountConfig config = new SoilScountConfig(defaultConfig);
        logger.info("Configuration for {} was created successfully.", SoilScountFetcher.class.getSimpleName());


        logger.debug("Creating a new instance of {}.", SoilScountFetcher.class);
        SoilScountFetcher fetcher = new SoilScountFetcher(config, HttpClient.newHttpSSLClient());
        logger.info("Fetcher for {} was created successfully.", SoilScountFetcher.class.getSimpleName());

        ExecutableFetcher<SoilscountModel> executor = ExecutableFetcher.create(fetcher);
        logger.info("Fetcher executor for {} was created successfully.", SoilScountFetcher.class.getSimpleName());

        {
            logger.debug("Creating a new instance of {}.", ProxySession.class);
            ProxySession proxySession = new ProxySession(fetcher, newHttpClient());
            logger.info("Fetcher session for {} was created successfully.", ProxySession.class);
            executor = ExecutableFetcher.createWithProxySession(proxySession);
        }

        return executor;
    }
}
