package de.avatic.lcc.service.api;

import eu.europa.ec.taxation.taric.client.*;
import jakarta.xml.bind.JAXBElement;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.WebServiceTemplate;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Service
public class EUTaxationApiService {

    private final WebServiceTemplate webServiceTemplate;
    private final ObjectFactory objectFactory;

    public EUTaxationApiService(WebServiceTemplate webServiceTemplate) {
        this.webServiceTemplate = webServiceTemplate;
        this.objectFactory = new ObjectFactory();
    }

    public GoodsDescrForWsResponse getGoodsDescription(String goodsCode, String languageCode) {
        GoodsDescrForWs request = new GoodsDescrForWs();
        request.setGoodsCode(goodsCode);
        request.setLanguageCode(languageCode);
        request.setReferenceDate(getCurrentDate());

        JAXBElement<GoodsDescrForWs> requestElement = objectFactory.createGoodsDescrForWs(request);

        @SuppressWarnings("unchecked")
        JAXBElement<GoodsDescrForWsResponse> responseElement =
                (JAXBElement<GoodsDescrForWsResponse>) webServiceTemplate.marshalSendAndReceive(requestElement);

        return responseElement.getValue();
    }

    public GoodsMeasForWsResponse getGoodsMeasures(String goodsCode, String countryCode, String tradeMovement) {
        GoodsMeasForWs request = new GoodsMeasForWs();
        request.setGoodsCode(goodsCode);
        request.setCountryCode(countryCode);
        request.setReferenceDate(getCurrentDate());
        request.setTradeMovement(TradeMovementCode.fromValue(tradeMovement));

        JAXBElement<GoodsMeasForWs> requestElement = objectFactory.createGoodsMeasForWs(request);

        @SuppressWarnings("unchecked")
        JAXBElement<GoodsMeasForWsResponse> responseElement =
                (JAXBElement<GoodsMeasForWsResponse>) webServiceTemplate.marshalSendAndReceive(requestElement);

        return responseElement.getValue();
    }

    private XMLGregorianCalendar getCurrentDate() {
        try {
            String currentDate = LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(currentDate);
        } catch (Exception e) {
            throw new RuntimeException("Fehler beim Erstellen des Datums", e);
        }
    }
}