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.search.MusicAlbumDetailSearch;
import com.yonge.cooleshow.biz.dal.dto.search.MusicAlbumSearch;
import com.yonge.cooleshow.biz.dal.entity.Student;
import com.yonge.cooleshow.biz.dal.entity.Teacher;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.AlbumDetailVo;
import com.yonge.cooleshow.biz.dal.vo.MusicAlbumVo;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

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

	@Autowired
	private MusicAlbumService musicAlbumService;
    
    @Autowired
    private StudentService studentService;
    
    @Autowired
    private TeacherService teacherService;


    @ApiOperation(value = "热门专辑", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicAlbumVo>> list(@ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false, defaultValue = "STUDENT") String clientType,
                                                           @RequestBody MusicAlbumSearch query) {

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser != null && sysUser.getId() != null) {
            query.setUserId(sysUser.getId());
        	
            if(StringUtils.isNotBlank(query.getClientId())){
    			if (StringUtils.equals(ClientEnum.STUDENT.getCode(), query.getClientId())) {
    				Student student = studentService.getById(sysUser.getId());
    	        	if(student != null){
    	                query.setSubjectIds(student.getSubjectId());
    	        	}
    			} else if (StringUtils.equals(ClientEnum.TEACHER.getCode(), query.getClientId())) {
    				Teacher teacher = teacherService.getById(sysUser.getId());
    	        	if(teacher != null){
    	                query.setSubjectIds(teacher.getSubjectId());
    	        	}
    			}
            }
        }

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

        // 检查app版本
        query.setAuditVersion(YesOrNoEnum.NO);
        query.setAlbumStatus(YesOrNoEnum.YES);
        query.setSortBy(1);
        IPage<MusicAlbumVo> iPage = musicAlbumService.selectStudentPage(PageUtil.getPage(query),query, ClientEnum.valueOf(clientType));
        return succeed(PageUtil.pageInfo(iPage));
    }


    /**
     * 专辑详情
     */
    @PostMapping(value = "/detail",  consumes="application/json", produces="application/json")
    @ApiOperation(value = "专辑详情",httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<AlbumDetailVo> detail(@ApiParam(value = "客户端类型") @RequestParam(value = "clientType", required = false, defaultValue = "STUDENT") String clientType,
                                                    @Valid @RequestBody MusicAlbumDetailSearch query) {

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

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        YesOrNoEnum favorite = YesOrNoEnum.NO;
        if (sysUser != null && sysUser.getId() != null) {
            favorite = musicAlbumService.checkFavorite(sysUser.getId(),query.getId(), query.getClientType());
        }
        query.setType(2);
        query.setState(YesOrNoEnum.YES);
        AlbumDetailVo albumDetailVo = musicAlbumService.detail(PageUtil.getPage(query), query, null, null);
        albumDetailVo.setFavorite(favorite);

        // 热门专辑
        MusicAlbumSearch musicAlbumSearch = new MusicAlbumSearch();
        musicAlbumSearch.setAuditVersion(YesOrNoEnum.NO);
        musicAlbumSearch.setAlbumStatus(YesOrNoEnum.YES);
        musicAlbumSearch.setSortBy(1);
        musicAlbumSearch.setPage(1);
        musicAlbumSearch.setRows(query.getRelatedNum() + 1);
        IPage<MusicAlbumVo> hotMusicAlbum = musicAlbumService.selectStudentPage(PageUtil.getPage(musicAlbumSearch),musicAlbumSearch, ClientEnum.valueOf(clientType));
        List<MusicAlbumVo> musicAlbumVos = hotMusicAlbum.getRecords()
                                                  .stream()
                                                  .filter(musicAlbumVo -> !musicAlbumVo.getId()
                                                                                       .equals(albumDetailVo.getId()))
                                                  .collect(Collectors.toList());
        if (musicAlbumVos.size() > query.getRelatedNum()) {
            musicAlbumVos = musicAlbumVos.subList(0,query.getRelatedNum());
        }
        albumDetailVo.setHotMusicAlbum(musicAlbumVos);

        // 相关专辑
        musicAlbumSearch.setAlbumTagIds(albumDetailVo.getAlbumTag());
        IPage<MusicAlbumVo> relatedMusicAlbum = musicAlbumService.selectStudentPage(PageUtil.getPage(musicAlbumSearch),musicAlbumSearch, ClientEnum.valueOf(clientType));
        List<MusicAlbumVo> musicAlbumVos1 = relatedMusicAlbum.getRecords()
                                                      .stream()
                                                      .filter(musicAlbumVo -> !musicAlbumVo.getId()
                                                                                           .equals(albumDetailVo.getId()))
                                                      .collect(Collectors.toList());
        if (musicAlbumVos1.size() > query.getRelatedNum()) {
            musicAlbumVos1 = musicAlbumVos1.subList(0,query.getRelatedNum());
        }
        albumDetailVo.setRelatedMusicAlbum(musicAlbumVos1);
        return succeed(albumDetailVo);
    }

}
