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.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 = "21. 监测管理")
@RestController
@RequestMapping("/slgc/monitor")
public class MonitorController extends BaseController {

    private final RestTemplate restTemplate;
    private final ApiProperties apiProperties;

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

    @Operation(summary = "21.01 查询监测列表")
    @GetMapping("/list")
    public AjaxResult list(String startDate, String bsnm, String frgrd) {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_BZselect?StartDate=" + (startDate != null ? startDate : "")
                    + "&BSNM=" + (bsnm != null ? bsnm : "") + "&FRGRD=" + (frgrd != null ? frgrd : "");
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "21.02 查询工程数据")
    @GetMapping("/gc")
    public AjaxResult gc(String startDate) {
        try {
            String url = apiProperties.getYjzh() + "/Select_GC?StartDate=" + (startDate != null ? startDate : "");
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(parseJsonFromSoap(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "21.04 查询实时水情(太浦闸/望亭/常熟)")
    @GetMapping("/realtime-tbadd")
    public AjaxResult realtimeTbadd() {
        try {
            String url = apiProperties.getTbaddRealtime() + "/tbadd/wd/3d";
            ResponseEntity<String> resp = restTemplate.getForEntity(url, String.class);
            return success(com.alibaba.fastjson2.JSON.parseObject(resp.getBody()));
        } catch (Exception e) {
            return success(Collections.emptyList());
        }
    }

    @Operation(summary = "21.03 查询工程设施")
    @GetMapping("/gcsz")
    public AjaxResult gcsz(String ennm, String ptcd, String rtcd) {
        try {
            String url = apiProperties.getYjzh() + "/SELECT_GCSZ?ennm=" + (ennm != null ? ennm : "")
                    + "&ptcd=" + (ptcd != null ? ptcd : "") + "&rtcd=" + (rtcd != null ? rtcd : "");
            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();
    }
}
