package com.tofly.ghrq_route.module.gps.domain;

import com.amap.api.location.AMapLocation;
import com.esri.arcgisruntime.geometry.Point;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.ArcgisUtils.ArcgisUtils;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Unique;

import java.util.Date;

/**
 * Created by Amu on 2017/3/15.
 */
@Entity(nameInDb = "DB_GPS_SAVED")
public class GPSDataBean {
    /**
     * location_x : 1
     * location_y : 1
     * location_longitude : 1
     * location_latitude : 1
     * address : sample string 1
     * direction : 1
     * network : 1
     * gpstime : 2017-03-15T13:24:19.9756603+08:00
     * notes : sample string 2
     */
    public static final int GPS_ISOVER_UNVALID = 0;
    public static final int GPS_ISOVER_VALID = 1;

    @Id @Unique @NotNull
    @Expose() @SerializedName("gpsId")
    private String gps_id;
    @Expose() @SerializedName("locationX")
    private String lng;     //GPS经度[必填]
    @Expose() @SerializedName("locationY")
    private String lat;      //GPS纬度[必填]
    private String address;             //地址[必填]
    @Expose()
    private String direction;              //方向
    @Expose()
    private String speed = "0";               //计算速度[必填]
    @Expose()
    private String distance = "0";            //计算距离
    @Expose() @SerializedName("gpsTimespan")
    private String gps_timespan = "0";        //时间间隔
    @Expose() @SerializedName("gpsSpeed")
    private String gps_speed = "0";           //系统自带
    @Expose()
    private int network;                //(gps:1,基站:2,wifi:3)[必填]
    @Expose() @SerializedName("gpstime")
    private String gpstime;             //GPSTIME[必填]
    @Expose() @SerializedName("checkTime")
    private String check_time;          //本地时间
    @Expose() @SerializedName("gpsType")
    private int gps_type;               //轨迹类型 0：人巡 1：车巡 [必填]
    @Expose()
    private String carid;               //车巡时的车id
    @Expose() @SerializedName("gpsStar")
    private int gps_star;               //卫星颗数[可填]
    @Expose() @SerializedName("gpsPrecision")
    private String gps_precision;       //定位精度[可填]
    private String notes;               //备注
    @Expose() @SerializedName("isvalid")
    private int isValid = 1;            //0:漂移点(精度>=200) 1：非漂移的点 2:该点的速度/距离需要重新计算
    @Expose() @SerializedName("gpsSection")
    private long section = 1;
    @Expose()
    private int state = -1;            // 默认-1 。 0：该点没有计划点到位 1：有计划的点到位
    private long sid;
    //private int gps_state;              //0无效 1有效


    @Generated(hash = 199930670)
    public GPSDataBean(@NotNull String gps_id, String lng, String lat, String address, String direction, String speed, String distance,
            String gps_timespan, String gps_speed, int network, String gpstime, String check_time, int gps_type, String carid, int gps_star,
            String gps_precision, String notes, int isValid, long section, int state, long sid) {
        this.gps_id = gps_id;
        this.lng = lng;
        this.lat = lat;
        this.address = address;
        this.direction = direction;
        this.speed = speed;
        this.distance = distance;
        this.gps_timespan = gps_timespan;
        this.gps_speed = gps_speed;
        this.network = network;
        this.gpstime = gpstime;
        this.check_time = check_time;
        this.gps_type = gps_type;
        this.carid = carid;
        this.gps_star = gps_star;
        this.gps_precision = gps_precision;
        this.notes = notes;
        this.isValid = isValid;
        this.section = section;
        this.state = state;
        this.sid = sid;
    }

    @Generated(hash = 1814363088)
    public GPSDataBean() {
    }
    

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public int getNetwork() {
        return network;
    }

    public void setNetwork(int network) {
        this.network = network;
    }

    public String getGpstime() {
        return gpstime;
    }

    public void setGpstime(String gpstime) {
        this.gpstime = gpstime;
    }

    public String getCheck_time() {
        return check_time;
    }

