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

import com.yonge.cooleshow.biz.dal.dto.search.MemberPriceSettingsSearch;
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.MemberPriceVo;
import com.yonge.cooleshow.biz.dal.wrapper.liveroom.LiveRoomWrapper;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.cooleshow.student.io.request.LiveRoomVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ksyun.ks3.dto.PostObjectFormFields;
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.vo.MusicActivityVo;
import com.yonge.cooleshow.biz.dal.vo.TeacherVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.thirdparty.entity.UploadSign;
import com.yonge.toolset.utils.string.ValueUtil;


@RestController
@RequestMapping("/open")
@Api(value = "开放权限接口", tags = "开放权限接口")
public class OpenClient extends BaseController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private UploadFileService uploadFileService;
    @Autowired
    private MusicSheetService musicSheetService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private MemberPriceSettingsService memberPriceSettingsService;

    @Autowired
    private ActivityPlanService activityPlanService;
    @Autowired
    private LiveRoomService liveRoomService;

    @ApiOperation(value = "查询老师头像昵称信息")
    @GetMapping("/getTeacher")
    public HttpResponseResult<TeacherVo> getTeacher(@ApiParam(value = "老师ID", required = true) @RequestParam("userId") Long userId) {
        if (null == userId) {
            return failed("缺少老师ID");
        }
        TeacherVo detail = teacherService.detail(userId);
        detail.setIdCardNo(ValueUtil.fuzzyIdCard(detail.getIdCardNo()));
        detail.setPhone(ValueUtil.fuzzyMobile(detail.getPhone()));
        return HttpResponseResult.succeed(detail);
    }

    @ApiOperation(value = "绑定老师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "老师id", required = true),
            @ApiImplicitParam(name = "isUpdate", value = "是否更新绑定老师", dataType = "Boolean")
    })
    @GetMapping("/bindTeacher")
    public HttpResponseResult<Map<String,TeacherVo>> bindTeacher(String phone, Long userId, Boolean isUpdate) {
        if (StringUtil.isEmpty(phone)) {
            return failed("缺少手机号");
        }
        if (null == userId) {
            return failed("缺少老师ID");
        }
        return studentService.bindTeacher(phone, userId, isUpdate);
    }



    @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, user);

        return succeed(activityVo);
    }



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


    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", dataType = "String", value = "为空时默认使用daya"),
            @ApiImplicitParam(name = "fileName", dataType = "String", value = "要上传的文件名称,不包含路径信息"),
            @ApiImplicitParam(name = "postData", dataType = "Map", value = "1.如果使用js sdk上传的时候设置了ACL请设置,例\"acl\":\"public-read\"值要与SDK中一致,没有则删除该项</br>" +
                    "2.提供js sdk中的key值,例\"key\":\"20150115/中文/${filename}\""),
            @ApiImplicitParam(name = "unknowValueField", dataType = "List", value = "对于用户无法确定表单值的放在unknownValueField中(比如有的上传控件会添加一些表单项,但表单项的值可能是随机的)"),
    })
    @ApiOperation(value = "获取上传文件签名", notes = "{\n" +
            "    \"bucketName\":\"\",\n" +
            "    \"filename\":\"test.png\",\n" +
            "    \"postData\":{\n" +
            "        \"acl\":\"public-read\",\n" +
            "        \"key\":\"20150115/中文/${filename}\"\n" +
            "    },\n" +
            "    \"unknowValueField\":[\"test\"]\n" +
            "}")
    @PostMapping("/getUploadSign")
    public HttpResponseResult<PostObjectFormFields> getUploadSign(@RequestBody UploadSign uploadSign) {
        return succeed(uploadFileService.getUploadSign(uploadSign));
    }

    @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/list")
    @ApiOperation(value = "查询列表")
    public HttpResponseResult<MemberPriceVo> list(@RequestBody MemberPriceSettingsSearch query) {
        MemberPriceVo memberPriceVo = memberPriceSettingsService.getVipShare(query);
        return succeed(memberPriceVo);
    }


    @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 = "直播间详情信息", notes = "直播间ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "分享用户ID", dataType = "Long")
    })
    @GetMapping("/liveRoom/detail/{ID}")
    public HttpResponseResult<LiveRoomVO> liveRoomDetailInfo(@PathVariable("ID") String liveRoomId,
                                                             @RequestParam(value = "userId", required = false) Long userId) {

        if (StringUtils.isEmpty(liveRoomId)) {
            return failed("无效的直播间编号");
        }

        LiveRoomWrapper roomWrapper = liveRoomService.findLiveRoomDetailInfoByRoomId(liveRoomId, userId);
        if (Objects.isNull(roomWrapper)) {
            return failed("无效的直播间编号");
        }

        return succeed(LiveRoomVO.from(roomWrapper.jsonString()));
    }
}
