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

import com.aidex.common.app.domain.vo.LocationVO;
import com.aidex.common.app.domain.vo.SysWharfVO;
import com.aidex.common.app.server.IAppService;
import com.aidex.common.exception.SysException;
import com.aidex.common.gps.domain.LocationEntity;
import com.aidex.common.gps.server.IGpsService;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.dist.CalcDist;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.domain.SysNotice;
import com.aidex.system.domain.SysWharf;
import com.aidex.system.service.SysNoticeService;
import com.aidex.system.service.SysWharfService;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class AppService implements IAppService {

    @Autowired
    private SysNoticeService sysNoticeService;
    @Autowired
    private SysWharfService sysWharfService;
    @Autowired
    private IGpsService iGpsService;


    public List<SysNotice> getCarouselList(){

        List<SysNotice> carouselList = sysNoticeService.findList(new SysNotice(){{setCarousel("1");}});
        return carouselList;
    }

    public List<SysNotice> getNoticeList(){
        List<SysNotice> noticeList = sysNoticeService.findList(new SysNotice(){{setCarousel("0");setNoticeType("1");}});
        return noticeList;
    }

    public SysNotice findCarouselAndNoticeDetails(String id){
        return sysNoticeService.get(id);
    }

    public List<SysWharf> findSysWharfList(){
        return sysWharfService.findList(new SysWharf());
    }

    /*
    * direction 方向 true正向 false反向
    * */
    public List<LocationVO> findAllLocation(Boolean direction){
        // 全部船只信息
        List<LocationEntity> locationEntities = iGpsService.getLocationBatch();
        if(locationEntities.size() <= 0)
            throw new SysException(8821,"暂无运行船只");

        // 返回结果
        List<LocationVO> locationList = new ArrayList<>();

        Integer stopSpeed = Integer.valueOf(ConfigUtils.getConfigByKey("stop.speed").getConfigValue());

        /* 正向运行 */
        if(direction){
            // 获取正向条件
            String[] front = ConfigUtils.getConfigByKey("forward.direction").getConfigValue().split("-");
            List<LocationEntity> frontList = locationEntities.stream().filter(local -> ((Double.valueOf(local.getDir()) >= Double.valueOf(front[0])) && (Double.valueOf(local.getDir()) <= Double.valueOf(front[1])))).collect(Collectors.toList());
            for (LocationEntity locationEntity : frontList) {
                LocationVO locationVO = new LocationVO();
                BeanUtils.copyProperties(locationEntity,locationVO);
                locationVO.setIsArrival(Boolean.FALSE);
                // 查询全部停止船只判断是否到站
                if(Integer.valueOf(locationVO.getSpeed()) <= stopSpeed){
                    locationVO.setIsArrival(Boolean.TRUE);
                }
                locationList.add(locationVO);
            }

        }else {
            // 获取反向条件
            String[] reverse = ConfigUtils.getConfigByKey("reverse.direction").getConfigValue().split("-");
            List<LocationEntity> reverseList = locationEntities.stream().filter(local -> ((Double.valueOf(local.getDir()) >= Double.valueOf(reverse[0])) && (Double.valueOf(local.getDir()) <= Double.valueOf(reverse[1])))).collect(Collectors.toList());
            for (LocationEntity locationEntity : reverseList) {
                LocationVO locationVO = new LocationVO();
                BeanUtils.copyProperties(locationEntity,locationVO);
                locationVO.setIsArrival(Boolean.FALSE);
                // 查询全部停止船只判断是否到站
                if(Integer.valueOf(locationVO.getSpeed()) <= stopSpeed){
                    locationVO.setIsArrival(Boolean.TRUE);
                }
                locationList.add(locationVO);
            }
        }
        return locationList;
    }


    public List<LocationVO> findAllLocationByNext(Boolean direction,String lat, String lon){
        // 全部船只信息
        List<LocationEntity> locationEntities = iGpsService.getLocationBatch();
        if(locationEntities.size() <= 0)
            throw new SysException(8821,"暂无运行船只");

        // 返回结果
        List<LocationVO> locationList = new ArrayList<>();
        // 正反向计算数据
        List<LocationEntity> calcList = new ArrayList<>();
        // 停靠时间
        Integer stopSpeed = Integer.valueOf(ConfigUtils.getConfigByKey("stop.speed").getConfigValue());

        // 正反向计算条件
        String[] conditions ;
        /* 正向运行 */
        if(direction){
            // 获取正向条件
            conditions = ConfigUtils.getConfigByKey("forward.direction").getConfigValue().split("-");
            calcList = locationEntities.stream().filter(local -> ((Double.valueOf(local.getDir()) >= Double.valueOf(conditions[0])) && (Double.valueOf(local.getDir()) <= Double.valueOf(conditions[1])))).collect(Collectors.toList());
        }else {
            // 获取反向条件
            conditions = ConfigUtils.getConfigByKey("reverse.direction").getConfigValue().split("-");
            calcList = locationEntities.stream().filter(local -> ((Double.valueOf(local.getDir()) >= Double.valueOf(conditions[0])) && (Double.valueOf(local.getDir()) <= Double.valueOf(conditions[1])))).collect(Collectors.toList());
        }

        for (LocationEntity locationEntity : calcList) {
            LocationVO locationVO = new LocationVO();
            BeanUtils.copyProperties(locationEntity,locationVO);
            locationVO.setIsArrival(Boolean.FALSE);
            Integer stopTime = 0;
            // 查询全部停止船只判断是否到站
            if(Integer.valueOf(locationVO.getSpeed()) <= stopSpeed){
                locationVO.setIsArrival(Boolean.TRUE);
                // 如果处于停止状态则计算停留剩余时间
                stopTime = CalcDist.calcStopTime(locationEntity.getUpdtime());
            }

            locationList.add(locationVO);
        }
        return locationList;
    }

    public List<SysWharfVO> findNearestStation(String lat, String lon){
        if(StringUtils.isEmpty(lat) || StringUtils.isEmpty(lon))
            throw new SysException(4441,"请告诉我你在哪");
        List<SysWharf> listWharf = sysWharfService.findList(new SysWharf());
        List<SysWharfVO> vos = new ArrayList<>();
        for (SysWharf sysWharf : listWharf) {
            SysWharfVO wharfVO = new SysWharfVO();
            BeanUtils.copyProperties(sysWharf,wharfVO);
            wharfVO.setDistance(CalcDist.calculateDistance(Double.valueOf(lat),Double.valueOf(lon),
                    Double.valueOf(sysWharf.getLat()),Double.valueOf(sysWharf.getLng())));
            vos.add(wharfVO);
        }
        return  vos.stream().sorted(Comparator.comparing(SysWharfVO::getDistance))
                .collect(Collectors.toList());
    }


}
