package com.yonge.cooleshow.website.controller.open;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.MusicImgDto;
import com.yonge.cooleshow.biz.dal.dto.search.HotSearchSearch;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.dto.search.StudentMusicSheetSearch;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.HotSearchEnum;
import com.yonge.cooleshow.biz.dal.service.HotSearchService;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.*;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 曲谱表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("/open/music/sheet")
@Api(tags = "开放曲谱接口")
public class OpenMusicSheetController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;

	@Autowired
	private MusicSheetService musicSheetService;

    @Autowired
    private MusicAlbumService musicAlbumService;

    @Autowired
    private HotSearchService hotSearchService;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "曲目详情", notes = "传入musicTag")
    public HttpResponseResult<MusicSheetWebsiteDetailVo> detail(@ApiParam(value = "曲谱编号", required = true) @PathVariable("id") Long id,
                                                                @ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false) String clientType) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();

        if (StringUtils.isEmpty(clientType)) {
            clientType = ClientEnum.STUDENT.getCode();
        }
        if (ClientEnum.invalid(clientType)) {
            return failed("无效的客户端类型");
        }

        MusicSheetWebsiteDetailVo detail = musicSheetService.websiteDetail(id, sysUser, ClientEnum.valueOf(clientType));
        return succeed(detail);
    }

    @ApiOperation(value = "首页曲目分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicSheetVo>> list(@ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false, defaultValue = "STUDENT") String clientType,
                                                           @RequestBody StudentMusicSheetSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();

        // 检查app版本
        query.setAuditVersion(YesOrNoEnum.NO);


        // 学生 只能看通过审核 并且 启用的 曲目
        query.setState(YesOrNoEnum.YES);
        query.setAuditStatus(AuthStatusEnum.PASS);
        if (sysUser != null) {
            query.setStudentId(sysUser.getId());
        }

        if (ClientEnum.invalid(clientType)) {
            return failed("无效的客户端类型");
        }

        IPage<MusicSheetVo> musicSheetVoIPage = musicSheetService.selectStudentPage(PageUtil.getPage(query), query, ClientEnum.valueOf(clientType));
        return succeed(PageUtil.pageInfo(musicSheetVoIPage));
    }


    @ApiOperation(value = "乐谱（专辑和曲目同时查询）分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/albumAndSheetList", consumes="application/json", produces="application/json")
    public HttpResponseResult<AlbumAndSheetVo> albumAndSheetList(@RequestBody StudentMusicSheetSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();

        // 学生 只能看通过审核 并且 启用的 曲目
        query.setState(YesOrNoEnum.YES);
        query.setAuditVersion(YesOrNoEnum.NO);
        query.setAuditStatus(AuthStatusEnum.PASS);
        if (sysUser != null) {
            query.setStudentId(sysUser.getId());
        }
        query.setRows(query.getSheetRow());
        IPage<MusicSheetVo> musicSheetVoIPage = musicSheetService.selectStudentPage(PageUtil.getPage(query), query, ClientEnum.STUDENT);

        MusicAlbumSearch musicAlbumSearch = new MusicAlbumSearch();
        musicAlbumSearch.setAlbumStatus(YesOrNoEnum.YES);
        musicAlbumSearch.setSortBy(1);
        musicAlbumSearch.setIdAndName(query.getIdAndName());
        musicAlbumSearch.setAuditVersion(YesOrNoEnum.NO);
        musicAlbumSearch.setAlbumTagIds(query.getMusicTagIds());
        musicAlbumSearch.setPage(query.getPage());
        musicAlbumSearch.setRows(query.getAlbumRow());
        IPage<MusicAlbumVo> musicAlbumVoIPage = musicAlbumService.selectPage(PageUtil.getPage(musicAlbumSearch), musicAlbumSearch);

        AlbumAndSheetVo albumAndSheetVo = new AlbumAndSheetVo();
        albumAndSheetVo.setMusicSheetList(PageUtil.pageInfo(musicSheetVoIPage));
        albumAndSheetVo.setMusicAlbumList(PageUtil.pageInfo(musicAlbumVoIPage));
        return succeed(albumAndSheetVo);
    }

    @ApiOperation(value = "热门搜索")
    @GetMapping(value="/hotTag/{type}")
    public HttpResponseResult<List<HotSearchVo>> hotTag(@PathVariable String type) {
        HotSearchSearch query = new HotSearchSearch();
        query.setPage(1);
        query.setRows(10);
        query.setStatus(YesOrNoEnum.YES);
        query.setType(HotSearchEnum.valueOf(type));
        IPage<HotSearchVo> pages = hotSearchService.selectPage(PageUtil.getPage(query), query);

        return succeed(pages.getRecords());
    }


    @ApiOperation(value = "新增曲谱渲染图")
    @PostMapping(value="/img")
    public HttpResponseResult<Boolean> img(@RequestBody @Valid MusicImgDto musicImgDto) {
        return succeed(musicSheetService.updateMusicImg(musicImgDto.getMusicImg(),musicImgDto.getMusicSheetId()));
    }

}
