package com.aidex.common.gps.server.impl;

import com.aidex.common.app.domain.vo.LocationVO;
import com.aidex.common.gps.domain.PushAlarmEntity;
import com.aidex.common.gps.domain.PushLocationEntity;
import com.aidex.common.gps.server.IGpsPlushService;
import com.aidex.common.plush.domain.GpsAlarmPlush;
import com.aidex.common.plush.domain.GpsLocationPlush;
import com.aidex.common.plush.domain.SysShipExtend;
import com.aidex.common.plush.service.GpsAlarmPlushService;
import com.aidex.common.plush.service.GpsLocationPlushService;
import com.aidex.common.plush.service.SysShipExtendService;
import com.aidex.common.utils.dist.CalcDist;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.domain.SysWharf;
import com.aidex.system.service.SysWharfService;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class GpsPlushService implements IGpsPlushService {

    @Autowired
    private SysShipExtendService sysShipExtendService;
    @Autowired
    private SysWharfService sysWharfService;
    @Autowired
    private GpsLocationPlushService gpsLocationPlushService;
    @Autowired
    private GpsAlarmPlushService gpsAlarmPlushService;


    public void plush(String method, String serialNumber, String data){
        // 只接收报警和位置  其他数据丢弃
        if(method.equals(METHOD.ALARM)){
            List<PushAlarmEntity> pushAlarmEntities = JSON.parseArray(data, PushAlarmEntity.class);
            alarmSource(pushAlarmEntities);
        }
        if(method.equals(METHOD.LOCATION)){
            List<PushLocationEntity> pushAlarmEntities = JSON.parseArray(data, PushLocationEntity.class);
            locationSource(pushAlarmEntities);
        }
    }

    private void alarmSource(List<PushAlarmEntity> alarmEntities){
        List<SysWharf> sysWharfs = sysWharfService.findList(new SysWharf());
        List<SysShipExtend> sysShipExtends = sysShipExtendService.findList(new SysShipExtend());
        String[] forward = ConfigUtils.getConfigByKey("forward.direction").getConfigValue().split("-");
        // 获取反向条件
        String[] reverse = ConfigUtils.getConfigByKey("reverse.direction").getConfigValue().split("-");
        if(sysShipExtends.size() > 0){
            for (PushAlarmEntity alarmEntity : alarmEntities) {
                SysShipExtend updateShipExtend = sysShipExtends.stream().filter(extend -> extend.getMacId().equals(alarmEntity.getMacid())).findFirst().orElse(null);
                // 设备是当前设备  并且方位正常则更新数据
                if(updateShipExtend != null &&
                        ((Double.valueOf(alarmEntity.getDir()) >= Double.valueOf(forward[0]) && Double.valueOf(alarmEntity.getDir()) <= Double.valueOf(forward[1]))) ||
                        (Double.valueOf(alarmEntity.getDir()) >= Double.valueOf(reverse[0]) && Double.valueOf(alarmEntity.getDir()) <= Double.valueOf(reverse[1]))){
                    updateShipExtend.setSpeed(alarmEntity.getSpeed());
                    updateShipExtend.setDir(alarmEntity.getDir());
                    updateShipExtend.setLon(alarmEntity.getMapLon());
                    updateShipExtend.setLat(alarmEntity.getMapLat());
                    updateShipExtend.setWharfStop(null);
                    updateShipExtend.setNextWharf(null);
                    if(Double.valueOf(alarmEntity.getDir()) >= Double.valueOf(forward[0]) && Double.valueOf(alarmEntity.getDir()) <= Double.valueOf(forward[1]))
                        updateShipExtend.setTravel(1L);
                    if(Double.valueOf(alarmEntity.getDir()) >= Double.valueOf(reverse[0]) && Double.valueOf(alarmEntity.getDir()) <= Double.valueOf(reverse[1]))
                        updateShipExtend.setTravel(0L);
                    if(alarmEntity.getClassify().equals(CLASSIFY.IN)){
                        SysWharf stopWharf = sysWharfs.stream().filter(wharf -> wharf.getWharfNanme().equals(alarmEntity.getNotea())).findFirst().orElse(null);
                        if(stopWharf != null){
                            updateShipExtend.setIsStop(1L);
                            updateShipExtend.setWharfStop(stopWharf.getId());
                        }
                    }
                    if(alarmEntity.getClassify().equals(CLASSIFY.OUT)){
                        SysWharf stopWharf = sysWharfs.stream().filter(wharf -> wharf.getWharfNanme().equals(alarmEntity.getNotea())).findFirst().orElse(null);
                        if(stopWharf != null){
                            updateShipExtend.setIsStop(0L);
                            // 正向行驶
                            if(updateShipExtend.getTravel() == 1L ){
                                Long nextSite = (stopWharf.getWharfOrder() / 10) - 1;
                                SysWharf nextWharf = sysWharfs.stream().filter(wharf -> wharf.getWharfOrder() == (nextSite * 10)).findFirst().orElse(null);
                                updateShipExtend.setNextWharf(nextWharf.getId());
                            }
                            // 反向行驶
                            if(updateShipExtend.getTravel() == 0L ){
                                Long nextSite = (stopWharf.getWharfOrder() / 10) + 1;
                                SysWharf nextWharf = sysWharfs.stream().filter(wharf -> wharf.getWharfOrder() == (nextSite * 10)).findFirst().orElse(null);
                                updateShipExtend.setNextWharf(nextWharf.getId());
                            }
                        }
                    }
                    sysShipExtendService.update(updateShipExtend);
                }
            }
//            GpsA alarmEntity = new GpsLocationPlush();
//            alarmEntity.setId(alarmEntity.getId());
//            alarmEntity.set(alarmEntity.getId());
//            alarmEntity.setId(alarmEntity.getId());

        }

    }

    private void locationSource(List<PushLocationEntity> locationEntities){
        List<SysShipExtend> sysShipExtends = sysShipExtendService.findList(new SysShipExtend());
        String[] forward = ConfigUtils.getConfigByKey("forward.direction").getConfigValue().split("-");
        // 获取反向条件
        String[] reverse = ConfigUtils.getConfigByKey("reverse.direction").getConfigValue().split("-");
        if(sysShipExtends.size() > 0) {
            for (PushLocationEntity location : locationEntities) {
                SysShipExtend updateShipExtend = sysShipExtends.stream().filter(extend -> extend.getMacId().equals(location.getMacid())).findFirst().orElse(null);
                // 设备是当前设备  并且方位正常则更新数据
                if (updateShipExtend != null &&
                        ((Double.valueOf(location.getDir()) >= Double.valueOf(forward[0]) && Double.valueOf(location.getDir()) <= Double.valueOf(forward[1]))) ||
                        (Double.valueOf(location.getDir()) >= Double.valueOf(reverse[0]) && Double.valueOf(location.getDir()) <= Double.valueOf(reverse[1]))) {
                    updateShipExtend.setSpeed(location.getSpeed());
                    updateShipExtend.setDir(location.getDir());
                    updateShipExtend.setLon(location.getMapLon());
                    updateShipExtend.setLat(location.getMapLat());
                    String[] paramsValue = location.getValue().split(",");
                    updateShipExtend.setMileage(paramsValue[0]);
                    updateShipExtend.setUpdateTime(System.currentTimeMillis());
                    sysShipExtendService.update(updateShipExtend);
                }
                GpsLocationPlush locationPlush = new GpsLocationPlush();
                locationPlush.setMacid(location.getMacid());
                locationPlush.setGpsTime(locationPlush.getGpsTime());
                locationPlush.setHeartTime(locationPlush.getHeartTime());
                locationPlush.setUpdTime(locationPlush.getUpdTime());
                locationPlush.setSpeed(location.getSpeed());
                locationPlush.setDir(location.getDir());
                locationPlush.setLat(location.getLat());
                locationPlush.setLon(location.getLon());
                locationPlush.setMapLat(location.getMapLat());
                locationPlush.setMapLon(location.getMapLon());
                locationPlush.setStats(location.getStats());
                locationPlush.setValue(location.getValue());
                gpsLocationPlushService.save(locationPlush);
            }
        }
    }

    public interface METHOD {
        // 报警信息接收
        String ALARM = "alarm";
        // 位置信息接收
        String LOCATION = "status";
    }

    public interface CLASSIFY {
        // 入界
        String IN = "4";
        // 出界
        String OUT = "3";
    }
}
