package de.votian.services.controller;

import de.votian.services.service.LegacyXmlIntegrationService;
import de.votian.services.service.LegacyXmlMetaobjectIntegrationService;
import de.votian.services.service.LegacyXmlOrderDataIntegrationService;
import de.votian.services.service.LegacyXmlOrderRequestIntegrationService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LegacyXmlController {

    private final LegacyXmlIntegrationService legacyXmlIntegrationService;
    private final LegacyXmlMetaobjectIntegrationService legacyXmlMetaobjectIntegrationService;
    private final LegacyXmlOrderDataIntegrationService legacyXmlOrderDataIntegrationService;
    private final LegacyXmlOrderRequestIntegrationService legacyXmlOrderRequestIntegrationService;
    private final de.votian.services.service.LegacyXmlSystemIntegrationService legacyXmlSystemIntegrationService;

    public LegacyXmlController(LegacyXmlIntegrationService legacyXmlIntegrationService,
                               LegacyXmlMetaobjectIntegrationService legacyXmlMetaobjectIntegrationService,
                               LegacyXmlOrderDataIntegrationService legacyXmlOrderDataIntegrationService,
                               LegacyXmlOrderRequestIntegrationService legacyXmlOrderRequestIntegrationService,
                               de.votian.services.service.LegacyXmlSystemIntegrationService legacyXmlSystemIntegrationService) {
        this.legacyXmlIntegrationService = legacyXmlIntegrationService;
        this.legacyXmlMetaobjectIntegrationService = legacyXmlMetaobjectIntegrationService;
        this.legacyXmlOrderDataIntegrationService = legacyXmlOrderDataIntegrationService;
        this.legacyXmlOrderRequestIntegrationService = legacyXmlOrderRequestIntegrationService;
        this.legacyXmlSystemIntegrationService = legacyXmlSystemIntegrationService;
    }

    @RequestMapping(
            value = "/service/costcenter_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> costcenter(@RequestBody(required = false) String body,
                                             @RequestParam(required = false) String costcenterReq) {
        return ResponseEntity.ok(legacyXmlIntegrationService.handleCostcenterRequest(resolveRequest(body, costcenterReq)));
    }

    @RequestMapping(
            value = "/service/customer_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> customer(@RequestBody(required = false) String body,
                                           @RequestParam(required = false) String customerReq) {
        return ResponseEntity.ok(legacyXmlSystemIntegrationService.handleCustomerRequest(resolveRequest(body, customerReq)));
    }

    @RequestMapping(
            value = "/service/metaobject_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> metaobject(@RequestBody(required = false) String body,
                                             @RequestParam(required = false) String metaobjectReq) {
        return ResponseEntity.ok(legacyXmlMetaobjectIntegrationService.handleMetaobjectRequest(resolveRequest(body, metaobjectReq)));
    }

    @RequestMapping(
            value = "/service/order_data_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> orderData(@RequestBody(required = false) String body,
                                            @RequestParam(required = false) String orderDataReq) {
        return ResponseEntity.ok(legacyXmlOrderDataIntegrationService.handleOrderDataRequest(resolveRequest(body, orderDataReq)));
    }

    @RequestMapping(
            value = "/service/order_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> order(@RequestBody(required = false) String body,
                                        @RequestParam(required = false) String orderReq) {
        return ResponseEntity.ok(legacyXmlOrderRequestIntegrationService.handleOrderRequest(resolveRequest(body, orderReq)));
    }

    @RequestMapping(
            value = "/service/station_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> station(@RequestBody(required = false) String body,
                                          @RequestParam(required = false) String stationReq,
                                          @RequestParam(required = false) String orderReq) {
        return ResponseEntity.ok(legacyXmlOrderRequestIntegrationService.handleStationRequest(
                resolveRequest(body, firstNonBlank(stationReq, orderReq))
        ));
    }

    @RequestMapping(
            value = "/service/zone_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> zone(@RequestBody(required = false) String body,
                                       @RequestParam(required = false) String zoneReq) {
        return ResponseEntity.ok(legacyXmlIntegrationService.handleZoneRequest(resolveRequest(body, zoneReq)));
    }

    @RequestMapping(
            value = "/service/price_request.php",
            method = {RequestMethod.GET, RequestMethod.POST},
            produces = MediaType.APPLICATION_XML_VALUE
    )
    public ResponseEntity<String> price(@RequestBody(required = false) String body,
                                        @RequestParam(required = false) String priceReq) {
        return ResponseEntity.ok(legacyXmlIntegrationService.handlePriceRequest(resolveRequest(body, priceReq)));
    }

    private String resolveRequest(String body, String requestParam) {
        if (requestParam != null && !requestParam.isBlank()) {
            return requestParam;
        }
        return body != null ? body : "";
    }

    private String firstNonBlank(String... values) {
        if (values == null) {
            return null;
        }
        for (String value : values) {
            if (value != null && !value.isBlank()) {
                return value;
            }
        }
        return null;
    }
}
