package com.ruoyi.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.enums.Audio;
import com.ruoyi.common.enums.MusicType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MusicUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.config.AliConfig;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IMusicInfoService;
import com.ruoyi.system.service.IUserHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 音乐信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@Service
public class MusicInfoServiceImpl implements IMusicInfoService
{
    @Autowired
    private MusicInfoMapper musicInfoMapper;
    @Autowired
    private UserLikeMusicMapper userLikeMusicMapper;
    @Autowired
    private UserCollectMapper userCollectMapper;
    @Autowired
    private MusicSceneMapper musicSceneMapper;
    @Autowired
    private MusicSceneRelateMapper relateMapper;
    @Autowired
    private ShopUserMapper shopUserMapper;



    /**
     * 查询音乐信息
     *
     * @param id 音乐信息主键
     * @return 音乐信息
     */
    @Override
    public MusicInfo selectMusicInfoById(String id)
    {
        return musicInfoMapper.selectMusicInfoById(id);
    }

    /**
     * 查询音乐信息列表
     *
     * @param musicInfo 音乐信息
     * @return 音乐信息
     */
    @Override
    public List<MusicInfo> selectMusicInfoList(MusicInfo musicInfo)
    {
        return musicInfoMapper.selectMusicInfoList(musicInfo);
    }

    /**
     * 新增音乐信息
     *
     * @param musicInfo 音乐信息
     * @return 结果
     */
    @Override
    public int insertMusicInfo(MusicInfo musicInfo)
    {

        musicInfo.setCreateTime(DateUtils.getNowDate());
        musicInfo.setMusicId(IdUtil.getSnowflakeNextId());
        musicInfo.setMusicType(MusicType.ORDINARY.getMusicType());
        musicInfo.setCreator(SecurityUtils.getUsername());
        musicInfo.setShelf(2L);
        return musicInfoMapper.insertMusicInfo(musicInfo);
    }

    /**
     * 修改音乐信息
     *
     * @param musicInfo 音乐信息
     * @return 结果
     */
    @Override
    public int updateMusicInfo(MusicInfo musicInfo)
    {
        musicInfo.setUpdateTime(DateUtils.getNowDate());
        return musicInfoMapper.updateMusicInfo(musicInfo);
    }

    /**
     * 批量删除音乐信息
     *
     * @param ids 需要删除的音乐信息主键
     * @return 结果
     */
    @Override
    public int deleteMusicInfoByIds(String[] ids)
    {
        return musicInfoMapper.deleteMusicInfoByIds(ids);
    }

    /**
     * 删除音乐信息信息
     *
     * @param id 音乐信息主键
     * @return 结果
     */
    @Override
    public int deleteMusicInfoById(String id)
    {
        return musicInfoMapper.deleteMusicInfoById(id);
    }

    @Override
    public List<MusicInfo> findMusicByCate(CategoryInfo categoryInfo) {
        musicInfoMapper.selectByCate(categoryInfo);
        return null;
    }

    @Override
    public List<MusicInfo> selectMusicInfoByCid(String categoryId) {
        return  musicInfoMapper.selectMusicInfoByCid(categoryId);
    }

    @Override
    public List<MusicInfo> findMusicByCollectId(String collectId) {
        return musicInfoMapper.selectMusicByCollectId(collectId);
    }

    @Override
    public int addLikeMusic(String musicId) {
        Long userId = SecurityUtils.getUserId();
        return musicInfoMapper.insertLikeMusic(userId,musicId);
    }

    @Override
    public List<MusicInfo> findLikeMusicByUser(Long userId) {
        return musicInfoMapper.selectLikeMusicByUser(userId);
    }

    @Override
    public int cancelLikeMusic(String musicId) {
        Long userId = SecurityUtils.getUserId();
        return musicInfoMapper.delLikeMusic(userId,musicId);
    }

    @Override
    public int addHistoryMusic(String musicId) {
        Long userId = SecurityUtils.getUserId();
        return musicInfoMapper.insertHistory(userId,musicId);
    }

    @Override
    public List<MusicInfo> findHistoryMusicByUser(Long userId) {
        return musicInfoMapper.selectHistoryMusicByUser(userId);
    }

