package com.goldenwater.slgc.controller.proxy;

import java.util.Collections;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.slgc.config.ApiProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "31. 三维数据管理")
@RestController
@RequestMapping("/slgc/sw")
public class SwController extends BaseController {

    private final RestTemplate restTemplate;
    private final ApiProperties apiProperties;

    public SwController(RestTemplate restTemplate, ApiProperties apiProperties) {
        this.restTemplate = restTemplate;
        this.apiProperties = apiProperties;
    }

    @Operation(summary = "查询三维基础资料")
    @GetMapping("/info/list")
    public AjaxResult infoList() {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_SWJCZL";
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "查询三维建模数据")
    @GetMapping("/jm/list")
    public AjaxResult jmList(@RequestParam(defaultValue = "") String stnm) {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_SWJM?stnm=" + stnm;
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "查询三维实景拍摄数据")
    @GetMapping("/sj/list")
    public AjaxResult sjList(@RequestParam(defaultValue = "") String stnm) {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_SJPS?stnm=" + stnm;
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "查询现场图片")
    @GetMapping("/tp/list")
    public AjaxResult tpList(@RequestParam(defaultValue = "") String stnm) {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_XCTP?stnm=" + stnm;
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @SuppressWarnings("unchecked")
    private Object parseJsonFromSoap(String xml) {
        if (xml == null || xml.isEmpty()) return Collections.emptyList();
        try {
            javax.xml.parsers.DocumentBuilder db = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
            org.w3c.dom.Document doc = db.parse(new java.io.ByteArrayInputStream(xml.getBytes("UTF-8")));
            javax.xml.xpath.XPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
            org.w3c.dom.NodeList nodes = (org.w3c.dom.NodeList) xpath.evaluate("//Table/string", doc, javax.xml.xpath.XPathConstants.NODESET);
            if (nodes.getLength() > 0) {
                String json = nodes.item(0).getTextContent().trim();
                com.alibaba.fastjson2.JSONArray arr = com.alibaba.fastjson2.JSONArray.parseArray(json);
                return arr != null ? arr : Collections.emptyList();
            }
        } catch (Exception ignored) {}
        return Collections.emptyList();
    }
}
