package com.ruoyi.web.controller.client;


import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.CategoryInfo;
import com.ruoyi.common.core.domain.entity.MusicInfo;
import com.ruoyi.common.core.domain.entity.MusicMaxReq;
import com.ruoyi.common.core.domain.entity.MusicScene;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.IMusicInfoService;
import com.ruoyi.system.service.IMusicSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/client/music")
@RestController
public class MusicController extends BaseController {

    @Autowired
    private IMusicInfoService musicService;
    @Autowired
    private IMusicSceneService musicSceneService;

    // 根据分类获取音乐
    @RequestMapping("/cate/music")
    public void getMusicByCate(@RequestBody CategoryInfo categoryInfo){
        musicService.findMusicByCate(categoryInfo);
    }


    // 获取收藏音乐 TODO: 收藏播放
    @RequestMapping("/collect/music/{collectId}")
    public TableDataInfo collectPlaylist(@PathVariable String collectId){
        startPage();
        List<MusicInfo> list = musicService.findMusicByCollectId(collectId);
        //musicService.createPlaylist();
        return getDataTableData(list);
    }


    // 添加我喜欢的音乐
    @RequestMapping("/like/music/{musicId}")
    public AjaxResult addLikeMusic(@PathVariable String musicId){
        musicService.addLikeMusic(musicId);
        return AjaxResult.success();
    }

    // 获取我喜欢的音乐 TODO: 喜欢播放
    @RequestMapping("/like/music")
    public TableDataInfo likeMusic(){
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<MusicInfo> list = musicService.findLikeMusicByUser(userId);
        //musicService.createPlaylist();
        return getDataTableData(list);
    }
    // 删除我喜欢的音乐 cancel/like
    @RequestMapping("/cancel/like/{musicId}")
    public AjaxResult cancelLikeMusic(@PathVariable String musicId){
        musicService.cancelLikeMusic(musicId);
        return AjaxResult.success();
    }

//    // 新增历史
//    @RequestMapping("/history/add/{musicId}")
//    public AjaxResult addHistory(@PathVariable String musicId){
//        musicService.addHistoryMusic(musicId);
//        return AjaxResult.success();
//    }

    // 查询历史 TODO: 历史播放
    @RequestMapping("/history/music")
    public TableDataInfo historyMusic(){
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<MusicInfo> list = musicService.findHistoryMusicByUser(userId);
        //musicService.createPlaylist();
        return getDataTableData(list);
    }

    // 获取音乐基本信息
    @RequestMapping("/getMusicInfo/{musicId}")
    public AjaxResult getMusicInfo(@PathVariable String musicId){
        Long userId = SecurityUtils.getUserId();
        MusicInfo musicInfo = musicService.getMusicInfo(userId, musicId);
        if (musicInfo != null){
            return AjaxResult.success(musicInfo);
        }else {
            return new AjaxResult(HttpStatus.USER_VIP_EXPIRE, "请开通会员");
        }
    }

    // 新增用户混音
    @RequestMapping("/add/mixMusic")
    public AjaxResult addMixMusicInfo(@RequestBody MusicMaxReq param){
        MusicInfo newMusic = musicService.addMixMusicInfo(param);
        if (newMusic != null){
            return AjaxResult.success(newMusic);
        }
        return AjaxResult.error("混音错误");
    }

    // 获取场景音乐
    @GetMapping("/scene/list")
    public TableDataInfo list(MusicScene musicScene)
    {
        startPage();
        List<MusicScene> list = musicSceneService.selectMusicSceneList(musicScene);
        return getDataTableData(list);
    }


    /**
     * 获取下一首播放的音乐ID
     * @param playMode 播放模式：1-顺序播放，2-随机播放
     * @param playlistType 播放列表类型：1-历史播放，2-分类播放，3-推荐播放，4-收藏播放，5-喜欢播放
     * @param categoryId 分类ID，当playlistType=2时必填
     * @param collectId 收藏ID，当playlistType=4时必填
     * @param reId 推荐ID，当playlistType=3时必填
     * @param currentMusicId 当前播放的音乐ID，用于顺序播放时获取下一首
     * @return 下一首音乐ID
     */
    @GetMapping("/getNextMusicId")
    public AjaxResult getNextMusicId(@RequestParam(defaultValue = "1") Integer playMode,
                                    @RequestParam Integer playlistType,
                                    @RequestParam(required = false) String categoryId,
                                    @RequestParam(required = false) String collectId,
                                    @RequestParam(required = false) String reId,
                                    @RequestParam(required = false) String currentMusicId){
        Long userId = SecurityUtils.getUserId();
        String nextMusicId = musicService.getNextMusicId(userId, playMode, playlistType, categoryId, collectId, reId,currentMusicId);
        return AjaxResult.success("请求成功",nextMusicId);
    }



}
