package com.ycwl.basic.repository;

import com.alibaba.fastjson.JSONObject;
import com.ycwl.basic.mapper.MpConfigMapper;
import com.ycwl.basic.mapper.MpNotifyConfigMapper;
import com.ycwl.basic.mapper.ScenicMapper;
import com.ycwl.basic.model.pc.mp.MpConfigEntity;
import com.ycwl.basic.model.pc.mp.MpNotifyConfigEntity;
import com.ycwl.basic.model.pc.mp.ScenicMpNotifyVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
import com.ycwl.basic.model.pc.scenic.entity.ScenicEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ScenicRepository {
    @Autowired
    private ScenicMapper scenicMapper;
    @Autowired
    private MpConfigMapper mpConfigMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public static final String SCENIC_CACHE_KEY = "scenic:%s";
    public static final String SCENIC_FULL_CACHE_KEY = "scenic:f%s";
    public static final String SCENIC_CONFIG_CACHE_KEY = "scenic:%s:config";
    public static final String SCENIC_MP_CACHE_KEY = "scenic:%s:mp";
    public static final String SCENIC_MP_NOTIFY_CACHE_KEY = "scenic:%s:mpNotify";
    @Autowired
    private MpNotifyConfigMapper mpNotifyConfigMapper;

    public ScenicEntity getScenic(Long id) {
        if (redisTemplate.hasKey(String.format(SCENIC_CACHE_KEY, id))) {
            return JSONObject.parseObject(redisTemplate.opsForValue().get(String.format(SCENIC_CACHE_KEY, id)), ScenicEntity.class);
        }
        ScenicEntity scenic = scenicMapper.get(id);
        if (scenic != null) {
            redisTemplate.opsForValue().set(String.format(SCENIC_CACHE_KEY, id), JSONObject.toJSONString(scenic));
        }
        return scenic;
    }

    public ScenicConfigEntity getScenicConfig(Long scenicId) {
        if (redisTemplate.hasKey(String.format(SCENIC_CONFIG_CACHE_KEY, scenicId))) {
            return JSONObject.parseObject(redisTemplate.opsForValue().get(String.format(SCENIC_CONFIG_CACHE_KEY, scenicId)), ScenicConfigEntity.class);
        }
        ScenicConfigEntity scenicConfig = scenicMapper.getConfig(scenicId);
        if (scenicConfig != null) {
            redisTemplate.opsForValue().set(String.format(SCENIC_CONFIG_CACHE_KEY, scenicId), JSONObject.toJSONString(scenicConfig));
        }
        return scenicConfig;
    }

    public MpConfigEntity getScenicMpConfig(Long scenicId) {
        if (redisTemplate.hasKey(String.format(SCENIC_MP_CACHE_KEY, scenicId))) {
            return JSONObject.parseObject(redisTemplate.opsForValue().get(String.format(SCENIC_MP_CACHE_KEY, scenicId)), MpConfigEntity.class);
        }
        ScenicEntity scenic = getScenic(scenicId);
        MpConfigEntity mpConfigEntity = mpConfigMapper.selectById(scenic.getMpId());
        if (mpConfigEntity != null) {
            redisTemplate.opsForValue().set(String.format(SCENIC_MP_CACHE_KEY, scenicId), JSONObject.toJSONString(mpConfigEntity));
        }
        return mpConfigEntity;
    }

    public ScenicMpNotifyVO getScenicMpNotifyConfig(Long scenicId) {
        if (redisTemplate.hasKey(String.format(SCENIC_MP_NOTIFY_CACHE_KEY, scenicId))) {
            return JSONObject.parseObject(redisTemplate.opsForValue().get(String.format(SCENIC_MP_NOTIFY_CACHE_KEY, scenicId)), ScenicMpNotifyVO.class);
        }
        MpConfigEntity mpConfig = getScenicMpConfig(scenicId);
        if (mpConfig == null) {
            return null;
        }
        ScenicMpNotifyVO mpNotifyConfig = new ScenicMpNotifyVO();
        mpNotifyConfig.setAppId(mpConfig.getAppId());
        mpNotifyConfig.setAppSecret(mpConfig.getAppSecret());
        mpNotifyConfig.setMpId(mpConfig.getId());
        mpNotifyConfig.setAppState(mpConfig.getState());
        List<MpNotifyConfigEntity> mpNotifyConfigList = mpNotifyConfigMapper.listByMpId(mpConfig.getId());
        mpNotifyConfigList.forEach(item -> {
            switch (item.getType()) {
                case 0:
                    mpNotifyConfig.setVideoGeneratedTemplateId(item.getTemplateId());
                    break;
                case 1:
                    mpNotifyConfig.setVideoDownloadTemplateId(item.getTemplateId());
                    break;
                case 2:
                    mpNotifyConfig.setVideoPreExpireTemplateId(item.getTemplateId());
                    break;
            }
        });
        redisTemplate.opsForValue().set(String.format(SCENIC_MP_NOTIFY_CACHE_KEY, scenicId), JSONObject.toJSONString(mpNotifyConfig));
        return mpNotifyConfig;
    }

    public String getVideoGeneratedTemplateId(Long scenicId) {
        ScenicMpNotifyVO scenicMpNotifyConfig = getScenicMpNotifyConfig(scenicId);
        if (scenicMpNotifyConfig != null) {
            return scenicMpNotifyConfig.getVideoGeneratedTemplateId();
        }
        return null;
    }

    public String getVideoDownloadTemplateId(Long scenicId) {
        ScenicMpNotifyVO scenicMpNotifyConfig = getScenicMpNotifyConfig(scenicId);
        if (scenicMpNotifyConfig != null) {
            return scenicMpNotifyConfig.getVideoDownloadTemplateId();
        }
        return null;
    }

    public String getVideoPreExpireTemplateId(Long scenicId) {
        ScenicMpNotifyVO scenicMpNotifyConfig = getScenicMpNotifyConfig(scenicId);
        if (scenicMpNotifyConfig != null) {
            return scenicMpNotifyConfig.getVideoPreExpireTemplateId();
        }
        return null;
    }

    public void clearCache(Long scenicId) {
        redisTemplate.delete(String.format(SCENIC_CACHE_KEY, scenicId));
        redisTemplate.delete(String.format(SCENIC_FULL_CACHE_KEY, scenicId));
        redisTemplate.delete(String.format(SCENIC_CONFIG_CACHE_KEY, scenicId));
        redisTemplate.delete(String.format(SCENIC_MP_CACHE_KEY, scenicId));
        redisTemplate.delete(String.format(SCENIC_MP_NOTIFY_CACHE_KEY, scenicId));
    }

}
