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);
    }


    // 创建歌单
    @RequestMapping("/collect/music/{collectId}")
    public TableDataInfo createPlaylist(@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();
    }

    // 获取我喜欢的音乐
    @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();
//    }

    // 查询历史
    @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);
    }


}
