package com.yonge.cooleshow.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dayaedu.cbs.openfeign.client.CoursewareFeignService;
import com.dayaedu.cbs.openfeign.wrapper.courseware.CbsLessonCoursewareWrapper;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.response.template.R;
import com.microsvc.toolkit.config.jwt.utils.JwtUserInfo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
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.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumMusicService;
import com.yonge.cooleshow.biz.dal.wrapper.LessonCoursewareDetailWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.LessonCoursewareWrapper;
import com.yonge.cooleshow.biz.dal.wrapper.TenantAlbumMusicWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 专辑表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("${app-config.url.teacher:}/tenantAlbumMusic")
@Api(tags = "机构专辑曲目 API接口")
public class TenantAlbumSheetController extends BaseController {

    @Resource
    private TenantAlbumMusicService tenantAlbumMusicService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private CoursewareFeignService coursewareFeignService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private TeacherService teacherService;


    @ApiOperation(value = "查询条件")
    @PostMapping("/selectCondition")
    public HttpResponseResult<TenantAlbumMusicWrapper.TenantAlbumMusicSelectData> selectCondition(@RequestBody TenantAlbumMusicWrapper.TenantAlbumMusicSelect query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
//        query.setUserId(user.getId());
        return succeed(tenantAlbumMusicService.getTenantAlbumMusicQuery(query));
    }


    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantAlbumMusicWrapper.StudentTenantAlbumMusic>> page(@RequestBody @Validated TenantAlbumMusicWrapper.StudentTenantAlbumMusicQuery query) {
//        SysUser user = sysUserFeignService.queryUserInfo();
//        if (user == null || null == user.getId()) {
//            return failed(HttpStatus.FORBIDDEN, "请登录");
//        }

//        Teacher teacher = teacherService.getById(user.getId());
//        query.setTenantId(Optional.ofNullable(teacher.getTenantId()).orElse(-1L));
        query.setClientType(ClientEnum.TEACHER);
        IPage<TenantAlbumMusicWrapper.StudentTenantAlbumMusic> page = tenantAlbumMusicService.selectPage(QueryInfo.getPage(query), query);
        return succeed((PageUtil.pageInfo(page)));
    }


    @ApiOperation(value = "获取课件详情")
    @PostMapping("/getLessonCoursewareDetail")
    public HttpResponseResult<LessonCoursewareWrapper.LessonCourseware> getLessonCoursewareDetail(@RequestBody @Valid LessonCoursewareWrapper.LessonCourseQuery query){
        query.setClient(ClientEnum.TEACHER);
//        SysUser sysUser = sysUserFeignService.queryUserInfo();
//        if (sysUser == null || null == sysUser.getId()) {
//            return failed("用户信息获取失败");
//        }
        query.setUserId(sysUserService.getUserId());
        LessonCoursewareWrapper.LessonCourseware lessonCourseware = tenantAlbumMusicService.getLessonCoursewareDetail(query);
        return succeed(lessonCourseware);
    }


    @ApiOperation(value = "获取课件关联的课程列表")
    @GetMapping("/getLessonCoursewareCourseList/{id}")
    public HttpResponseResult<List<LessonCoursewareWrapper.CourseScheduleCoursewareDetail>> getLessonCoursewareCourseList(@PathVariable("id") Long id){
        List<CbsLessonCoursewareWrapper.CourseScheduleCoursewareDetail> courseScheduleCoursewareDetails = coursewareFeignService.coursewareDetail(id).feignData();
        if (CollectionUtils.isNotEmpty(courseScheduleCoursewareDetails)) {
            String jsonString = JSONObject.toJSONString(courseScheduleCoursewareDetails);
            List<LessonCoursewareWrapper.CourseScheduleCoursewareDetail> courseScheduleCoursewareDetailList = JSONObject
                .parseArray(jsonString, LessonCoursewareWrapper.CourseScheduleCoursewareDetail.class);
            return succeed(courseScheduleCoursewareDetailList);
        }
        return succeed(new ArrayList<>());
    }

    @ApiOperation(value = "获取课程详情")
    @GetMapping("/getLessonCourseDetail/{id}")
    public HttpResponseResult<LessonCoursewareDetailWrapper.LessonCoursewareDetail> getLessonCourseDetail(@PathVariable("id") String id){
        return succeed(tenantAlbumMusicService.getLessonCourseDetail(id));
    }


    @ApiOperation(value = "关联课程类型")
    @PostMapping("/refLevel")
    public HttpResponseResult<List<LessonCoursewareDetailWrapper.RefLevelDetailInfo>> refLevel(@RequestBody LessonCoursewareDetailWrapper.RefLevelQuery query) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        Teacher teacher = teacherService.getById(user.getId());
        query.setTenantId(Optional.ofNullable(teacher.getTenantId()).orElse(-1L));
        List<LessonCoursewareDetailWrapper.RefLevelDetailInfo> result = tenantAlbumMusicService.refLevel(query);

        return HttpResponseResult.succeed(result);
    }


}
