package com.vote.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.utils.JwtUtils;
import com.vctgo.common.core.utils.StrUtils;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.security.auth.AuthUtil;
import com.vctgo.common.security.service.TokenService;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.model.LoginUser;
import com.vote.system.domain.VoteRegistrations;
import com.vote.system.domain.VoteSubmission;
import com.vote.system.domain.appletDto.VoteAppletNewsDto;
import com.vote.system.domain.appletVo.VoteEventAppletnNewsVo;
import com.vote.system.domain.appletVo.VoteEventNewsVo;
import com.vote.system.domain.dto.VoteEventWorkSettingsDto;
import com.vote.system.domain.dto.VoteRegistrationsDto;
import com.vote.system.domain.dto.VoteSubmissionDto;
import com.vote.system.service.VoteEventAppletService;
import javafx.scene.control.RadioButton;
import org.checkerframework.checker.index.qual.GTENegativeOne;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 小程序接口
 *
 * @author feng
 * @date 2024/9/2
 */
@RestController
@RequestMapping("/v1")
public class VoteEventAppletController {

    @Autowired
    private VoteEventAppletService voteEventAppletService;

    @Autowired
    private TokenService tokenService;


    /**
     * 首页 活动 轮播图
     */
    @GetMapping("/carouselsList")
    public AjaxResult getCarouselsList() {
        return AjaxResult.success(voteEventAppletService.carouselsList());
    }


    /**
     * 查看轮播图详情
     */
    @GetMapping("/carousels/{id}")
    public AjaxResult getCarousels(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventAppletService.getCarousels(id));
    }

    /**
     * 查看活动详情
     */
    @GetMapping("/event/{id}")
    public AjaxResult getNews(@PathVariable("id") Long id) {
        return AjaxResult.success(voteEventAppletService.getEvent(id));
    }

    /**
     * 查询新闻列表
     */
    @GetMapping("/newsList")
    public AjaxResult getNewsList(VoteAppletNewsDto voteAppletNewsDto) {
        IPage<VoteEventNewsVo> page = voteEventAppletService.getNewsList(voteAppletNewsDto);
        return AjaxResult.success(page);
    }

    /**
     * 查看新闻详情
     *
     * @param voteAppletNewsDto
     * @return
     */
    @GetMapping("/newsDetails")
    public AjaxResult newsDetails(VoteAppletNewsDto voteAppletNewsDto) {
        return AjaxResult.success(voteEventAppletService.getNewsDetails(voteAppletNewsDto));
    }

    /**
     * 点赞人列表
     */
    @GetMapping("/likeRecord")
    public AjaxResult getLikeRecord(VoteAppletNewsDto voteAppletNewsDto) {
        IPage<String> page = voteEventAppletService.getLikeRecord(voteAppletNewsDto.getNewsId());
        return AjaxResult.success(page);
    }

    /**
     * 新闻点赞
     */
    @PostMapping("/thumbsUp")
    public AjaxResult thumbsUp(@RequestBody VoteAppletNewsDto voteAppletNewsDto, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        voteAppletNewsDto.setUserId(loginUser.getUserid());
        return AjaxResult.success(voteEventAppletService.thumbsUp(voteAppletNewsDto));
    }

    /**
     * 报名
     */
    @PostMapping("/signUp")
    public AjaxResult signUp(@RequestBody VoteRegistrationsDto voteRegistrationsDto, HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String userId = JwtUtils.getUserId(token);
            voteRegistrationsDto.setUid(Long.valueOf(userId));
        }
        return AjaxResult.success(voteEventAppletService.signUp(voteRegistrationsDto));
    }

    /**
     * 作品类型回显
     */
    @GetMapping("/workType/{eventId}")
    public AjaxResult getWorkType(@PathVariable("eventId") Long eventId) {
        return AjaxResult.success(voteEventAppletService.getWorkType(eventId));
    }

    /**
     * 提交作品
     */
    @PostMapping("/submission")
    public AjaxResult submission(@RequestBody VoteSubmissionDto voteSubmissionDto, HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String userId = JwtUtils.getUserId(token);
            voteSubmissionDto.setUserId(Long.valueOf(userId));
        }
        return AjaxResult.success(voteEventAppletService.submission(voteSubmissionDto));
    }

    /**
     * 查询作品表模板
     */
    @GetMapping("/submissionFormDate/{workSettingsId}")
    public AjaxResult submissionFormDate(@PathVariable("workSettingsId") Long workSettingsId) {
        return AjaxResult.success(voteEventAppletService.getsSubmissionFormDate(workSettingsId));
    }

    /**
     * 流程状态
     */
    @GetMapping("/registrationsStatus")
    public AjaxResult registrationsStatus(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        return AjaxResult.success("成功", voteEventAppletService.registrationsStatus(loginUser.getUserid()));
    }

    /**
     * 获取用户作品
     */
    @GetMapping("/getUserSubmission")
    public AjaxResult getUserSubmission(HttpServletRequest request) {
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String userId = JwtUtils.getUserId(token);
            return AjaxResult.success(voteEventAppletService.getUserSubmission(Long.valueOf(userId)));
        }
        return AjaxResult.error("获取用户token失败");
    }

    /**
     * 获取作品详情
     */
    @GetMapping("/submissionDetail/{submissionId}")
    public AjaxResult getSubmissionDetail(@PathVariable("submissionId") Long submissionId) {
        return AjaxResult.success(voteEventAppletService.getSubmissionDetail(submissionId));
    }


    /**
     * 编辑作品
     */
    @PutMapping("/updateById")
    public AjaxResult updateById(@RequestBody VoteSubmission voteSubmission) {
        return AjaxResult.success(voteEventAppletService.updateById(voteSubmission));
    }

    /**
     * 获取园所
     */
    @GetMapping("/getBranchKindergarten")
    public AjaxResult getBranchKindergarten(VoteRegistrationsDto voteRegistrationsDto) {
        return AjaxResult.success(voteEventAppletService.getBranchKindergarten(voteRegistrationsDto));
    }


    /**
     * 用户详情信息
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(HttpServletRequest request) {
        return AjaxResult.success(tokenService.getLoginUser(request));
    }

    /**
     * 当前用户在该活动下的报名详情
     */
    @GetMapping("/getRegDetail/{eventId}")
    public AjaxResult getRegDetail(HttpServletRequest request, @PathVariable("eventId") Long eventId) {
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String userId = JwtUtils.getUserId(token);
            return AjaxResult.success(voteEventAppletService.getRegDetail(Long.valueOf(userId),eventId));
        }
        return AjaxResult.error("获取用户token失败");
    }


    /**
     * 修改用户报名信息
     */
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody VoteRegistrations voteRegistrations,HttpServletRequest request){
        String token = SecurityUtils.getToken(request);
        if (StrUtils.isNotEmpty(token)) {
            String userId = JwtUtils.getUserId(token);
            return AjaxResult.success(voteEventAppletService.edit(Long.valueOf(userId),voteRegistrations));
        }
        return AjaxResult.error("获取用户token失败");
    }
}
