package cn.com.qmth.examcloud.core.questions.api.controller;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.dao.PaperDetailRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetail;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.service.PaperDetailService;
import cn.com.qmth.examcloud.core.questions.service.PaperService;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by songyue on 16/12/28.
 */
@RestController
@RequestMapping("${api_cqb}/")
public class PaperDetailController extends ControllerSupport {

    @Autowired
    PaperDetailService paperDetailService;

    @Autowired
    PaperService paperService;

    @Autowired
    PaperDetailRepo paperDetailRepo;

    /**
     * 获取大题对应的小题
     *
     * @param detail_id
     * @return
     */
    @ApiOperation(value = "获取大题对应的小题", notes = "获取大题对应的小题")
    @GetMapping(value = "/paperDetail/units/{detail_id}")
    public List<PaperDetailUnit> getUnitsByPaperDetailId(String detail_id) {
        return paperDetailService.getUnitsByPaperDetailId(detail_id);
    }

    /**
     * 获取大题
     *
     * @param detail_id
     * @return
     */
    @ApiOperation(value = "获取大题", notes = "获取大题")
    @GetMapping(value = "/paperDetail/{detail_id}")
    public PaperDetail getPaperDetailId(@PathVariable String detail_id) {
        return paperDetailService.findById(detail_id);
    }

    /**
     * 更新大题信息
     *
     * @param pd
     * @return
     */
    @ApiOperation(value = "更新试卷中的大题", notes = "更新试卷中的大题")
    @PostMapping(value = "/updatePaperDetail/{paperId}")
    public ResponseEntity<Object> updatePaperDetail(HttpServletRequest request,
                                                    @PathVariable String paperId,
                                                    @RequestBody PaperDetail pd) {
        User user = getAccessUser();
        //AccessUser user = (AccessUser) request.getAttribute("accessUser");
        PaperDetail paperDetail = paperDetailService.savePaperDetail(pd, paperId, user);
        return new ResponseEntity<>(paperDetail, HttpStatus.OK);

    }

    /**
     * 新增大题
     *
     * @param pd
     * @return
     */
    @ApiOperation(value = "新增大题", notes = "新增大题")
    @PostMapping(value = "/paperDetail")
    public ResponseEntity<Object> addPaperDetail(@RequestBody PaperDetail pd) {
        PaperDetail paperDetail = paperDetailRepo.save(pd);
        return new ResponseEntity<>(paperDetail, HttpStatus.OK);
    }

    /**
     * 删除大题
     *
     * @param detailId
     * @return
     */
    @ApiOperation(value = "删除大题", notes = "删除大题")
    @DeleteMapping(value = "/paperDetail/{paperId}/{detailId}")
    public ResponseEntity<Object> removePaperDetail(@PathVariable String paperId,@PathVariable String detailId) {
    	User user = getAccessUser();
        paperDetailService.deletePaperDetail(paperId,detailId,user);
        return new ResponseEntity<>(detailId, HttpStatus.OK);
    }

    /**
     * 根据试卷ID得到所有大题
     *
     * @param paperId
     * @return
     */
    @ApiOperation(value = "根据试卷ID得到所有大题", notes = "根据试卷ID得到所有大题")
    @GetMapping(value = "/paperDetail/paper/{paperId}")
    public ResponseEntity<Object> getPaperDetailsByPaperId(@PathVariable String paperId) {
        return new ResponseEntity<>(paperService.findPaperDetailsById(paperId), HttpStatus.OK);
    }
    
    @ApiOperation(value = "移动大题", notes = "移动大题")
    @PutMapping(value = "/paperDetail/{paperId}/{detailId}/{vc}")
    public ResponseEntity<Object> movePaperDetail(@PathVariable String paperId,@PathVariable String detailId,@PathVariable String vc) {
    	User user = getAccessUser();
        paperDetailService.movePaperDetail(paperId,detailId,vc,user);
        return new ResponseEntity<>(detailId, HttpStatus.OK);
    }

}
