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

import cn.reghao.file.api.iface.OssService;
import cn.reghao.jutil.jdk.web.db.Page;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.string.IdGenerator;
import cn.reghao.tnb.content.api.constant.PostScope;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.app.data.model.dto.AudioPublishSbt;
import cn.reghao.tnb.content.app.data.db.mapper.AudioPostMapper;
import cn.reghao.tnb.content.app.data.model.dto.AudioScopeUpdate;
import cn.reghao.tnb.content.app.data.model.po.AudioPost;
import cn.reghao.tnb.content.app.data.model.vo.UserAudioCard;
import cn.reghao.tnb.oss.api.dto.media.AudioInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2023-01-29 10:25:10
 */
@Slf4j
@Service
public class AudioPostService {
    @DubboReference(check = false)
    private OssService ossService;

    private final int pageSize = 10;
    private final IdGenerator idGenerator;
    private final AudioPostMapper audioPostMapper;

    public AudioPostService(AudioPostMapper audioPostMapper) {
        this.idGenerator = new IdGenerator("audio-id");
        this.audioPostMapper = audioPostMapper;
    }

    public void submit(AudioPublishSbt audioPublishSbt) {
        try {
            int channelCode = audioPublishSbt.getChannelCode();
            String audioFileId = audioPublishSbt.getAudioFileId();
            AudioInfo audioInfo = ossService.getAudioInfo(channelCode, audioFileId);
            if (audioInfo == null) {
                log.error("音频文件 {} 在 oss 中不存在", audioFileId);
                return;
            }

            int scope = PostScope.PUBLIC.getCode();
            ossService.setObjectScope(channelCode, audioFileId, scope);

            String audioUrl = audioInfo.getUrl();
            int duration = audioInfo.getDuration();
            String codec = audioInfo.getCodec();
            String audioId = idGenerator.getUuid();
            AudioPost audioPost = new AudioPost(audioId, audioUrl, duration, codec, audioPublishSbt);
            audioPostMapper.save(audioPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateAudioScope(AudioScopeUpdate audioScopeUpdate) {
        String audioId = audioScopeUpdate.getAudioId();
        AudioPost audioPost = audioPostMapper.findByAudioId(audioId);
        if (audioPost == null) {
            return;
        }

        long loginUser = UserContext.getUserId();
        long publishBy = audioPost.getPublishBy();
        if (loginUser == publishBy) {
            audioPostMapper.updateAudioScope(audioScopeUpdate);
        }

        String audioFileId = audioPost.getAudioFileId();
        int scope = audioScopeUpdate.getScope();
        int channelCode = audioPost.getChannelCode();
        try {
            ossService.setObjectScope(channelCode, audioFileId, scope);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteAudio(String audioId) {
    }

    public PageList<UserAudioCard> getPageByUserId(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 UserAudioCard getAudioInfo(String audioId) {
        UserAudioCard userAudioCard = audioPostMapper.findAudioInfo(audioId);
        return userAudioCard;
    }
}
