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

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.base.Constants;
import cn.com.qmth.examcloud.core.questions.dao.entity.CourseProperty;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyDto;
import cn.com.qmth.examcloud.core.questions.service.CoursePropertyService;
import cn.com.qmth.examcloud.web.support.ControllerSupport;
import io.swagger.annotations.ApiOperation;

/**
 * @author weiwenhai
 * @describle 课程属性
 * @date 2017.11.2
 */
@Controller
@RequestMapping("${api_cqb}/")
public class CoursePropertyController extends ControllerSupport {

    @Autowired
    private CoursePropertyService coursePropertyService;

    @ApiOperation(value = "根据orgId查询所有课程属性")
    @GetMapping(value = "/courseProperty/all")
    public ResponseEntity<Object> findAllByOrg() {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<CourseProperty> courseProperties = coursePropertyService.findAllByOrgId(user.getRootOrgId());
        return new ResponseEntity<>(courseProperties, HttpStatus.OK);
    }

    @ApiOperation(value = "根据orgId查询所有课程属性(分页)")
    @GetMapping(value = "/courseProperty/all/{curPage}/{pageSize}")
    public ResponseEntity<Object> findAllByOrgId(@ModelAttribute CoursePropertyDto coursePropertyDto,
            @PathVariable Integer curPage, @PathVariable Integer pageSize) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        coursePropertyDto.setOrgId(user.getRootOrgId());

        PageRequest pageRequest = PageRequest.of(curPage - 1, pageSize, new Sort(Direction.DESC, "updateTime"));
        Page<CourseProperty> coursePropertiesPage = coursePropertyService.findList(coursePropertyDto, pageRequest);

        return new ResponseEntity<>(coursePropertiesPage, HttpStatus.OK);
    }

    @ApiOperation(value = "新增课程属性", notes = "新增课程属性")
    @PostMapping(value = "/courseProperty/save")
    public ResponseEntity<Object> saveCourseProperty(@RequestBody CoursePropertyDto coursePropertyDto) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        coursePropertyDto.setOrgId(user.getRootOrgId());
        coursePropertyService.saveCourseProperty(coursePropertyDto);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "启用")
    @PutMapping(value = "/courseProperty/open/{id}")
    public ResponseEntity<Object> openCourseProperty(@PathVariable String id) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        coursePropertyService.updateCoursePropertyStatus(id, true);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "批量启用")
    @PutMapping(value = "/courseProperty/opens/{ids}")
    public ResponseEntity<Object> opensCourseProperty(@PathVariable String ids) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<String> idList = Stream.of(ids.split(",")).collect(Collectors.toList());
        coursePropertyService.updateCoursePropertyStatus(idList, true);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "禁用")
    @PutMapping(value = "/courseProperty/close/{id}")
    public ResponseEntity<Object> closeCourseProperty(@PathVariable String id) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        coursePropertyService.updateCoursePropertyStatus(id, false);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "批量禁用")
    @PutMapping(value = "/courseProperty/closes/{ids}")
    public ResponseEntity<Object> closesCourseProperty(@PathVariable String ids) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<String> idList = Stream.of(ids.split(",")).collect(Collectors.toList());
        coursePropertyService.updateCoursePropertyStatus(idList, false);

        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation(value = "根据courseId查询所有课程属性")
    @GetMapping(value = "/courseProperty/all/{courseId}")
    public ResponseEntity<Object> findAllByCourseId(@PathVariable Long courseId) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<CourseProperty> courseProperties = coursePropertyService.findAllByCourseId(courseId);
        return new ResponseEntity<>(courseProperties, HttpStatus.OK);
    }

    @ApiOperation(value = "根据courseCode查询所有课程属性")
    @GetMapping(value = "/courseProperty/code/{courseCode}")
    public ResponseEntity<Object> findAllByCourseCode(@PathVariable String courseCode) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<CourseProperty> courseProperties = coursePropertyService.findAllByCourseCode(courseCode);
        return new ResponseEntity<>(courseProperties, HttpStatus.OK);
    }

    @ApiOperation(value = "根据courseCode查询所有开启的课程属性")
    @GetMapping(value = "/courseProperty/enable/{courseCode}")
    public ResponseEntity<Object> findAllEnable(@PathVariable String courseCode) {
        User user = getAccessUser();
        if (user == null) {
            throw new StatusException(Constants.SYS_CODE_500, "请先登录！");
        }

        List<CourseProperty> courseProperties = coursePropertyService.findByEnable(courseCode, true);
        return new ResponseEntity<>(courseProperties, HttpStatus.OK);
    }

}