package ru.nbch.credit_tracker.service.xml.stax;

import lombok.extern.slf4j.Slf4j;
import ru.nbch.credit_tracker.entities.user_response.monitoring_signal_response.MonitoringSignalResponse;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

@Slf4j
public class MonitoringSignalResponseStaxXmlReader extends BaseStaxReader<MonitoringSignalResponse>{
    @Override
    protected MonitoringSignalResponse createObject() {
        return new MonitoringSignalResponse();
    }

    @Override
    protected boolean innerProcess(XMLStreamReader reader, MonitoringSignalResponse obj) throws XMLStreamException {
        String text = buffer.toString();
        String currPath = getPath();
        switch (currPath) {
            case "/MonitoringSignalResponse/Subjects/Subject" ->
                    obj.getSubjects().finalizeSubject();
            case "/MonitoringSignalResponse/Subjects/Subject/Signals/Signal" ->
                    obj.getSubjects().getCurrentSubject().getSignals().finalizeSignal();
            case "/MonitoringSignalResponse/Subjects/Subject/Id" ->
                    obj.getSubjects().getCurrentSubject().setId(text);
            case "/MonitoringSignalResponse/Subjects/Subject/ClientId" ->
                    obj.getSubjects().getCurrentSubject().setClientId(text);
            case "/MonitoringSignalResponse/Subjects/Subject/FlagDebt5000" -> {
                try {
                    int flagDebt5000 = Integer.parseInt(text);
                    obj.getSubjects().getCurrentSubject().setFlagDebt5000(flagDebt5000);
                } catch (NumberFormatException e) {
                    log.warn("Couldn't parse flagDebt5000: {}", text);
                }
            }
            case "/MonitoringSignalResponse/Subjects/Subject/Flag90_12" -> {
                try {
                    int flag9012 = Integer.parseInt(text);
                    obj.getSubjects().getCurrentSubject().setFlag9012(flag9012);
                } catch (NumberFormatException e) {
                    log.warn("Couldn't parse flag9012: {}", text);
                }
            }
            case "/MonitoringSignalResponse/Subjects/Subject/Signals/Signal/SignalType" -> {
                try {
                    int signalType = Integer.parseInt(text);
                    obj.getSubjects().getCurrentSubject().getSignals().getCurrentSignal().setSignalType(signalType);
                } catch (NumberFormatException e) {
                    log.warn("Couldn't parse signalType: {}", text);
                }
            }
            case "/MonitoringSignalResponse/Subjects/Subject/Signals/Signal/InquiryDate"  ->
                    obj.getSubjects().getCurrentSubject().getSignals().getCurrentSignal().setInquiryDate(text);
            case "/MonitoringSignalResponse/Subjects/Subject/Signals/Signal/InquiryPurpose" -> {
                try {
                    int inquiryPurpose = Integer.parseInt(text);
                    obj.getSubjects().getCurrentSubject().getSignals().getCurrentSignal().setInquiryPurpose(inquiryPurpose);
                } catch (NumberFormatException e) {
                    log.warn("Couldn't parse inquiryPurpose: {}", text);
                }
            }

        }
        return false;
    }

    @Override
    protected void innerProcessStartElement(XMLStreamReader reader, MonitoringSignalResponse obj) throws XMLStreamException {
        String currPath = getPath();
        switch (currPath) {
            case "/MonitoringSignalResponse/Subjects/Subject" ->
                    obj.getSubjects().createCurrentSubject();
            case "/MonitoringSignalResponse/Subjects/Subject/Signals/Signal" ->
                    obj.getSubjects().getCurrentSubject().getSignals().createCurrentSignal();
        }
    }

    @Override
    public Class<MonitoringSignalResponse> type() {
        return MonitoringSignalResponse.class;
    }
}
