package com.app.controller.api;

import com.app.constant.OpenCourseConstant;
import com.app.constant.SystemConstant;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchPageDto;
import com.app.entity.basics.OpenCourse;
import com.app.entity.basics.YearSemester;
import com.app.entity.guardian.GuardianChild;
import com.app.entity.order.OpenCourseOrderItem;
import com.app.service.basics.OpenCourseService;
import com.app.service.basics.YearSemesterService;
import com.app.service.guardian.GuardianChildService;
import com.app.service.order.OpenCourseOrderItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Optional;

/**
 * 功能描述：开课
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api(description = "开课")
@Slf4j
@Validated
@RestController("ApiOpenCourseController")
@RequestMapping("/api/open-course")
public class OpenCourseController {

    @Autowired
    private GuardianChildService guardianChildService;

    @Autowired
    private OpenCourseService openCourseService;

    @Autowired
    private OpenCourseOrderItemService openCourseOrderItemService;

    @Autowired
    private YearSemesterService schoolYearSemesterService;

    @ApiOperation(value = "查询[开课]")
    @PostMapping("/child/{accountChildId}/page")
    public ResponseEntity<Result<PageResult<OpenCourse>>> page(@Valid @RequestBody SearchPageDto params, @PathVariable("accountChildId") String accountChildId) {
        String organId = SystemConstant.organId;
        Optional<GuardianChild> optional = guardianChildService.findById(accountChildId);
        if (optional.isPresent()) {
            GuardianChild _guardianChild = optional.get();

            YearSemester schoolYearSemester = schoolYearSemesterService.find(organId);
            Integer schoolYear = schoolYearSemester.getSchoolYear();
            Integer semester = schoolYearSemester.getSemester();

            return new ResponseEntity(new Result().ok(openCourseService.page(null,schoolYear, semester, _guardianChild.getSchoolId(),
                    null, _guardianChild.getGradeName(), OpenCourseConstant.OpenCourseType.AFTER_CLASS_COURSE, Boolean.FALSE,
                    params.getPage(), params.getSize())), HttpStatus.OK);
        }

        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @ApiOperation(value = "查询[开课]详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "标识", dataType = "String", paramType = "path", required = true, defaultValue = "")
    })
    @GetMapping("/{id}")
    public ResponseEntity<Result<OpenCourse>> findById(@NotBlank @PathVariable("id") String id) {
        return new ResponseEntity(new Result().ok(openCourseService.findById(id)), HttpStatus.OK);
    }

    @ApiOperation(value = "查询[我的课程]")
    @PostMapping("/my")
    public ResponseEntity<Result<PageResult<OpenCourseOrderItem>>> myOpenCourse(@Valid @RequestBody SearchPageDto params) {
        String accountId = "";
        return new ResponseEntity(new Result().ok(openCourseOrderItemService.myOpenCourse(accountId, params.getPage(), params.getSize())), HttpStatus.OK);
    }

    @ApiOperation(value = "查询[我的选课单]")
    @PostMapping("/my/shopping-car/child/{accountChildId}")
    public ResponseEntity<Result<OpenCourse>> myShoppingCar(@PathVariable("accountChildId") String accountChildId) {
        String accountId = "";
        return new ResponseEntity(new Result().ok(openCourseService.myShoppingCar(accountId, accountChildId)), HttpStatus.OK);
    }

    // todo 课程推荐...

}
