package com.yonge.cooleshow.student.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
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.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.entity.UserMusic;
import com.yonge.cooleshow.biz.dal.service.UserMusicService;
import com.yonge.cooleshow.biz.dal.wrapper.UserMusicWrapper;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 java.util.Arrays;
import java.util.List;

import static com.yonge.cooleshow.common.entity.HttpResponseResult.failed;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.student:}/userMusic")
@Api(tags = "用户曲目作品")
public class UserMusicController {

    @Autowired
    private UserMusicService userMusicService;

    @Autowired
    private SysUserFeignService sysUserFeignService;
    @ApiOperation(value = "详情", notes = "用户曲目作品-根据详情ID查询单条, 传入id")
    @GetMapping("/detail/{id}")
    public HttpResponseResult<UserMusicWrapper.UserMusic> detail(@PathVariable("id") Long id) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        UserMusicWrapper.UserMusicQuery build = UserMusicWrapper.UserMusicQuery.builder().id(id).build();
        IPage<UserMusicWrapper.UserMusic> pages = userMusicService.selectPage(QueryInfo.getPage(build), build,
            JwtUserInfo.builder().userId(user.getId().toString()).clientType(ClientEnum.STUDENT.name()).build());
        List<UserMusicWrapper.UserMusic> records = pages.getRecords();
        if (records.isEmpty()) {
            throw new BizException(999,"作品已删除");
        }

        return HttpResponseResult.succeed(records.get(0));
    }

    @ApiOperation(value = "查询分页", notes = "用户曲目作品- 传入 UserMusicWrapper.UserMusicQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<UserMusicWrapper.UserMusic>> page(@RequestBody UserMusicWrapper.UserMusicQuery query) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        query.setUserId(user.getId());
        query.setClientType(ClientEnum.STUDENT);
        JwtUserInfo<Object> userInfo = JwtUserInfo.builder().userId(user.getId().toString()).clientType(ClientEnum.STUDENT.name()).build();
        IPage<UserMusicWrapper.UserMusic> pages = userMusicService.selectPage(QueryInfo.getPage(query), query, userInfo);

        return HttpResponseResult.succeed(QueryInfo.pageInfo(pages));
    }

    @ApiOperation(value = "保存草稿/发布作品", notes = "用户曲目作品- 传入 UserMusicWrapper.UserMusic")
    @PostMapping("/save")
    public HttpResponseResult<String> add(@Validated @RequestBody UserMusicWrapper.AddUserMusic userMusic) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        userMusic.setUserId(user.getId());
        userMusic.setClientType(ClientEnum.STUDENT);
        // 新增数据
        ;

        return HttpResponseResult.succeed(userMusicService.save(userMusic).toString());
    }

    @ApiOperation(value = "删除", notes = "用户曲目作品- 传入id")
    @PostMapping("/remove")
    public HttpResponseResult<Boolean> remove(@RequestParam String id) {

        if (StringUtils.isBlank(id)) {
            throw new BizException( "id不能为空");
        }
        List<String> list = Arrays.asList(id.split(","));

        userMusicService.lambdaUpdate()
            .in(UserMusic::getId, list)
            .set(UserMusic::getDelFlag, true)
            .update();

        return HttpResponseResult.succeed(true);
    }


    @ApiOperation(value = "详情")
    @GetMapping("/musicPracticeRecord/{id}")
    public R<UserMusicWrapper.UserMusic> musicPracticeRecord(@PathVariable("id") Long id) {

        UserMusicWrapper.UserMusicQuery build = UserMusicWrapper.UserMusicQuery.builder().musicPracticeRecordId(id).build();
        IPage<UserMusicWrapper.UserMusic> pages = userMusicService.selectPage(QueryInfo.getPage(build), build);
        List<UserMusicWrapper.UserMusic> records = pages.getRecords();
        if (records.isEmpty()) {
            return R.from(new UserMusicWrapper.UserMusic()).data(null);
        }

        return R.from(records.get(0));
    }
}
