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

import com.google.gson.annotations.SerializedName;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.PatternSyntaxException;

/**
 * Created by lihualong on 2017/11/3.
 */
public class DeviceUploadBean {
    /**
     * userid : 0F36E15B51504C4E848A884636D5C3C5
     * avatar : file/8FD9910CB3854FA0BB994C3084F1E7B9.png
     * username : 庄磊
     * depid : 956A5C40D5204C8A84BC15C25B9110A7
     * depname : 巡检组
     * lat : 0
     * lng : 0
     * upload_state : 0
     * data_flow :
     * on_time : null
     * off_time : null
     * equip_info : []
     */
    @SerializedName("id")
    private String userid;
    private String avatar;
    private String realname;
    private String deptid;
    private String deptname;
    private Latlng x_y;
    private String logintime;
    private String ontime;
    private String offtime;
    private List<EquitInfo> equip_info;
    private EquitInfo equit_Info_0;
//    上班状态 1:上班 2：下班
//    private int sign_state;

    public DeviceUploadBean() {
        x_y = new Latlng();
    }

    public DeviceUploadBean(DeviceUploadBean uploadBean) {
        this.userid = uploadBean.userid;
        this.avatar = uploadBean.avatar;
        this.realname = uploadBean.realname;
        this.deptid = uploadBean.deptid;
        this.deptname = uploadBean.deptname;
        this.x_y = uploadBean.x_y;
        this.logintime = uploadBean.logintime;
        this.ontime = uploadBean.ontime;
        this.offtime = uploadBean.offtime;
        this.equip_info = uploadBean.equip_info;
        this.equit_Info_0 = uploadBean.equit_Info_0;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getDeptid() {
        return deptid;
    }

    public void setDeptid(String deptid) {
        this.deptid = deptid;
    }

    public String getDeptname() {
        return deptname;
    }

    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }

    public Latlng getLat_lng() {
        return x_y;
    }

    public void setLat_lng(Latlng lat_lng) {
        this.x_y = lat_lng;
    }

    public double getX() {
        double x = 0;
        if (x_y != null && x_y.x != null) {
            x = x_y.x;
        }
        return x;
    }

    public void setX(double x) {
        this.x_y.x = x;
    }

    public double getY() {
        double y = 0;
        if (x_y != null && x_y.y != null) {
            y = x_y.y;
        }
        return y;
    }

    public void setY(double y) {
        this.x_y.y = y;
    }

    public String getLogintime() {
        return logintime;
    }

    public void setLogintime(String logintime) {
        this.logintime = logintime;
    }

    public String getOnTime() {
        String time = ontime;
        if (ontime != null && !ontime.isEmpty()){
            try {
                String[] times = ontime.split(",");
                if (times != null && times.length > 0)
                    time = times[0];
            }catch (PatternSyntaxException ex){
                time = ontime;
            }
        }
        return time;
    }

    public void setOnTime(String on_time) {
        this.ontime = on_time;
    }

    public String getOffTime() {
        return offtime;
    }

    public void setOffTime(String offtime) {
        this.offtime = offtime;
    }

    public EquitInfo getEquip_info() {
        if (equip_info != null && equip_info.size() > 0) {
            equit_Info_0 = equip_info.get(0);
        }
        return equit_Info_0;
    }

    public void setEquip_info(EquitInfo equip_info) {
        this.equit_Info_0 = equip_info;
    }

    public void setEquip_info(List<EquitInfo> equip_info) {
        this.equip_info = equip_info;
    }

    public static class Latlng{
        public Double x;
        public Double y;
    }

    public static class EquitInfo{
        public static final int GPS_STATE_ONLINE = 1;
        public static final int GPS_STATE_OFF = 0;
        public static final int GPS_STATE_ERROR = 2;

        public static final int NETWORK_STATE_ONLINE = 1;
        /**
         * id : C31590DC6DA6407BAB1AE10B4F73A55B
         * sid : 3264319
         * userid : 8E42247D5F7E4A6989947A667EBC95AE
         * gps_state : 1
         * network_type : 2
         * network_state : 1
         * check_time : 2017-11-29 10:11:48
         * upload_time : 2017-11-29 10:11:45
         * electricity : 71
         * work_state : 2
         * gps_precision :
         * gps_star : 0
         * residual_space : 26266
         */

        private String id;
        private int sid;
        private String userid;
        private int gpsState;
        private int networkType;
        private int networkState;
        private String checkTime;
        private String uploadtime;
        private String electricity;
        private int workState;
        private String gpsPrecision;
        private int gpsStar;
        private String residualSpace;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public int getSid() {
            return sid;
        }

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

        public String getUserid() {
            return userid;
        }

        public void setUserid(String userid) {
            this.userid = userid;
        }

        public int getGps_state() {
            return gpsState;
        }

        public void setGps_state(int gps_state) {
            this.gpsState = gps_state;
        }

        public int getNetwork_type() {
            return networkType;
        }

        public void setNetwork_type(int network_type) {
            this.networkType = network_type;
        }

        public int getNetwork_state() {
            return networkState;
        }

        public void setNetwork_state(int network_state) {
            this.networkState = network_state;
        }

        public String getCheck_time() {
            return checkTime;
        }

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

        public String getUpload_time() {
            return uploadtime;
        }

        public void setUpload_time(String upload_time) {
            this.uploadtime = upload_time;
        }

        public String getElectricity() {
            return electricity;
        }

