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

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.ShareProfitParam;
import com.yonge.cooleshow.biz.dal.entity.SysConfig;
import com.yonge.cooleshow.biz.dal.service.*;
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.ActivityShareEnum;
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.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/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 MusicAlbumService musicAlbumService;

    /**
     * 服务对象
     */
    @Resource
    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  @Valid 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);
    }
}
