package org.jeecg.common.aspect.baiduAddress;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class GeoCode {
    private Integer status;

    private GeoCode.Result result;


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Result {
        private Integer precise;
        private Integer confidence;
        private Integer comprehension;
        private String level;
        //经纬度
        private GeoCode.Result.Location location;
        //格式化地址
        private String formatted_address;
        // 地址组件
        private GeoAddressComponent addressComponent;

        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        @JsonDeserialize(using = GeoCode.Result.GeoLocationDeserializer.class)
        public static class Location {
            private Double lng;
            private Double lat;
        }


        // 经纬度的精度
        public static class GeoLocationDeserializer extends StdDeserializer<GeoCode.Result.Location> {


            public GeoLocationDeserializer() {
                this(null);
            }

            @Override
            public GeoCode.Result.Location deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
                JsonNode node = p.getCodec().readTree(p);
                Double lng = GeoUtil.format(node.get("lng").doubleValue());
                Double lat = GeoUtil.format(node.get("lat").doubleValue());
                return new GeoCode.Result.Location(lng.doubleValue(), lat.doubleValue());
            }

            protected GeoLocationDeserializer(Class<?> vc) {
                super(vc);
            }


        }

    }
}
