package com.example.system.controller;

import com.example.system.model.ArticleTable;
import com.example.system.model.Result;
import com.example.system.service.ArticleService;
import com.example.system.service.UserService;
import com.example.system.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;


@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    //获取历史文章并按照升序排列
    @RequestMapping(value = "/getHistoryArticle")
    public Object getHistoryArticle() {
        log.info("获取历史文章");
        return articleService.getHistoryArticle();
    }

    // 创建一篇文章 创建成功返回true，失败返回false 必传内容：文章标题，文章类型，文章内容, 比赛id
    @PostMapping("/createArticle")
    public Object createArticle(ArticleTable article, HttpServletRequest request) {
        log.info("创建一篇文章 article: {}", article);
        if (article.getArticleTitle() == null
                || article.getArticleContent() == null
                || article.getArticleType() == null
                || article.getCompetitionId() == null) {
            return Result.fail("参数不全");
        }
        String userToken =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(userToken);
        article.setUserId((Integer)claims.get("userId"));
        article.setPublishTime(new Date(System.currentTimeMillis()));
        Integer articleId = articleService.createArticle(article);
        if (articleId != null) {
            return Result.success(articleId);
        } else {
            return Result.fail("文章创建失败");
        }
    }

    //根据文章id获取对应的文章;
    @RequestMapping("/getArticleById")
    public Object getArticleById(Integer id) {
        log.info("根据文章id获取对应的文章 id: {}", id);
        if (id <= 0) {
            return Result.fail("文章id不合法");
        }
        ArticleTable ret;
        ret = articleService.getArticleById(id);
        return ret;
    }

    //删除文章(根据文章id)
    @RequestMapping(value = "/deleteArticleById", method = RequestMethod.POST)
    public Object deleteArticleById(@RequestParam Integer id) {
        log.info("删除自己的文章(根据文章id) id: {}", id);
        if (id == null || id <= 0) {
            return Result.fail("文章id不合法");
        }
        Boolean ret = articleService.deleteArticleById(id);
        return ret ? Result.success("删除成功") : Result.fail("删除失败");
    }

    //根据文章id修改文章，只能修改自己的文章
    @RequestMapping("/upData")
    public Object upData(ArticleTable articleTable, HttpServletRequest request) {
        log.info("修改自己的文章 articleTable: {}", articleTable);
        if (articleTable.getUserId() == null
                || articleTable.getArticleId() == null
                || articleTable.getArticleTitle() == null
                || articleTable.getArticleContent() == null
                || articleTable.getArticleType() == null
        ) {
            return Result.fail("参数不全");
        }
        //获取用户的id
        String userToken =request.getHeader("Authorization");
        Claims claims = JWTUtil.checkToken(userToken);
        Integer userId = (Integer) claims.get("userId");
        if (!userId.equals(articleTable.getUserId())) {
            return Result.fail("只能修改自己的文章");
        }
        //修改文章
        if (articleService.updateArticle(articleTable)) {
            return Result.success("更新成功");
        }
        return Result.success("更新失败");
    }
}