package com.sdut.labex.controller;

import com.alibaba.fastjson.JSONObject;
import com.sdut.labex.Factory.ExperimentFactory;
import com.sdut.labex.entity.Experiment;
import com.sdut.labex.service.ExperimentsService;
import com.sdut.labex.utils.ResVo;
import com.sdut.labex.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * File: ExpermimentsController
 * Created: 2023/6/12
 * Author: springforest
 * Description:
 */
@RestController
@CrossOrigin
public class ExperimentsController {
    @Resource
    private ExperimentsService experimentsService;

    //添加实验
    @PostMapping("/experiment")
    public ResVo applyExperiment(@RequestBody JSONObject jsonObject) {
        Experiment experiment = ExperimentFactory.createExperiment(jsonObject);
        //设置申请人
        experiment.setApplyUser(UserHolder.getUser().getUsername());
        //获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(formattedDateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        experiment.setApplyTime(date);
        experiment.setStatus(0);
        return experimentsService.applyExperiment(experiment);
    }

    //修改实验信息
    @PutMapping("/experiment")
    public ResVo updateExperiment(@RequestBody JSONObject jsonObject) {
        Experiment experiment = ExperimentFactory.createExperiment(jsonObject);
        experiment.setStatus(0);
        return experimentsService.updateExperiment(experiment);
    }

    //删除实验
    @DeleteMapping("/experiment/{id}")
    public ResVo deleteExperiment(@PathVariable String id) {
        return experimentsService.deleteExperiment(id);
    }

    //获取未审核的实验
    @GetMapping("/unreviewed")
    public ResVo getUnreviewedExperiments() {
        return experimentsService.getUnreviewedExperiments();
    }

    //设置实验审核状态
    @PutMapping("/isAdmitted/{id}/{status}")
    public ResVo isAdmit(@PathVariable Integer id, @PathVariable Integer status) {
        return experimentsService.isAdmitted(id, status);
    }

    //获取实验列表
    @PostMapping("/experiments/{pageNum}/{pageSize}")
    public ResVo getExperiments(@RequestBody JSONObject jsonObject, @PathVariable int pageNum, @PathVariable int pageSize) {
        Experiment experiment = ExperimentFactory.createExperiment(jsonObject);
        if (UserHolder.getUser().getRole().equals("teacher")) {
            experiment.setApplyUser(UserHolder.getUser().getUsername());
        }
        return experimentsService.getExperiments(experiment, pageNum, pageSize);
    }

    //按ID获取实验
    @GetMapping("/experiment/{id}")
    public ResVo getExperiment(@PathVariable String id) {
        return experimentsService.getExperiment(id);
    }

    //删除

}
