package com.example.system.controller;

import com.example.system.model.InformationTable;
import com.example.system.model.ResultInformationTable;
import com.example.system.model.UserTable;
import com.example.system.service.InformationService;
import com.example.system.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.crossstore.ChangeSetPersister;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import org.springframework.web.bind.annotation.CrossOrigin;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin
@RequestMapping("/api/information")
public class InformationController {

    @Autowired
    private InformationService informationService;

    // 获取用户的文档资料列表
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<InformationTable>> getUserInformation(@PathVariable("userId") Long userId) {
        List<InformationTable> informationList = informationService.getUserInformation(userId);
        return new ResponseEntity<>(informationList, HttpStatus.OK);
    }

    // 创建新的文档资料
    @PostMapping("/user/{userId}/competition/{competitionId}")
    public ResponseEntity<InformationTable> createInformation(@PathVariable("userId") Long userId,
                                                              @PathVariable("competitionId") Long competitionId,
                                                              @RequestBody InformationTable information) {
        InformationTable createdInformation = informationService.createInformation(userId, competitionId, information);
        return new ResponseEntity<>(createdInformation, HttpStatus.CREATED);
    }

    // 编辑文档资料
    @PutMapping("/{informationId}")
    public ResponseEntity<InformationTable> updateInformation(@PathVariable("informationId") Long informationId,
                                                              @RequestBody InformationTable information) {
        InformationTable updatedInformation;
        try {
            updatedInformation = informationService.updateInformation(informationId, information);
        } catch (NotFoundException e) {
            throw new RuntimeException(e);
        }
        return new ResponseEntity<>(updatedInformation, HttpStatus.OK);
    }

    // 删除文档资料
    @DeleteMapping("/{informationId}")
    public ResponseEntity<Void> deleteInformation(@PathVariable("informationId") Long informationId) {
        informationService.deleteInformation(informationId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    // 获取所有资料
    @RequestMapping("/getAll")
    public List<ResultInformationTable> getAll() {
        return informationService.getAll();
    }

    //获取比赛相关的资料
    @RequestMapping("/getPortion")
    public List<ResultInformationTable> getPortion(Long competitionId) {
        return informationService.getPortion(competitionId);
    }
    //获取自己的资料
    @RequestMapping("/getSelf")
    public List<ResultInformationTable> getSelf(HttpSession session) {
        String toKen = (String) session.getAttribute("Authorization");
        System.out.println("Token: " + toKen);
        Claims claims = JWTUtil.checkToken(toKen);
        Long userId = (Long) claims.get("userId");
        return informationService.getSelf(userId);
    }
}