package ru.nbch.credit_tracker.config.xml;

import jakarta.xml.bind.JAXBException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;
import ru.nbch.credit_tracker.entities.external_response.monitoring_report_file.SgnlReportMonitorigSetupError;
import ru.nbch.credit_tracker.entities.external_response.online_report.SgnlOnlineReport;
import ru.nbch.credit_tracker.entities.user_response.errors.MonitoringError;
import ru.nbch.credit_tracker.entities.user_response.success.MonitoringSetupResponse;
import ru.nbch.credit_tracker.service.xml.jaxb.IXmlProcessor;
import ru.nbch.credit_tracker.service.xml.jaxb.XmlProcessorImplAnyClasses;
import ru.nbch.credit_tracker.service.xml.stax.*;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

@Configuration
public class XmlConfig {

    @Bean
    public IXmlProcessor xmlProcessor(CreditTrackerSettings settings) throws JAXBException {
        return new XmlProcessorImplAnyClasses(Charset.forName(settings.getEncoding()),
                // unmarshalling classes
                SgnlOnlineReport.class,
                SgnlReportMonitorigSetupError.class,
                // marshalling classes
                MonitoringError.class,
                MonitoringSetupResponse.class);
    }

    @Bean
    public StaxXmlProcessor staxXmlProcessor() {
        List<Supplier<StaxReader<?>>> staxReader = new ArrayList<>();
        staxReader.add(SgnlReportStatusStaxReader::new);
        staxReader.add(MonitoringRequestStaxReader::new);
        staxReader.add(OnlineDownloadReportPersonsStaxReader::new);
        staxReader.add(MonitoringSignalResponseStaxXmlReader::new);
        staxReader.add(MonitoringReportErrorsStaxtReader::new);
        return new XmlProcessorStaxImpl(staxReader);
    }
}
