package com.usky.sas.common.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.usky.sas.common.global.Constant;
import com.usky.sas.domain.SasConfig;
import com.usky.sas.domain.SasSystemActivation;
import com.usky.sas.domain.SasSystemConfig;
import com.usky.sas.service.SasConfigService;
import com.usky.sas.service.SasSystemActivationService;
import com.usky.sas.service.SasSystemConfigService;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(-2147483648)
public class DataInitializer {
    @Autowired
    private SasSystemActivationService systemActivationService;
    @Autowired
    private SasConfigService configService;
    @Autowired
    private SasSystemConfigService systemConfigService;
    public static final Map<Integer, Integer> bitrate = new ConcurrentHashMap();

    public DataInitializer() {
    }

    @EventListener({ContextRefreshedEvent.class})
    @Order(1)
    public void initData() throws Exception {
        if (CollUtil.isEmpty(this.systemActivationService.list())) {
            SasSystemActivation activation = new SasSystemActivation();
            activation.setId(1);
            activation.setClientId(IdUtil.randomUUID());
            this.systemActivationService.save(activation);
        }

        if (CollUtil.isEmpty(this.configService.list())) {
            SasConfig config = new SasConfig();
            config.setId(1);
            config.setKeyds(Constant.key);
            config.setHost("192.168.0.159");
            config.setPort("1883");
            config.setUsername("guest");
            config.setPassword("guest");
            config.setIsTsl(Constant.defaultIsTsl);
            this.configService.save(config);
        }

        if (CollUtil.isEmpty(this.systemConfigService.list())) {
            SasSystemConfig systemConfig = new SasSystemConfig();
            systemConfig.setId(1);
            this.systemConfigService.save(systemConfig);
        }

        bitrate.put(11, 256);
        bitrate.put(15, 512);
        bitrate.put(19, 1024);
        bitrate.put(23, 2048);
        bitrate.put(24, 3072);
        bitrate.put(25, 4096);
        bitrate.put(26, 8192);
        bitrate.put(27, 16384);
    }
}

