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

import lombok.extern.slf4j.Slf4j;
import ru.nbch.credit_tracker.entities.external_response.online_download.Persons;

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

@Slf4j
public class OnlineDownloadReportPersonsStaxReader extends BaseStaxReader<Persons> {

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

    @Override
    protected boolean innerProcess(XMLStreamReader reader, Persons obj) throws XMLStreamException {
        String text = buffer.toString();
        String currPath = getPath();
        switch (currPath) {
            case "/Report/Persons/Person" ->
                    obj.finalizePerson();
            case "/Report/Persons/Person/Inquiry" ->
                    obj.getCurrentPerson().finalizeInquiry();
            case "/Report/Persons/Person/Inquiry/Signal" ->
                    obj.getCurrentPerson().getCurrentInquiry().finalizeSignal();
        }
        return false;
    }

    @Override
    protected void innerProcessStartElement(XMLStreamReader reader, Persons obj) throws XMLStreamException {
        String currPath = getPath();
        switch (currPath) {
            case "/Report/Persons/Person" ->
                    obj.createCurrentPerson();
            case "/Report/Persons/Person/Inquiry" ->
                    obj.getCurrentPerson().createCurrentInquiry();
            case "/Report/Persons/Person/Inquiry/Signal" ->
                    obj.getCurrentPerson().getCurrentInquiry().createCurrentSignal();
        }

        for (int i = 0; i < reader.getAttributeCount(); i++) {
            String text = reader.getAttributeValue(i);
            switch (reader.getAttributeLocalName(i)) {
                case "uid" -> {
                    try {
                        int uid = Integer.parseInt(text);
                        obj.getCurrentPerson().setUid(uid);
                    } catch (NumberFormatException e) {
                        log.error("Couldn't parse uid: {}", text);
                        throw new RuntimeException(e);
                    }
                }
                case "date" ->
                        obj.getCurrentPerson().getCurrentInquiry().setDate(text);
                case "bus_category" ->
                        obj.getCurrentPerson().getCurrentInquiry().setBusCategory(text);
                case "inq_p" -> {
                    try {
                        int inqP = Integer.parseInt(text);
                        obj.getCurrentPerson().getCurrentInquiry().setInqP(inqP);
                    } catch (NumberFormatException e) {
                        log.warn("Couldn't parse inqP: {}", text);
                    }
                }
                case "n" -> {
                    try {
                        int n = Integer.parseInt(text);
                        obj.getCurrentPerson().getCurrentInquiry().getCurrentSignal().setN(n);
                    } catch (NumberFormatException e) {
                        log.warn("Couldn't parse signal n: {}", text);
                    }
                }
            }
        }
    }

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