package cn.reghao.tnb.content.app.vod.service.impl;

import cn.reghao.jutil.jdk.db.Page;
import cn.reghao.tnb.content.api.dto.VideoRegion;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoCategoryMapper;
import cn.reghao.tnb.content.app.vod.model.po.VideoCategory;
import cn.reghao.tnb.content.app.vod.model.query.CategoryQuery;
import cn.reghao.tnb.content.app.vod.service.CategoryService;
import cn.reghao.tnb.content.app.util.redis.ds.RedisStringObject;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-08-06 21:08:56
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    private final VideoCategoryMapper videoCategoryMapper;
    private final RedisStringObject redisStringObject;

    public CategoryServiceImpl(VideoCategoryMapper videoCategoryMapper, RedisStringObject redisStringObject) {
        this.videoCategoryMapper = videoCategoryMapper;
        this.redisStringObject = redisStringObject;
    }

    public List<VideoCategory> getAllCategory() {
        CategoryQuery categoryQuery = new CategoryQuery.Builder().build();
        List<VideoCategory> list = getVideoCategories(categoryQuery);
        return list;
    }

    public List<VideoRegion> getCategories(boolean vip) {
        CategoryQuery categoryQuery = new CategoryQuery.Builder().build();
        List<VideoCategory> list = getVideoCategories(categoryQuery);
        setVideoCategory(list);

        Map<Integer, VideoRegion> map = new HashMap<>(list.size());
        for (VideoCategory category : list) {
            if (category.isVip() && !vip) {
                continue;
            }

            int pid = category.getPid();
            int id = category.getId();
            String name = category.getName();
            int regionId = category.getRegionId();
            String icon = category.getIcon();
            int sort = category.getSort();
            if (pid == 0) {
                map.put(id, new VideoRegion(pid, regionId, id, name, icon, sort));
            } else {
                map.get(pid).getChildren().add(new VideoRegion(pid, regionId, id, name, icon, sort));
            }
        }

        List<VideoRegion> parents = new ArrayList<>(map.values()).stream()
                .filter(videoRegion -> !videoRegion.getChildren().isEmpty())
                .sorted(Comparator.comparingInt(VideoRegion::getSort))
                .collect(Collectors.toList());
        /*parents.forEach(videoRegion -> {
            videoRegion.getChildren().sort(Comparator.comparingInt(VideoRegion::getSort));
        });*/
        return parents;
    }

    public List<VideoCategory> getVideoCategories(CategoryQuery categoryQuery) {
        int pageNumber = 1;
        int pageSize = 1000;
        Page page = new Page(pageNumber, pageSize);
        List<VideoCategory> list = videoCategoryMapper.findVideoCategoryByPage(page, categoryQuery);
        return list;
    }

    private void setVideoCategory(List<VideoCategory> list) {
        String keyPrefix = "tnb:content:video:category";
        list.forEach(videoCategory -> {
            int id = videoCategory.getId();
            String key = String.format("%s:%s", keyPrefix, id);
            redisStringObject.set(key, videoCategory);
        });
    }

    public VideoCategory getVideoCategory(int id) {
        String key = String.format("tnb:content:video:category:%s", id);
        Object value = redisStringObject.get(key);
        if (value instanceof VideoCategory) {
            return (VideoCategory) value;
        }

        return null;
    }
}