        public void setElectricity(String electricity) {
            this.electricity = electricity;
        }

        public int getWork_state() {
            return workState;
        }

        public void setWork_state(int work_state) {
            this.workState = work_state;
        }

        public String getGps_precision() {
            return gpsPrecision;
        }

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

        public int getGps_star() {
            return gpsStar;
        }

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

        public String getResidual_space() {
            return residualSpace;
        }

        public void setResidual_space(String residual_space) {
            this.residualSpace = residual_space;
        }
    }

    public int get_sign_state() {
        int sign_state = -1;
        EquitInfo equitInfo = getEquip_info();
        if (equitInfo != null) {
            int work_state = equitInfo.getWork_state();
            if (work_state >= 0) {
                if (work_state == 3 || work_state == 0) {      //已经下班
                    sign_state = 0;
                    return sign_state;
                }

                if (ontime != null && !ontime.isEmpty() && offtime != null && !offtime.isEmpty()) {
                    int compare = DateUtils.getTimeCompare(ontime, offtime);  //签到时间小于签退时间，表明已经下班
                    if (compare <= 0) {
                        sign_state = 0;
                        return sign_state;
                    }
                }

                int gps_state = equitInfo.getGps_state();
                int network_state = equitInfo.getNetwork_state();
                if (DeviceUploadBean.EquitInfo.GPS_STATE_ONLINE == gps_state
                        && DeviceUploadBean.EquitInfo.NETWORK_STATE_ONLINE == network_state) {
                    sign_state = 1;
                } else if (DeviceUploadBean.EquitInfo.GPS_STATE_OFF == gps_state
                        && DeviceUploadBean.EquitInfo.NETWORK_STATE_ONLINE == network_state) {
                    sign_state = 2;
                } else if (DeviceUploadBean.EquitInfo.GPS_STATE_ERROR == gps_state
                        && DeviceUploadBean.EquitInfo.NETWORK_STATE_ONLINE == network_state) {
                    sign_state = 3;
                } else {
                    sign_state = 4;
                }
            } else {
                sign_state = 0;
            }
        }
        return sign_state;
    }

    public static final String USERID_KEY = "userid";
    public static final String AVATAR = "avatar";
    public static final String USERNAME = "username";
    public static final String DEPID = "depid";
    public static final String DEPNAME = "depname";
    public static final String LAT = "lat";
    public static final String LNG = "lng";
    public static final String ON_TIME = "on_time";
    public static final String OFF_TIME = "off_time";
    public static final String INFO_ID = "info_id";
    public static final String INFO_WORKSTATE = "info_workstate";
    public static final String INFO_GPSSTATE = "info_gpsstate";
    public static final String INFO_NETWORKSTATE = "info_networkstate";

    public HashMap<String, Object> getAttrsFromDevice(){
        HashMap<String, Object> attributes = new HashMap<>();
        attributes.put(USERID_KEY, getUserid());
        attributes.put(AVATAR, getAvatar());
        attributes.put(USERNAME, getRealname());
        attributes.put(DEPID, getDeptid());
        attributes.put(DEPNAME, getDeptname());
        attributes.put(LAT, getY());
        attributes.put(LNG, getX());
        attributes.put(ON_TIME, getOnTime());
        attributes.put(OFF_TIME, getOffTime());

        DeviceUploadBean.EquitInfo equip_info = getEquip_info();
        if (equip_info != null) {
            attributes.put(INFO_ID, equip_info.getId());
            attributes.put(INFO_WORKSTATE, equip_info.getWork_state());
            attributes.put(INFO_GPSSTATE, equip_info.getGps_state());
            attributes.put(INFO_NETWORKSTATE, equip_info.getNetwork_state());
        }
        return attributes;
    }

    public static DeviceUploadBean getDeviceFromAttrs(Map<String, Object> attributes){
        DeviceUploadBean deviceUploadBean = new DeviceUploadBean();
        if (attributes != null) {
            deviceUploadBean.setRealname((String) attributes.get(USERNAME));
            deviceUploadBean.setUserid((String) attributes.get(USERID_KEY));
            deviceUploadBean.setAvatar((String) attributes.get(AVATAR));
            deviceUploadBean.setDeptid((String) attributes.get(DEPID));
            deviceUploadBean.setDeptname((String) attributes.get(DEPNAME));
            deviceUploadBean.setOffTime((String) attributes.get(OFF_TIME));
            deviceUploadBean.setOnTime((String) attributes.get(ON_TIME));
            deviceUploadBean.setX((double) attributes.get(LNG));
            deviceUploadBean.setY((double) attributes.get(LAT));

            Object info_id = attributes.get(INFO_ID);
            if (info_id != null) {
                DeviceUploadBean.EquitInfo equitInfo = new DeviceUploadBean.EquitInfo();
                equitInfo.setId((String) info_id);
                Object work_state = attributes.get(INFO_WORKSTATE);
                if (work_state != null) {
                    equitInfo.setWork_state((Integer) work_state);
                }
                Object gps_state = attributes.get(INFO_GPSSTATE);
                if (gps_state != null) {
                    equitInfo.setGps_state((Integer) gps_state);
                }
                Object network_state = attributes.get(INFO_NETWORKSTATE);
                if (network_state != null) {
                    equitInfo.setNetwork_state((Integer) network_state);
                }
                deviceUploadBean.setEquip_info(equitInfo);
            }
        }
        return deviceUploadBean;
    }
}
