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

import com.aidex.common.constant.Constants;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.gps.common.GeneratorGspApi;
import com.aidex.common.gps.common.GpsApi;
import com.aidex.common.gps.common.GpsRequest;
import com.aidex.common.gps.domain.*;
import com.aidex.common.gps.server.IGpsService;
import com.aidex.common.utils.StringUtils;
import com.aidex.system.domain.SysShip;
import com.aidex.system.service.SysShipService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GpsService implements IGpsService {

    @Autowired
    private GeneratorGspApi generatorGspApi;
    @Autowired
    private GpsRequest gpsRequest;
    @Autowired
    private SysShipService sysShipService;
    @Autowired
    private RedisCache redisCache;


    public String getLocationUrl(String shipId){
        SysShip sysShip = sysShipService.get(shipId);
        return "";
//        return generatorGspApi.GenerationMapUrl(GeneratorGspApi.RquestMethod.follow,sysShip.getShipNum(), GeneratorGspApi.GpsMapType.QQ);
    }

    public String getPlayUrl(String shipId){
        SysShip sysShip = sysShipService.get(shipId);
        String userInfo = redisCache.getStringValue(Constants.GPS_LOGIN_INFO_PREFIX);
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findDeviceInfo);
        params.put("macid",sysShip.getShipNum());
        DeviceEntity deviceEntity = gpsRequest.post(params).toJavaObject(DeviceEntity.class);
        return generatorGspApi.GenerationMapUrl(GeneratorGspApi.GpsMapType.QQ,sysShip.getShipNanme(),deviceEntity.getObjectid(), JSONObject.parse(userInfo).getString("id"));
    }

    public LocationEntity getLocation(String shipId){
        SysShip sysShip = sysShipService.get(shipId);
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocation);
        params.put("macid",sysShip.getShipNum());
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        LocationEntity locationEntitie = gpsRequest.post(params).toJavaObject(LocationEntity.class);
        return locationEntitie;
    }

    public List<LocationEntity> getLocationBatch(){
        List<SysShip> sysShips = sysShipService.findList(new SysShip());
        String macIds = sysShips.stream().map(SysShip::getShipNum).collect(Collectors.joining(","));
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocationByBatch);
        params.put("macids",macIds);
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        List<LocationEntity> locationEntities = gpsRequest.postArray(params).toJavaList(LocationEntity.class);
         return locationEntities;
    }

    public List<LocationEntity> getLocationBatchByMac(String macIds){
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocationByBatch);
        params.put("macids",macIds);
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        List<LocationEntity> locationEntities = gpsRequest.postArray(params).toJavaList(LocationEntity.class);
        return locationEntities;
    }

    public List<MileageEntity> getMileageStatisics(String startTime,String endTime){
        Long startOfDay, endOfDay ;
        if(StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)){
            LocalDate today = LocalDate.now();
            startOfDay = today.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
            endOfDay = LocalDateTime.of(today, LocalTime.MAX).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        }else {
            startOfDay = Long.valueOf(startTime);
            endOfDay = Long.valueOf(endTime);
        }
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.mileageStatustics);
        params.put("startTime",startTime);
        params.put("endTime", endOfDay);
        List<MileageEntity> mileageEntities = gpsRequest.post(params).getJSONArray("rows").toJavaList(MileageEntity.class);
        return mileageEntities;
    }


    public List<FenceEntity> findFenceIn(String macId,String fenceName,Long startTime, Long endTime){
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocationByBatch);
        params.put("fenceType",1);
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        params.put("macid", macId);
        params.put("fenceName", fenceName);
        params.put("fenceStatus", 1);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        List<FenceEntity> fenceEntities = gpsRequest.postArray(params).toJavaList(FenceEntity.class);
        return fenceEntities;
    }


    public List<FenceEntity> findFenceInByMacId(String macId,Long startTime, Long endTime){
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findLocationByBatch);
        params.put("fenceType",1);
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        params.put("macid", macId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        List<FenceEntity> fenceEntities = gpsRequest.postArray(params).toJavaList(FenceEntity.class);
        return fenceEntities;
    }


    public List<AccEntity> findAccByMacId(String macId, Long startTime, Long endTime){
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findAcc);
        params.put("macid", macId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("accState", "1");
        List<AccEntity> accEntities = gpsRequest.postArray(params).toJavaList(AccEntity.class);
        return accEntities;
    }

    public List<MyGpsEntity> findMyGps(){
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.findUserDevices);
        List<MyGpsEntity> myG = gpsRequest.postArray(params).toJavaList(MyGpsEntity.class);
        return myG;
    }

    public List<AlarmDatetailsEntity> findAlarmDetails(String macid,String alarmTypes, Long startTime, Long endTime){
        Map<String, Object> params = generatorGspApi.GeneratorQueryParams(GpsApi.alarmDetails);
        params.put("macid",macid);
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("mapType", GeneratorGspApi.GpsMapType.QQ);
        params.put("alarmTypes", alarmTypes);
        List<AlarmDatetailsEntity> alarmDatetailsEntities = gpsRequest.postArray(params).toJavaList(AlarmDatetailsEntity.class);
        return alarmDatetailsEntities;
    }

}
