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

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.qmth.examcloud.core.questions.dao.entity.AudioTimeConfig;
import cn.com.qmth.examcloud.core.questions.service.AudioTimeConfigService;

/**
 * @author chenken
 * @date 2017年8月10日 下午3:31:42
 * @company QMTH
 * @description AudioTimeConfigController.java
 */
@Controller
@RequestMapping("${api_cqb}/")
public class AudioTimeConfigController extends ControllerSupport {

    @Autowired
    private AudioTimeConfigService audioTimeConfigService;

    @ApiOperation(value = "根据examId查询音频设置 ", notes = "根据examId查询音频设置")
    @PostMapping(value = "/audioTimeConfig/{examId}/{courseCode}")
    public ResponseEntity<Object> findAudioTimeConfig(@PathVariable String examId, @PathVariable String courseCode) {
        List<AudioTimeConfig> audioTimeConfigs = audioTimeConfigService.findAudioTimeConfigByExamId(examId, courseCode);
        return new ResponseEntity<Object>(audioTimeConfigs, HttpStatus.OK);
    }

    @ApiOperation(value = "根据传入的对象查询音频题", notes = "根据传入的对象查询音频题")
    @PostMapping("/audioTimeConfig/all")
    public ResponseEntity<Object> findAudioTimeConfigList(@RequestBody List<AudioTimeConfig> audioTimeConfigModelList) {
        List<AudioTimeConfig> list = audioTimeConfigService.findAudioTimeConfigList(audioTimeConfigModelList);
        return new ResponseEntity<Object>(list, HttpStatus.OK);
    }

    @ApiOperation(value = "根据传入的对象查询已经保存的音频题", notes = "根据传入的对象查询已经保存的音频题")
    @PostMapping("/audioTimeConfig/update")
    public ResponseEntity<Object> findUpdateList(@RequestBody List<AudioTimeConfig> audioTimeConfigModelList) {
        List<AudioTimeConfig> list = audioTimeConfigService.findUpdateAudioTimeConfigList(audioTimeConfigModelList);
        return new ResponseEntity<Object>(list, HttpStatus.OK);
    }

    @ApiOperation(value = "新增音频播放次数", notes = "新增音频播放次数")
    @PostMapping("/addAudioTimeConfig")
    public ResponseEntity<Object> addAudioTimeConfig(@RequestBody List<AudioTimeConfig> audioTimeConfigList, HttpServletRequest request) {
        User user = getAccessUser();
        if (user != null) {
            audioTimeConfigService.saveAudioTimeConfig(audioTimeConfigList, user);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "清理音频播放次数设置", notes = "清理音频播放次数设置")
    @PostMapping("/deleteAudioTimeConfig/{examId}/{courseCode}")
    public ResponseEntity<Object> deleteAudioTimeConfig(@PathVariable String examId, @PathVariable String courseCode) {
        User user = getAccessUser();
        if (user != null) {
            audioTimeConfigService.deleteByExamIdAndCourseCode(examId, courseCode);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据examID，courseCode，groupCode，paperDetailUnit的ID查询音频次数", notes = "查询音频次数")
    @GetMapping("/seachAudioTimeConfig/{examId}/{courseCode}/{groupCode}/{paperDetailUnitId}")
    public ResponseEntity<Object> seachAudioTimeConfig(@PathVariable String examId,
                                                       @PathVariable String courseCode,
                                                       @PathVariable String groupCode,
                                                       @PathVariable String paperDetailUnitId) {
        Map<String, String> map = audioTimeConfigService.findAudioTimeConfig(examId, courseCode, groupCode, paperDetailUnitId);
        return new ResponseEntity<Object>(map, HttpStatus.OK);
    }
}

