package com.yonge.cooleshow.student.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.EvaluationRecordDto;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.queryInfo.UserRewardQueryInfo;
import com.yonge.cooleshow.biz.dal.service.ActivityEvaluationRecordService;
import com.yonge.cooleshow.biz.dal.service.ActivityPlanService;
import com.yonge.cooleshow.biz.dal.service.ActivityUserRewardService;
import com.yonge.cooleshow.biz.dal.vo.CheckVo;
import com.yonge.cooleshow.biz.dal.vo.activity.UserRewardWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.cooleshow.student.io.request.ActivityVo;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
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.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "学生活动接口")
@RestController
@RequestMapping("/activity")
public class StudentActivityController extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(StudentActivityController.class);
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private ActivityPlanService activityPlanService;

    @Autowired
    private ActivityEvaluationRecordService activityEvaluationRecordService;

    @Autowired
    private ActivityUserRewardService activityUserRewardService;

    @ApiOperation(value = "评测活动参与项目", notes = "评测项目id")
    @PostMapping(value = "/evaluation/{evaluationId}")
    public HttpResponseResult<Boolean> joinEvaluation(@PathVariable Long evaluationId) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        return succeed(activityPlanService.joinEvaluation(evaluationId, user));
    }


    @ApiOperation(value = "免费活动报名活动报名", notes = "活动id")
    @PostMapping(value = "/joinActivity/{id}")
    public HttpResponseResult<Boolean> joinActivity(@PathVariable Long id) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        return succeed(activityPlanService.joinActivity(id, user));
    }


    @ApiOperation(value = "保存活动分数")
    @PostMapping("/evaluationScore")
    public HttpResponseResult evaluationScore( @RequestBody EvaluationRecordDto record){

        // 判断是否为活动评测 活动评测 记录活动记录
        if (record.getEvaluationId() != null) {
            try {
                activityEvaluationRecordService.saveRecord(record.getEvaluationId(),record.getUserId(),record.getScore());
            } catch (Exception e) {
                e.printStackTrace();
                log.error("活动评测记录保存失败，{}",e.fillInStackTrace());
            }
        }
        return succeed();
    }


    @ApiOperation(value = "检查是否存在未领奖")
    @GetMapping("/checkReceiveReward")
    public HttpResponseResult<CheckVo> checkReceiveReward(){

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

        Boolean b = activityUserRewardService.checkReceiveReward(user.getId(), ClientEnum.STUDENT);
        YesOrNoEnum check = b?YesOrNoEnum.YES:YesOrNoEnum.NO;
        CheckVo checkVo = new CheckVo();
        checkVo.setCheck(check);

        return succeed(checkVo);
    }


    @ApiOperation(value = "查看领奖列表")
    @PostMapping("/receiveRewardList")
    public HttpResponseResult<PageInfo<ActivityVo.ReceiveRewardList>> receiveRewardList(@RequestBody ActivityVo.ReceiveRewardQuery query){

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

        query.setClient(ClientEnum.STUDENT);
        query.setReceive(YesOrNoEnum.NO);
        query.setUserId(user.getId());

        IPage<UserRewardWrapper> iPage = activityUserRewardService.receiveRewardList(PageUtil.getPage(query), UserRewardQueryInfo.from(query.jsonString()));

        // 数据转换
        List<ActivityVo.ReceiveRewardList> pageInfos = JSON.parseArray(JSON.toJSONString(iPage.getRecords()),
                                                                                        ActivityVo.ReceiveRewardList.class);

        return succeed(PageUtil.getPageInfo(iPage,pageInfos));
    }


    @ApiOperation(value = "领取奖品")
    @PostMapping("/receiveReward/{receiveRewardId}")
    public HttpResponseResult<CheckVo> receiveReward(@PathVariable Long receiveRewardId){

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

