package com.fdkankan.geo;







import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * @author Xiewj
 * @date 2024/3/15
 */
@Slf4j
@Getter
@Setter
@Configuration(value = "GeoQueryUtil")
@ConditionalOnProperty(prefix  = "geoquery",name = "dataFilePath")
@ConfigurationProperties(prefix = "geoquery")
public class GeoQueryUtil {
    @Value("${geoquery.dataFilePath:/mnt/geoQuery/GeoJSON.json}")
    private String dataFilePath="I:\\geoserver\\AreaCity-Query-Geometry\\GeoJSON-Polygon-ok_geo-240508-162233";

    @Value("${geoquery.saveWkbsFilePath:/mnt/geoQuery/GeoJSON.wkbs}")
    private String saveWkbsFilePath="I:\\geoserver\\AreaCity-Query-Geometry\\GeoJSON.wkbs";

    @PostConstruct
    public void init() {
        File dataFile = new File(dataFilePath);
        if (!dataFile.exists()) {
            if (!dataFile.getParentFile().exists()){
                dataFile.getParentFile().mkdirs();
            }
            // 从资源文件中读取数据
            try {
                ClassPathResource classPathResource = new ClassPathResource("GeoJSON.json");
                InputStream is = classPathResource.getInputStream();
                Files.copy(is, Paths.get(dataFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
                log.info("File copied from resources to dataFilePath.");
            } catch (IOException e) {
                log.error("Error copying file from resources: " + e.getMessage());
            }
        }
        File saveWkbsFile = new File(saveWkbsFilePath);
        if (!saveWkbsFile.exists()) {
            if (!saveWkbsFile.getParentFile().exists()){
                saveWkbsFile.getParentFile().mkdirs();
            }
            // 从资源文件中读取数据
            try {
                ClassPathResource classPathResource = new ClassPathResource("GeoJSON.wkbs");
                InputStream is = classPathResource.getInputStream();
                Files.copy(is, Paths.get(saveWkbsFile.getPath()), StandardCopyOption.REPLACE_EXISTING);
                log.info("File copied from resources to saveWkbsFilePath.");
            } catch (IOException e) {
                log.error("Error copying file from resources: " + e.getMessage());
            }
        }
        AreaCityQuery.Init_StoreInWkbsFile(dataFilePath, saveWkbsFilePath, true);
        System.out.println(AreaCityQuery.GetInitInfo().toString()); //打印初始化详细信息，包括性能信息
    }
   public boolean queryPoint(String location)  {
       String[] split = location.split(",");
       if (split.length<2){
           return false;
       }
       double lng= Double.parseDouble(split[0]);
       double lat= Double.parseDouble(split[1]);
       AreaCityQuery.QueryResult res1= null;
       try {
           res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
       } catch (Exception e) {
           throw new RuntimeException(e);
       }
       if (res1!=null &&res1.Result.size()>0){
           log.info("查询结果：{},{}",true,res1);
           return true;
       }else {
           log.info("查询结果：{},{}",false,res1);
           return false;
       }
    }
    public boolean queryPoint(Double lng,Double lat)  {
        AreaCityQuery.QueryResult res1= null;
        try {
            res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (res1!=null &&res1.Result.size()>0){
            log.info("查询结果：{},{}",true,res1);
            return true;
        }else {
            log.info("查询结果：{},{}",false,res1);
            return false;
        }
    }
    public AreaCityQuery.QueryResult queryPointInfo(String location)  {
        String[] split = location.split(",");
        if (split.length<2){
            throw new RuntimeException("非法地理坐标");
        }
        double lng= Double.parseDouble(split[0]);
        double lat= Double.parseDouble(split[1]);
        AreaCityQuery.QueryResult res1= null;
        try {
            res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return res1;
    }
    public AreaCityQuery.QueryResult queryPointInfo(Double lng,Double lat)  {
        AreaCityQuery.QueryResult res1= null;
        try {
            res1 = AreaCityQuery.QueryPoint(lng, lat, null, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return res1;
    }

    public static void main(String[] args) throws Exception {
        GeoQueryUtil queryUtil  = new GeoQueryUtil();
        queryUtil.init();
        AreaCityQuery.QueryResult queryResult = queryUtil.queryPointInfo("120.885676,23.544815");
        System.out.println(queryResult);

    }
}
