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

import ru.nbch.credit_tracker.entities.user_request.monitoring_request.MonitoringRequest;

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

public class MonitoringRequestStaxReader extends BaseStaxReader<MonitoringRequest> {

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

    @Override
    protected MonitoringRequest createObject() {
        return new MonitoringRequest();
    }

    @Override
    protected boolean innerProcess(XMLStreamReader reader, MonitoringRequest obj) {
        String text = buffer.toString();
        String currPath = getPath();
        switch (currPath) {
            case "/MonitoringRequest/UserId" ->
                obj.setUserId(text);
            case "/MonitoringRequest/Password" ->
                obj.setPassword(text);
            case "/MonitoringRequest/PackageName" ->
                obj.setPackageName(text);
            case "/MonitoringRequest/Subjects/Subject" ->
                obj.finalizeSubject();
            case "/MonitoringRequest/Subjects/Subject/Id" ->
                obj.getCurrentSubject().setId(text);
            case "/MonitoringRequest/Subjects/Subject/ClientId" ->
                obj.getCurrentSubject().setClientId(text);
        }
        return false;
    }

    @Override
    protected void innerProcessStartElement(XMLStreamReader reader, MonitoringRequest obj) throws XMLStreamException {
        String text = buffer.toString();
        String currPath = getPath();

        switch (currPath) {
            case "/MonitoringRequest/Subjects/Subject" ->
                    obj.createCurrentSubject();
        }
    }
}
