package cn.com.taiji.track.dto;

import cn.com.taiji.track.entity.ShipStatusEntity;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.util.factory.Hints;
import org.opengis.feature.simple.SimpleFeature;

import java.io.Serializable;
import java.util.Date;


/**
 * @author kok20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BeidouShipLocationDTO extends IGeomesaTrackDTO implements Serializable {

    private static final long serialVersionUID = -8983779632713666636L;

    private String deviceId;
    private String location;
    private String shipType;
    private String workType;
    private String workWay;
    private String sendTime;
    private String locationTime;
    private String online;
    private String shipLength;
    private String shipWidth;
    private String texture;
    private String longitude;
    private String latitude;
    private String direction;
    private String speed;
    private String kwh;
    private String shipName;
    private String isOnline;
    private String isInport;
    private String isAnchor;
    private String deviceStatus;
    private String xwDeptId;

    @Override
    public boolean checkPoint() {
        if (location != null) {
            longitude = location.substring(location.indexOf("(")+1,location.indexOf(" ",location.indexOf("(")));
            latitude = location.substring(location.indexOf(" ",location.indexOf("("))+1,location.indexOf(")"));
            return true;
        }
        return false;
    }

    @Override
    public String getTypeName() { return "beidou-data"; }

    @Override
    public String getLayerType() {
        return "geo_beidou_ship";
    }

    @Override
    public String getWsLayerName() {
        return "beidou-ship";
    }

    @Override
    public String getFid() {
        return deviceId;
    }

    @Override
    public StringBuilder getSimpleFeatureType(){
            StringBuilder attributes = new StringBuilder();
            attributes.append("deviceId:String:index=true,");
            attributes.append("*location:Point:srid=4326,");
            attributes.append("shipType:String,");
            attributes.append("workType:String,");
            attributes.append("workWay:String,");
            attributes.append("sendTime:String,");
            attributes.append("locationTime:String,");
            attributes.append("online:String,");
            attributes.append("shipLength:String,");
            attributes.append("shipWidth:String,");
            attributes.append("texture:String,");
            attributes.append("longitude:String,");
            attributes.append("latitude:String,");
            attributes.append("direction:String,");
            attributes.append("speed:Double,");
            attributes.append("kwh:String,");
            attributes.append("shipName:String,");
            attributes.append("isOnline:String,");
            attributes.append("isInport:String,");
            attributes.append("isAnchor:String,");
            attributes.append("deviceStatus:String,");
            attributes.append("xwDeptId:String,");
            attributes.append("layerType:String,");
            attributes.append("syncTime:Date");
            return attributes;
    }

    @Override
    public SimpleFeature toSimpleFeature(SimpleFeatureBuilder builder, String fid) {
        builder.set("deviceId", deviceId);
        builder.set("location", location);
        builder.set("shipType", shipType);
        builder.set("workType", workType);
        builder.set("workWay", workWay);
        builder.set("sendTime", sendTime);
        builder.set("locationTime", locationTime);
        builder.set("online", online);
        builder.set("shipLength", shipLength);
        builder.set("shipWidth", shipWidth);
        builder.set("texture", texture);
        builder.set("longitude", longitude);
        builder.set("latitude", latitude);
        builder.set("direction", direction);
        builder.set("speed", Double.valueOf(speed));
        builder.set("kwh", kwh);
        builder.set("shipName", shipName);
        builder.set("isOnline", isOnline);
        builder.set("isInport", isInport);
        builder.set("isAnchor", isAnchor);
        if(deviceStatus!=null){
            builder.set("deviceStatus", deviceStatus);
        }else{
            builder.set("deviceStatus", 0);
        }
        builder.set("xwDeptId", xwDeptId);

        builder.set("layerType", getLayerType());
        builder.set("syncTime", new Date());
        builder.featureUserData(Hints.USE_PROVIDED_FID, Boolean.TRUE);
        return builder.buildFeature(fid);
    }

    @Override
    public String[] getPropList(){
        return new String[] {
                "deviceId",
                "location",
                "shipType",
                "workType",
                "workWay",
                "sendTime",
                "locationTime",
                "online",
                "shipLength",
                "shipWidth",
                "texture",
                "longitude",
                "latitude",
                "direction",
                "speed",
                "kwh",
                "shipName",
                "isOnline",
                "isInport",
                "isAnchor",
                "deviceStatus",
                "xwDeptId",
                "layerType",
                "syncTime"
        };
    }

    @Override
    public Object[] getValueList() {
        return new Object[] {
                deviceId,
                location,
                shipType,
                workType,
                workWay,
                sendTime,
                locationTime,
                online,
                shipLength,
                shipWidth,
                texture,
                longitude,
                latitude,
                direction,
                speed,
                kwh,
                shipName,
                isOnline,
                isInport,
                isAnchor,
                deviceStatus==null ? 0 : deviceStatus,
                xwDeptId,
                getLayerType(),
                new Date()
        };
    }
}
