package com.fs.live.service.impl;

import com.alibaba.fastjson.JSON;
import com.fs.common.constant.LiveKeysConstant;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.fs.live.domain.LiveCommentFeatureConfig;
import com.fs.live.mapper.LiveCommentFeatureConfigMapper;
import com.fs.live.service.ILiveCommentFeatureConfigService;
import com.fs.live.service.LiveAppWebSocketNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class LiveCommentFeatureConfigServiceImpl implements ILiveCommentFeatureConfigService {

    private static final Long CONFIG_ID = 1L;

    @Autowired
    private LiveCommentFeatureConfigMapper configMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private LiveAppWebSocketNotifyService liveAppWebSocketNotifyService;

    @Override
    public LiveCommentFeatureConfig getEffectiveConfig() {
        LiveCommentFeatureConfig cached = redisCache.getCacheObject(LiveKeysConstant.LIVE_COMMENT_FEATURE_CONFIG_ROW);
        if (cached != null) {
            return cached;
        }
        LiveCommentFeatureConfig row = configMapper.selectByConfigId(CONFIG_ID);
        if (row == null) {
            row = defaultConfig();
        }
        redisCache.setCacheObject(LiveKeysConstant.LIVE_COMMENT_FEATURE_CONFIG_ROW, row,
                LiveKeysConstant.LIVE_COMMENT_FEATURE_CONFIG_EXPIRE_SEC, TimeUnit.SECONDS);
        return row;
    }

    @Override
    public void evictConfigCache() {
        redisCache.deleteObject(LiveKeysConstant.LIVE_COMMENT_FEATURE_CONFIG_ROW);
    }

    @Override
    public String buildConfigPushJson() {
        LiveCommentFeatureConfig cfg = getEffectiveConfig();
        Map<String, Object> wrap = new HashMap<>(2);
        wrap.put("config", cfg);
        return JSON.toJSONString(wrap);
    }

    @Override
    public int updateConfig(LiveCommentFeatureConfig config) {
        config.setConfigId(CONFIG_ID);
        if (config.getUpdateTime() == null) {
            config.setUpdateTime(DateUtils.getNowDate());
        }
        int rows = configMapper.updateLiveCommentFeatureConfig(config);
        evictConfigCache();
        if (rows > 0) {
            notifyLiveAppConfigChanged();
        }
        return rows;
    }

    @Override
    public void notifyLiveAppConfigChanged() {
        liveAppWebSocketNotifyService.broadcastCommentConfigRefresh();
    }

    private static LiveCommentFeatureConfig defaultConfig() {
        LiveCommentFeatureConfig c = new LiveCommentFeatureConfig();
        c.setConfigId(CONFIG_ID);
        c.setFloatEnabled(0);
        c.setFloatCooldownSec(60);
        c.setFloatRoleCodes("USER,ASSISTANT,ANCHOR,ADMIN");
        c.setPinMaxPerRoom(3);
        c.setPinDurationOptions("5,10,30,-1");
        c.setPinRoleCodes("ADMIN,ASSISTANT,ANCHOR");
        return c;
    }
}