    @Override
    public MusicInfo getMusicInfo(Long userId, String musicId) {
        MusicInfo musicInfo = musicInfoMapper.selectByMusicId(musicId);

        if (musicInfo.getVip() != null && musicInfo.getVip()==1){
            ShopUser shopUser = shopUserMapper.selectShopUserByUserId(userId);
            if (!MusicUtil.getShopIsVip(shopUser)){
                return null;
            }
        }

        // 查询是否是用户喜欢
        UserLikeMusic userLikeMusic = userLikeMusicMapper.selectUserLikeMusicAndUserId(userId, musicId);
        if (userLikeMusic != null){
            musicInfo.setLike(true);
        }
        // 查询是否是用户分类
        UserCollect userCollect = userCollectMapper.selectUserCollectAndUserId(userId, musicId);
        if (userCollect != null){
            musicInfo.setCollect(true);
            musicInfo.setUserCollect(userCollect);
        }
        // 判断是否是是混音
        if (musicInfo.getMusicType().equals(MusicType.MIXING.getMusicType())){
            // 查询混音相关信息
            MusicSceneRelate relateInfo = relateMapper.selectByMusicId(musicId);
            String sceneIds = relateInfo.getSceneIds();
            String[] sceneArray = sceneIds.split(",");
            List<MusicScene> musicScenes = musicSceneMapper.selectMusicSceneBySceneIds(sceneArray);
            relateInfo.setMusicScenes(musicScenes);
            musicInfo.setMusicScene(relateInfo);
        }
        return musicInfo;
    }

    @Override
    public MusicInfo addMixMusicInfo(MusicMaxReq param) {
        //TODO:
       // Long userId = SecurityUtils.getUserId();
        Long userId = 10000L;
        String musicId = String.valueOf(param.getMusicId());
        String sceneIds = param.getSceneIds();
        String sceneJson = param.getSceneJson();
        // 新增混音音乐标签
        MusicInfo musicInfo = musicInfoMapper.selectByMusicId(musicId);
        if (musicInfo != null){
            long newMusicId = IdUtil.getSnowflakeNextId();
            param.setMusicId(newMusicId);
            param.setMusicType(MusicType.MIXING.getMusicType());
            param.setMusicAddr(musicInfo.getMusicAddr());
            musicInfoMapper.insertMusicInfo(param);
            MusicSceneRelate musicSceneRelate = new MusicSceneRelate();
            musicSceneRelate.setMusicId(String.valueOf(newMusicId));
            musicSceneRelate.setSceneIds(sceneIds);
            musicSceneRelate.setUserId(userId);
            musicSceneRelate.setSceneInfo(sceneJson);
            musicSceneRelate.setSourceMusicId(musicId);
            relateMapper.insertMusicSceneRelate(musicSceneRelate);
            return musicInfo;
        }
        return null;
    }

    @Override
    public MusicInfo editMixMusicInfo(MusicMaxReq param) {
        Long userId = 10000L;
        Long musicId = param.getMusicId();
        String sceneIds = param.getSceneIds();
        String sceneJson = param.getSceneJson();
        String sourceMusicId = param.getSourceMusicId();
        // 查询混音音乐
        MusicInfo musicInfo = musicInfoMapper.selectByMusicId(String.valueOf(musicId));
        if (musicInfo != null){
            // 先删除在新增
            relateMapper.deleteByMusicId(musicId);
            //musicSceneMapper.selectMusicSceneById()
            MusicSceneRelate musicSceneRelate = new MusicSceneRelate();
            musicInfoMapper.updateMusicInfo(param);
            musicSceneRelate.setMusicId(String.valueOf(musicId));
            musicSceneRelate.setSceneIds(sceneIds);
            musicSceneRelate.setUserId(userId);
            musicSceneRelate.setSceneInfo(sceneJson);
            musicSceneRelate.setSourceMusicId(sourceMusicId);
            relateMapper.insertMusicSceneRelate(musicSceneRelate);
            return musicInfo;
        }
        return null;
    }

    @Override
    public List<MusicInfo> selectMusicInfoByReId(String reId) {
        return  musicInfoMapper.selectMusicInfoByReId(reId);
    }


    //TODO: 待完善
    public Map<String, String> getMixMusicInfo(String musicId){
        MusicInfo musicInfo = musicInfoMapper.selectByMusicId(musicId);
        if (musicInfo.getMusicType().equals(MusicType.MIXING.getMusicType())){
            // 获取混音信息
            relateMapper.selectByMusicId(musicId);

        }
        return null;
    }

}
