package com.yonge.cooleshow.student.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
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 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.TeacherStyleSearch;
import com.yonge.cooleshow.biz.dal.entity.TeacherStyleVideo;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.service.AppVersionInfoService;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import com.yonge.cooleshow.biz.dal.service.StudentStarService;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.biz.dal.service.TeacherStyleVideoService;
import com.yonge.cooleshow.biz.dal.vo.HotTeacherVo;
import com.yonge.cooleshow.biz.dal.vo.TeacherHomeVo;
import com.yonge.cooleshow.biz.dal.vo.TeacherStyleVideoVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;

@RestController
@RequestMapping("/teacher")
@Api(value = "教师表", tags = "教师表")
public class TeacherController extends BaseController {
    @Autowired
    private TeacherStyleVideoService styleVideoService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentStarService studentStarService;
    @Autowired
    private AppVersionInfoService appVersionInfoService;
    
    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "老师风采-分页")
    @PostMapping("/stylePage")
    public HttpResponseResult<PageInfo<TeacherStyleVideoVo>> stylePage(@RequestBody TeacherStyleSearch query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        /*if(StringUtils.isBlank(query.getSubjectId())){
        	Student student = studentService.getById(user.getId());
        	query.setSubjectId(student.getSubjectId());
        }*/
        // 检查app版本
        query.setAuditVersion(appVersionInfoService.getAppAuditVersion(query.getPlatform(), query.getVersion()));
        query.setStudentId(user.getId());
        IPage<TeacherStyleVideoVo> pages = styleVideoService.stylePage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "查询教师基本信息")
    @GetMapping("/queryTeacherHome")
    public HttpResponseResult<TeacherHomeVo> queryTeacherHome(@ApiParam(value = "老师ID", required = true) @RequestParam("userId") Long userId) {
        if (null == userId) {
            return failed("缺少老师ID");
        }
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        HttpResponseResult<TeacherHomeVo> res = teacherService.queryTeacherHome(user.getId(), userId);
        //学生端过滤只看审核通过的
        if (null != res.getData() && !CollectionUtils.isEmpty(res.getData().getStyleVideo())) {
            List<TeacherStyleVideo> styleVideo = res.getData().getStyleVideo();
            List<TeacherStyleVideo> collect = styleVideo.stream().filter(o -> AuthStatusEnum.PASS.equals(o.getAuthStatus())).collect(Collectors.toList());
            res.getData().setStyleVideo(collect);
        }
        return res;
    }

    @ApiOperation(value = "关注/取消关注")
    @GetMapping("/starOrUnStar")
    public HttpResponseResult<Boolean> starOrUnStar(@ApiParam(value = "老师ID", required = true) @RequestParam("userId") Long userId,
                                                    @ApiParam(value = "状态 0 取消关注 1 关注", required = true) @RequestParam("starStatus") Integer starStatus) {
        if (null == userId) {
            return failed("缺少老师ID");
        }
        YesOrNoEnum starStatusEnum = YesOrNoEnum.valueOf(starStatus);
        if (null == starStatusEnum) {
            return failed("缺少关注状态");
        }
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return studentStarService.starOrUnStar(user.getId(), userId, starStatusEnum);
    }

    @ApiOperation(value = "增加视频浏览量")
    @GetMapping("/addVideoBrowse")
    public HttpResponseResult<Boolean> addVideoBrowse(@ApiParam(value = "视频ID", required = true) @RequestParam("videoId") Long videoId) {
        if (null == videoId) {
            return failed("缺少videoId");
        }
        return styleVideoService.addVideoBrowse(videoId);
    }

    @ApiOperation(value = "增加主页浏览量")
    @GetMapping("/addHomeBrowse")
    public HttpResponseResult<Boolean> addHomeBrowse(@ApiParam(value = "老师ID", required = true) @RequestParam("userId") Long userId) {
        if (null == userId) {
            return failed("缺少userId");
        }
        return teacherService.addHomeBrowse(userId);
    }

    @ApiOperation(value = "推荐老师列表")
    @GetMapping("/queryHotTeacherList")
    public HttpResponseResult<List<HotTeacherVo>> queryHotTeacherList() {
        SysUser user = sysUserFeignService.queryUserInfo();
        Long userId = null;
        if (user != null && null != user.getId()) {
            userId = user.getId();
        }
        List<HotTeacherVo> list = teacherService.queryHotTeacherList(userId);
        
        for(HotTeacherVo vo : list){
        	vo.setGraduateSchool(null);
        }
        
        return HttpResponseResult.succeed(list);
    }
}