package cn.reghao.tnb.content.app.data.service;

import cn.reghao.jutil.jdk.web.db.Page;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.app.data.db.mapper.AudioPostMapper;
import cn.reghao.tnb.content.app.data.model.po.AudioPost;
import cn.reghao.tnb.content.app.data.model.vo.AudioInfo;
import cn.reghao.tnb.content.app.data.model.vo.UserAudioCard;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2023-10-12 13:49:01
 */
@Service
public class AudioService {
    private final int pageSize = 10;
    private final AudioPostMapper audioPostMapper;

    public AudioService(AudioPostMapper audioPostMapper) {
        this.audioPostMapper = audioPostMapper;
    }

    public PageList<UserAudioCard> getAudios(int page) {
        long userId = UserContext.getUserId();
        int total = audioPostMapper.countByUserId(userId);
        Page page1 = new Page(page, pageSize);
        List<UserAudioCard> list = audioPostMapper.findAudioCardByUserIdAndByPage(page1, userId);
        return PageList.pageList(page, pageSize, total, list);
    }

    public PageList<UserAudioCard> getPageByUserId(long userId, int page) {
        int total = audioPostMapper.countByUserId(userId);
        Page page1 = new Page(page, pageSize);
        List<UserAudioCard> list = audioPostMapper.findAudioCardByUserIdAndByPage(page1, userId);
        return PageList.pageList(page, pageSize, total, list);
    }

    public AudioInfo getAudioInfo(String audioId) {
        AudioPost audioPost = audioPostMapper.findByAudioId(audioId);
        if (audioPost == null) {
            return null;
        }

        long loginUser = UserContext.getUserId();
        String audioFileId = audioPost.getAudioFileId();
        try {
            int reqUser = 1;
            /*List<AudioUrl> list = ossConsoleClient.getAudioUrls(audioFileId, reqUser);
            if (list.isEmpty()) {
                return null;
            }
            String audioUrl = list.get(0).getUrl();

            long userId = UserContext.getUserId();
            double currentTime = 0.0;
            if (userId != -1) {
                Double currentTime1 = videoRecordService.getCurrentTime(userId, audioId);
                if (currentTime1 != null) {
                    currentTime = currentTime1;
                }
            }

            return new AudioFileInfo(audioPost, audioUrl, currentTime);*/
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
