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

import io.swagger.annotations.ApiOperation;

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.web.support.ControllerSupport;
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;

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

    @Autowired
    private CoursePropertyService coursePropertyService;

    @ApiOperation(value = "根据orgId查询所有课程属性", notes = "不带分页")
    @GetMapping(value = "/courseProperty/all")
    public ResponseEntity<Object> findAllByOrg() {
        User user = getAccessUser();
        if (user != null) {
            List<CourseProperty> courseProperties = coursePropertyService.findAllByOrgId(user.getRootOrgId());
            return new ResponseEntity<Object>(courseProperties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据orgId查询所有课程属性", notes = "带分页")
    @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) {
            return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
        }
        coursePropertyDto.setOrgId(user.getRootOrgId());
        Page<CourseProperty> coursePropertiesPage = coursePropertyService.findAllByOrg(coursePropertyDto, new PageRequest(curPage - 1, pageSize, new Sort(Direction.DESC, "id")));
        return new ResponseEntity<Object>(coursePropertiesPage, HttpStatus.OK);
    }

    @ApiOperation(value = "新增课程属性", notes = "新增课程属性")
    @PostMapping(value = "/courseProperty/save")
    public ResponseEntity<Object> saveCourseProperty(@RequestBody CoursePropertyDto coursePropertyDto) {
        User user = getAccessUser();
        if (user != null) {
            try {
                coursePropertyDto.setOrgId(user.getRootOrgId());
                coursePropertyService.saveCourseProperty(coursePropertyDto);
            } catch (Exception e) {
                return new ResponseEntity<Object>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "启用", notes = "启用")
    @PutMapping(value = "/courseProperty/open/{id}")
    public ResponseEntity<Object> openCourseProperty(@PathVariable Long id) {
        User user = getAccessUser();
        if (user != null) {
            coursePropertyService.openCourseProperty(id);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "批量启用", notes = "启用")
    @PutMapping(value = "/courseProperty/opens/{ids}")
    public ResponseEntity<Object> opensCourseProperty(@PathVariable String ids) {
        User user = getAccessUser();
        if (user != null) {
            List<String> idList = Stream.of(ids.split(",")).collect(Collectors.toList());
            coursePropertyService.opensCourseProperty(idList);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "禁用", notes = "禁用")
    @PutMapping(value = "/courseProperty/close/{id}")
    public ResponseEntity<Object> closeCourseProperty(@PathVariable Long id) {
        User user = getAccessUser();
        if (user != null) {
            coursePropertyService.closeCourseProperty(id);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "批量禁用", notes = "禁用")
    @PutMapping(value = "/courseProperty/closes/{ids}")
    public ResponseEntity<Object> closesCourseProperty(@PathVariable String ids) {
        User user = getAccessUser();
        if (user != null) {
            List<String> idList = Stream.of(ids.split(",")).collect(Collectors.toList());
            coursePropertyService.closesCourseProperty(idList);
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据courseId查询所有课程属性", notes = "不带分页")
    @GetMapping(value = "/courseProperty/all/{courseId}")
    public ResponseEntity<Object> findAllByCourseId(@PathVariable Long courseId) {
        User user = getAccessUser();
        if (user != null) {
            List<CourseProperty> courseProperties = coursePropertyService.findAllByCourseId(courseId);
            return new ResponseEntity<Object>(courseProperties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据courseCode查询所有课程属性", notes = "不带分页")
    @GetMapping(value = "/courseProperty/code/{courseCode}")
    public ResponseEntity<Object> findAllByCourseCode(@PathVariable String courseCode) {
        User user = getAccessUser();
        if (user != null) {
            List<CourseProperty> courseProperties = coursePropertyService.findAllByCourseCode(courseCode);
            return new ResponseEntity<Object>(courseProperties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }

    @ApiOperation(value = "根据courseCode查询所有开启的课程属性", notes = "不带分页")
    @GetMapping(value = "/courseProperty/enable/{courseCode}")
    public ResponseEntity<Object> findAllEnable(@PathVariable String courseCode) {
        User user = getAccessUser();
        if (user != null) {
            List<CourseProperty> courseProperties = coursePropertyService.findAllEnable(courseCode, true);
            return new ResponseEntity<Object>(courseProperties, HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.NOT_FOUND);
    }
}
