package com.imdroid.sideslope.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.imdroid.secapi.dto.GnssDevice;
import com.imdroid.secapi.dto.GnssDeviceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Layton
 * @date 2023/2/3 20:46
 */
@Component
public class LocalDeviceServiceImpl implements DeviceService {
    @Autowired
    private GnssDeviceMapper gnssDeviceMapper;

    private static final Cache<String, Device> deviceCache = CacheBuilder.newBuilder().
            expireAfterAccess(20, TimeUnit.MINUTES).build();

    private static final Cache<String, List<Device>> subDeviceCache = CacheBuilder.newBuilder().
            expireAfterAccess(10, TimeUnit.MINUTES).build();

    Device getDeviceFromDB(String deviceId){
        GnssDevice gnssDevice = gnssDeviceMapper.queryByDeviceId(deviceId);
        if (gnssDevice == null) {
            return null;
        }
        Device device = new Device();
        device.setTenantId(gnssDevice.getTenantid());
        device.setDeviceId(gnssDevice.getDeviceid());
        device.setDeviceType(gnssDevice.getDevicetype());
        device.setModel(gnssDevice.getModel());
        device.setParentId(gnssDevice.getParentid());
        device.setName(gnssDevice.getName());
        device.setProjectId(gnssDevice.getProject_id());
        device.setCalcGroupId(gnssDevice.getCalc_group_id());
        device.setBasicGroupId(gnssDevice.getGroup_id());
        device.setOpMode(gnssDevice.getOpmode());
        device.setFwdId(gnssDevice.getFwd_group_id());
        device.setIPose(gnssDevice.getIpose());
        device.setIPosn(gnssDevice.getIposn());
        device.setIPosd(gnssDevice.getIposd());
        device.setEcefx(gnssDevice.getEcefx());
        device.setEcefy(gnssDevice.getEcefy());
        device.setEcefz(gnssDevice.getEcefz());
        device.setLoggingmode(gnssDevice.getLoggingmode());
        return device;
    }

    public List<Device> getDeviceListFromDBByParentId(String parentId) {
        QueryWrapper<GnssDevice> query = new QueryWrapper();
        query.eq("parentid", parentId);
        List<GnssDevice> gnssDeviceList = gnssDeviceMapper.selectList(query);
        List<Device> deviceList = new ArrayList<>(gnssDeviceList.size());
        for (GnssDevice gnssDevice : gnssDeviceList) {
            Device device = findByDeviceId(gnssDevice.getDeviceid());
            if(device!=null) deviceList.add(device);
        }
        return deviceList;
    }
    @Override
    public Device findByDeviceId(String deviceId) {
        Device device = deviceCache.getIfPresent(deviceId);
        if (device == null) {
            device = getDeviceFromDB(deviceId);
            if (device != null) {
                deviceCache.put(deviceId, device);
            }
        }
        return device;
    }

    @Override
    public List<Device> findByParentId(String parentId) {
        List<Device> devices = subDeviceCache.getIfPresent(parentId);
        if (devices == null) {
            devices = getDeviceListFromDBByParentId(parentId);
            if (devices != null) {
                subDeviceCache.put(parentId, devices);
            }
        }
        return devices;
    }

    public void refresh(String deviceId, String oldParentId){
        Device device = deviceCache.getIfPresent(deviceId);
        if (device != null) {
            if(device.getParentId()!=null) {
                subDeviceCache.invalidate(device.getParentId());
                if(oldParentId!=null && !oldParentId.equals(device.getParentId())){
                    subDeviceCache.invalidate(oldParentId);
                }
            }
            deviceCache.invalidate(deviceId);
        }
    }
}