    public void setCheck_time(String check_time) {
        this.check_time = check_time;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getGps_id() {
        return this.gps_id;
    }

    public void setGps_id(String gps_id) {
        this.gps_id = gps_id;
    }

    public int getGps_type() {
        return gps_type;
    }

    public void setGps_type(int gps_type) {
        this.gps_type = gps_type;
    }

    public int getGps_star() {
        return this.gps_star;
    }

    public void setGps_star(int gps_star) {
        this.gps_star = gps_star;
    }

    public String getGps_precision() {
        return this.gps_precision;
    }

    public void setGps_precision(String gps_precision) {
        this.gps_precision = gps_precision;
    }

    public String getSpeed() {
        return this.speed;
    }

    public void setSpeed(String speed) {
        this.speed = speed;
    }

    public String getCarid() {
        return this.carid;
    }

    public void setCarid(String carid) {
        this.carid = carid;
    }

    public int getIsValid() {
        return isValid;
    }

    public void setIsValid(int isValid) {
        this.isValid = isValid;
    }

    public long getSection() {
        return section;
    }

    public void setSection(long section) {
        this.section = section;
    }

    public String getDistance() {
        return this.distance;
    }

    public void setDistance(String distance) {
        this.distance = distance;
    }

    public String getGps_timespan() {
        return this.gps_timespan;
    }

    public void setGps_timespan(String gps_timespan) {
        this.gps_timespan = gps_timespan;
    }

    public String getGps_speed() {
        return this.gps_speed;
    }

    public void setGps_speed(String gps_speed) {
        this.gps_speed = gps_speed;
    }

    public String getLng() {
        return this.lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public String getLat() {
        return this.lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public long getSid() {
        return this.sid;
    }

    public void setSid(long sid) {
        this.sid = sid;
    }

    /*public static GPSDataBean GpsDataFromBDLocation(UserBean userBean, BDLocation bdLocation){
        GPSDataBean gpsDataBean = new GPSDataBean();
        Point wgs84Point = LocationUtil.bd09ToWgs84(new Point(bdLocation.getLatitude(), bdLocation.getLongitude()));
        gpsDataBean.setLng(String.valueOf(wgs84Point.getX()));
        gpsDataBean.setLat(String.valueOf(wgs84Point.getY()));

        int network_type = GpsTool.LOCATION_TYPE_GPS;
        int bdGpsType = bdLocation.getLocType();
        if (bdGpsType == BDLocation.TypeGpsLocation) {
            network_type = GpsTool.LOCATION_TYPE_GPS;
        } else if (bdGpsType == BDLocation.TypeNetWorkLocation) {
            int networkState = NetUtil.getNetworkState(BaseApp.getInstance());
            if (networkState == NetUtil.NETWORN_WIFI) {
                network_type = GpsTool.LOCATION_TYPE_WIFI;
            } else if (networkState == NetUtil.NETWORN_MOBILE) {
                network_type = GpsTool.LOCATION_TYPE_CELL;
            }
        }
        gpsDataBean.setNetwork(network_type);
        gpsDataBean.setGpstime(bdLocation.getTime());
        gpsDataBean.setCheck_time(DateUtils.getDate(new Date().getTime()));
        String speed = String.format("%.2f", bdLocation.getSpeed());
        gpsDataBean.setGps_speed(speed);
        boolean carMode = userBean.isCarMode();
        gpsDataBean.setGps_type(carMode ? 1 : 0);
        gpsDataBean.setCarid(userBean.getCarid());
        int gpsStar = bdLocation.getSatelliteNumber();
        gpsDataBean.setGps_star(gpsStar);
        String accuracy = String.format("%.2f", bdLocation.getRadius());
        gpsDataBean.setGps_precision(accuracy);

        return gpsDataBean;
    }*/

    public static GPSDataBean GpsDataFromAMapLocation(UserBean userBean, AMapLocation aMapLocation){
        GPSDataBean gpsDataBean = new GPSDataBean();

        LocationUtil_xu.CoordinateClass coordinate = LocationUtil_xu.gcj02towgs84(aMapLocation.getLongitude(), aMapLocation.getLatitude());
        if (coordinate != null){
            Point locProj = ArcgisUtils.loctionProjectDefault(coordinate.Longtitude, coordinate.Latitude);
            if (locProj != null) {
                String lng = String.format("%.6f", locProj.getX());
                String lat = String.format("%.6f", locProj.getY());
                gpsDataBean.setLng(lng);
                gpsDataBean.setLat(lat);
            }
        }
        gpsDataBean.setNetwork(aMapLocation.getLocationType());
        gpsDataBean.setGpstime(DateUtils.getDate(aMapLocation.getTime()));
        gpsDataBean.setCheck_time(DateUtils.getDate(new Date().getTime()));
        gpsDataBean.setAddress(aMapLocation.getAddress());
        String speed = String.format("%.2f", aMapLocation.getSpeed());
        gpsDataBean.setGps_speed(speed);
        boolean carMode = userBean.isCarMode();
        gpsDataBean.setGps_type(carMode ? 1 : 0);
        gpsDataBean.setCarid(userBean.getCarid());
        int gpsStar = aMapLocation.getSatellites();
        gpsDataBean.setGps_star(gpsStar);
        String accuracy = String.format("%d", (int)aMapLocation.getAccuracy());
        gpsDataBean.setGps_precision(accuracy);

        return gpsDataBean;
    }

    public int getState() {
        return this.state;
    }

    public void setState(int state) {
        this.state = state;
    }
}
