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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.ShareProfitParam;
import com.yonge.cooleshow.biz.dal.entity.SysConfig;
import com.yonge.cooleshow.biz.dal.entity.Teacher;
import com.yonge.cooleshow.biz.dal.service.ActivityPlanService;
import com.yonge.cooleshow.biz.dal.service.AppVersionInfoService;
import com.yonge.cooleshow.biz.dal.service.CourseGroupService;
import com.yonge.cooleshow.biz.dal.service.MemberPriceSettingsService;
import com.yonge.cooleshow.biz.dal.service.MusicAlbumService;
import com.yonge.cooleshow.biz.dal.service.MusicSheetService;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.service.TeacherService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
import com.yonge.cooleshow.biz.dal.vo.CheckVo;
import com.yonge.cooleshow.biz.dal.vo.MusicActivityVo;
import com.yonge.cooleshow.biz.dal.vo.ShareProfitVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping("${app-config.url.teacher:}/open")
@Api(tags = "开放权限接口-分享")
public class OpenShareController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(OpenShareController.class);

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private VideoLessonGroupService videoLessonGroupService;

    @Autowired
    private MusicSheetService musicSheetService;

    @Autowired
    private MemberPriceSettingsService memberPriceSettingsService;

    @Autowired
    private ActivityPlanService activityPlanService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private MusicAlbumService musicAlbumService;

    @Autowired
    private AppVersionInfoService appVersionInfoService;


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

    /**
     * 服务对象
     */
    @Autowired
    private CourseGroupService courseGroupService;

    @ApiOperation(value = "老师乐谱分享分润")
    @PostMapping(value="/musicShareProfit")
    public HttpResponseResult<ShareProfitVo> shareMusicSheetProfit(@RequestBody @Valid ShareProfitParam param) {
        SysUser sysUser = sysUserFeignService.queryUserById(param.getUserId());
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicSheetService.shareMusicSheetProfit(sysUser,param.getBizId()));
    }

    @ApiOperation(value = "老师专辑分享分润")
    @PostMapping(value="/albumShareProfit")
    public HttpResponseResult<ShareProfitVo> albumShareProfit(@RequestBody @Valid  ShareProfitParam param) {
        SysUser sysUser = sysUserFeignService.queryUserById(param.getUserId());
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(musicAlbumService.albumShareProfit(sysUser,param.getBizId()));
    }

    @ApiOperation(value = "老师直播课分享分润")
    @PostMapping(value= "/liveShareProfit")
    public HttpResponseResult<ShareProfitVo> shareLiveCourseProfit(@RequestBody @Valid  ShareProfitParam param) {
        SysUser sysUser = sysUserFeignService.queryUserById(param.getUserId());
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(courseGroupService.shareLiveCourseProfit(sysUser, param.getBizId()));
    }



    @ApiOperation(value = "老师视频课分享分润")
    @PostMapping(value= "/videoShareProfit")
    public HttpResponseResult<ShareProfitVo> shareVideoCourseProfit(@RequestBody @Valid  ShareProfitParam param) {
        SysUser sysUser = sysUserFeignService.queryUserById(param.getUserId());
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(videoLessonGroupService.shareVideoCourseProfit(sysUser, param.getBizId()));
    }



    @ApiOperation(value = "老师VIP分享分润")
    @PostMapping(value="/vipProfit")
    public HttpResponseResult<ShareProfitVo> shareVipProfit(@RequestBody ShareProfitParam param) {
        SysUser sysUser = sysUserFeignService.queryUserById(param.getUserId());
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        return succeed(memberPriceSettingsService.shareVipProfit(sysUser,param.getBizId()));
    }

    @ApiOperation(value = "老师活动分享", notes = "活动id")
    @PostMapping(value = "/activity/info/{id}")
    public HttpResponseResult<MusicActivityVo> getDetail(@PathVariable Long id) {
        // SysUser user = sysUserFeignService.queryUserInfo();

        MusicActivityVo activityVo = activityPlanService.getActivityInfo(id, null);

        return succeed(activityVo);
    }


    @ApiOperation(value = "检查活动状态", notes = "活动id")
    @PostMapping(value = "/activity/state/{id}")
    public HttpResponseResult<CheckVo> state(@PathVariable Long id) {
        // SysUser user = sysUserFeignService.queryUserInfo();

        YesOrNoEnum state = activityPlanService.state(id);

        CheckVo checkVo = new CheckVo();
        checkVo.setCheck(state);
        return succeed(checkVo);
    }


    @PostMapping("/memberPriceSettings/vipPermissions")
    @ApiOperation(value = "查询vip权限")
    public HttpResponseResult<List<SysConfig>> vipPermissions() {
        Map<String,Object> params = new HashMap<>();
        params.put("group", "VIP_PERMISSION");
        List<SysConfig> configs = sysConfigService.findAll(params);
        return succeed(configs);
    }

    @ApiOperation(value = "用户老师身份查询")
    @GetMapping("/teacher/identity/{ID}")
    public HttpResponseResult<Boolean> userTeacherIdentityInfo(@PathVariable("ID") Long userId) {

        // 校验请求参数
        if (Optional.ofNullable(userId).orElse(0L) <= 0) {
            return succeed(false);
        }

        // 根据用户ID，查询老师身份信息
        Teacher teacher = teacherService.getOne(Wrappers.<Teacher>lambdaQuery().eq(Teacher::getUserId, userId));

        // 响应数据
        return succeed(Objects.nonNull(teacher));
    }

    @PostMapping("/appVersion")
    @ApiOperation(value = "查询app是否为审核版本")
    public HttpResponseResult<CheckVo> appVersion(@RequestBody Map<String,String> obj) {
        YesOrNoEnum appAuditVersion = appVersionInfoService.getAppAuditVersion(obj.get("platform"), obj.get("version"));
        CheckVo checkVo = new CheckVo();
        checkVo.setCheck(appAuditVersion);
        return succeed(checkVo);
    }
}
