package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.app.annotation.NoRepeatSubmit;
import com.fs.app.domain.FsAppRole;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.FsUserRewards;
import com.fs.his.dto.ClaimRewardsAddDTO;
import com.fs.his.enums.ActivityTypeEnum;
import com.fs.his.enums.RewardTypeEnum;
import com.fs.his.service.IAppUserRewardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
@Slf4j
@Api("App用户奖励")
@RestController
@RequestMapping(value="/app/reward")
public class AppUserRewardController  extends AppBaseController{

    @Autowired
    private IAppUserRewardService appUserRewardService;

    /**
     * 跳转下发首次登录注册App奖励
     * */
    @Login
    @ApiOperation("跳转下发注册登录奖励")
    @GetMapping("/issueFirstLoginReward")
    @NoRepeatSubmit(expire = 5, message = "请勿重复点击，正在处理中...")
    public R issueFirstLoginReward() {
        String loginUserId = getUserId();
        if (StringUtils.isEmpty(loginUserId)){
            return R.error("请登录");
        }
        Long fsUserId = Long.valueOf(loginUserId);
        boolean rewardAdded = appUserRewardService.checkFirstLoginRewardStatus(fsUserId);
        if (rewardAdded) {
            //添加首次登录奖励
            appUserRewardService.addUserFirstLoginRewards(fsUserId);
            log.info("用户首次登录奖励添加成功: userId={}", fsUserId);
        }
        return R.ok();
    }

    /**
     * 跳转下发App连续看课奖励
     * */
    @Login
    @ApiOperation("跳转下发App连续看课奖励")
    @PostMapping("/issueWatchCourseReward")
    @NoRepeatSubmit(expire = 5, message = "请勿重复点击，正在处理中...")
    public R issueWatchCourseReward(@RequestBody FsUserRewards queryRewards) {
        String loginUserId = getUserId();
        if (StringUtils.isEmpty(loginUserId)) {
            return R.error("请登录");
        }
        if (queryRewards.getVideoId() == null) {
            return R.error("缺少视频参数");
        }
        Long fsUserId = Long.valueOf(loginUserId);
        try {
            //添加看课奖励
            appUserRewardService.addUserWatchCourseRewards(fsUserId, queryRewards.getVideoId());
            log.info("用户App看课奖励添加成功: userId={}, videoId={}", fsUserId, queryRewards.getVideoId());
            return R.ok();
        } catch (CustomException e) {
            log.warn("看课奖励发放被拦截: {}", e.getMessage());
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error("看课奖励发放异常", e);
            return R.error("系统异常，请稍后重试");
        }
    }
    /**
     * 查询我的奖品列表
     */
    @Login
    @ApiOperation("查询用户的奖品列表")
    @PostMapping("/getMyRewardList")
    public TableDataInfo getMyRewardList(@RequestBody FsUserRewards queryRewards) {
        String loginUserId = getUserId();
        if (StringUtils.isEmpty(loginUserId)){
            return getDataTable(Collections.emptyList());
        }
        queryRewards.setFsUserId(Long.valueOf(loginUserId));
        startPage();
        List<FsUserRewards> list = appUserRewardService.getMyRewardList(queryRewards);
        if (CollectionUtils.isEmpty(list)){
            return getDataTable(Collections.emptyList());
        }
        return getDataTable(list);
    }
    /**
     * 奖品列表点击领取奖励
     */
    @Login
    @ApiOperation("奖品列表用户领取奖励")
    @PostMapping("/claim")
    @NoRepeatSubmit(expire = 5)
    public R claimRewards(@RequestBody ClaimRewardsAddDTO claimRewardsAddDTO) {//领取商品时必传收货地址id
        String loginUserId = getUserId();
        if (StringUtils.isEmpty(loginUserId)){
            return R.error("请登录");
        }
        claimRewardsAddDTO.setFsUserId(Long.valueOf(loginUserId));
        try {
            return appUserRewardService.claimRewards(claimRewardsAddDTO);
        } catch (CustomException e) {
            log.error("领取奖品失败: {}", e.getMessage());
            if (e.getCode() != null) {
                return R.error(e.getCode(), e.getMessage());
            }
            return R.error(e.getMessage());
        } catch (Exception e) {
            log.error("领取奖品系统异常", e);
            return R.error("系统异常");
        }
    }

    /**
     * 首次登录注册App弹窗
     */
    @Login
    @ApiOperation("判断是否展示用户首次登录注册App弹窗")
    @GetMapping("/showFirstLoginRewardWindows")
    public R showFirstLoginRewardWindows() {
        String loginUserId = getUserId();
        if (StringUtils.isEmpty(loginUserId)){
            return R.error("请登录");
        }
        FsAppRole result = appUserRewardService.showFirstLoginRewardWindows(Long.valueOf(loginUserId));
        return R.ok().put("data", result);
    }

    /**
     * 连续看课奖励弹窗
     */
    @Login
    @ApiOperation("判断是否展示用户App连续看课奖励弹窗")
    @GetMapping("/showWatchCourseRewardWindows")
    public R showWatchCourseRewardWindows() {
        String loginUserId = getUserId();
        if (StringUtils.isEmpty(loginUserId)){
            return R.error("请登录");
        }
        FsAppRole result = appUserRewardService.showWatchCourseRewardWindows(Long.valueOf(loginUserId));
        return R.ok().put("data", result);
    }
    /**
     * 查询奖品类型
     */
    @Login
    @ApiOperation("查询奖品类型")
    @GetMapping("/getRewardType")
    public R getRewardType() {
        RewardTypeEnum[] values = RewardTypeEnum.values();
        R response = R.ok();
        response.put("data", values);
        return response;
    }
    /**
     * 查询活动类型
     */
    @Login
    @ApiOperation("查询活动类型")
    @GetMapping("/activityType")
    public R getActivityType() {
        ActivityTypeEnum[] values = ActivityTypeEnum.values();
        R response = R.ok();
        response.put("data", values);
        return response;
    }

    /**
     * 记录用户当日的有效看课标识
     */
    @Login
    @ApiOperation("记录用户累计有效看课标识")
    @GetMapping("/recordUserWatchCourseDays")
    public R recordUserWatchCourseDays() {
        String loginUserId = getUserId();
        appUserRewardService.recordUserWatchCourseDays(Long.valueOf(loginUserId));
        return R.ok();
    }
}
