package com.sysu.admin.utils.shape;

import com.alibaba.fastjson.JSON;
import com.sysu.admin.utils.shape.postgis.PostGisUtil;
import com.sysu.admin.utils.shape.vo.Param;
import com.sysu.admin.utils.shape.vo.ParamType;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.jdbc.JDBCDataStore;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        try {
            GeometryJSON fjson = new GeometryJSON();
            FileReader fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\resetKey\\districts.js");
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer jsonString = new StringBuffer();
            while(bufferedReader.ready()){
                jsonString.append(bufferedReader.readLine());
            }
            List<TestPoint> testPoints = JSON.parseArray(jsonString.toString(), TestPoint.class);

            List<Geometry> geometryList = new ArrayList<>();


            List<Param> paramList = new ArrayList<>();

            for(TestPoint testPoint : testPoints){

                paramList.add(new Param(new String[]{"name"}, new Object[]{testPoint.getName()} ));

                StringBuffer sb = new StringBuffer();
                sb.append("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Point\",\"coordinates\":[");
                sb.append(testPoint.getCenter());
                sb.append("]}}");
                System.out.println(sb.toString());
                geometryList.add(fjson.readPoint(new StringReader(sb.toString())));
            }
            PostGisUtil postGisUtil = new PostGisUtil();
            JDBCDataStore pgDatastore = postGisUtil.connPostGis();
            String[] names = {"name"};//属性名
            Class[] types = {String.class};//属性类型
            ParamType[] paramTypes = ParamType.build(names,types);
            toPointShpPostGis("districts",geometryList, pgDatastore, paramTypes, paramList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void toPointShpPostGis(String typeName ,List<Geometry> geometryList, JDBCDataStore ds, ParamType[] paramTypes, List<Param> params) throws IOException {


        if(!ds.hasSchema(typeName)) {
            //定义图形信息和属性信息
            SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
            //设置坐标系
            //创建
            tb.setCRS(DefaultGeographicCRS.WGS84);
            tb.setName(typeName);
            tb.add("the_geom", Point.class);
            for (ParamType paramType : paramTypes) {
                tb.add(paramType.getName(), paramType.getType());
            }
            ds.createSchema(tb.buildFeatureType());
        }
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriterAppend(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);

        for(int i=0,len=geometryList.size();i<len;i++){
            Geometry geometry = geometryList.get(i);
            SimpleFeature feature = writer.next();
            feature.setAttribute("the_geom", geometry);
            Param param = params.get(i);
            for (int j = 0; j < param.getNames().length; j++) {
                feature.setAttribute(param.getNames()[j], param.getValues()[j]);
            }
            writer.write();
        }
        writer.close();
        ds.dispose();
    }

}


