package com.qxgmat.controller.admin;

import com.github.pagehelper.Page;
import com.nuliji.tools.PageMessage;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.dto.admin.request.*;
import com.qxgmat.help.ShiroHelp;
import com.qxgmat.service.inline.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController("AdminReadyController")
@RequestMapping("/admin/ready")
@Api(tags = "GetReady接口", description = "ready相关", produces = MediaType.APPLICATION_JSON_VALUE)
public class ReadyController {
    private static final Logger logger = LoggerFactory.getLogger(QuestionController.class);
    @Autowired
    private ShiroHelp shiroHelp;

    @Autowired
    private ReadyArticleService readyArticleService;

    @Autowired
    private ReadyArticleCategoryService readyArticleCategoryService;

    @Autowired
    private ReadyRoomService readyRoomService;

    @Autowired
    private ReadyRoomAreaService readyRoomAreaService;

    @Autowired
    private ReadyReadService readyReadService;

    @Autowired
    private ReadyDataService readyDataService;

    @RequestMapping(value = "/category/all", method = RequestMethod.GET)
    @ApiOperation(value = "所有分类层级", httpMethod = "GET")
    public Response<List<ReadyArticleCategory>> allCategory(HttpSession session) {
        List<ReadyArticleCategory> p = readyArticleCategoryService.all();
        return ResponseHelp.success(p);
    }

    @RequestMapping(value = "/category/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改分类信息", httpMethod = "PUT")
    public Response<Boolean> editCategory(@RequestBody @Validated ReadyArticleCategoryDto dto, HttpSession session) {
        ReadyArticleCategory entity = Transform.dtoToEntity(dto);
        if (dto.getIndex() != null){
            // 排序操作
            readyArticleCategoryService.changeOrder(dto.getId(), dto.getIndex());
        }
        readyArticleCategoryService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/category/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除分类信息", httpMethod = "DELETE")
    public Response<Boolean> deleteCategory(int id, HttpSession session) {
        ReadyArticleCategory category = readyArticleCategoryService.get(id);
        readyArticleService.deleteByCategory(category.getId());
        readyArticleCategoryService.delete(id);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/article/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加文章", httpMethod = "POST")
    private Response<Boolean> addArticle(@RequestBody @Validated ReadyArticleDto dto){
        ReadyArticle entity = Transform.dtoToEntity(dto);
        if(entity.getCategoryId() == null || entity.getCategoryId() == 0){
            if (entity.getParentCategoryId() == null || entity.getParentCategoryId() == 0){
                // 添加一级标题
                ReadyArticleCategory category = readyArticleCategoryService.addCategory(dto.getParentCategory(), 0);
                entity.setParentCategoryId(category.getId());
            }
            // 添加二级标题
            ReadyArticleCategory category = readyArticleCategoryService.addCategory(dto.getCategory(), entity.getParentCategoryId());
            entity.setCategoryId(category.getId());
        }
        readyArticleService.add(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/article/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改文章", httpMethod = "PUT")
    private Response<Boolean> editArticle(@RequestBody @Validated ReadyArticleDto dto){
        ReadyArticle entity = Transform.dtoToEntity(dto);
        readyArticleService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/article/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除文章", httpMethod = "DELETE")
    private Response<Boolean> deleteArticle(@RequestParam int id){
        readyArticleService.delete(id);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/article/detail", method = RequestMethod.GET)
    @ApiOperation(value = "文章详情", httpMethod = "GET")
    private Response<ReadyArticle> detailArticle(@RequestParam int id){
        ReadyArticle article = readyArticleService.get(id);
        return ResponseHelp.success(article);
    }

    @RequestMapping(value = "/article/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取文章列表", httpMethod = "GET")
    private Response<PageMessage<ReadyArticle>> listArticle(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Integer parentCategoryId,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction
    ){
        Page<ReadyArticle> p = readyArticleService.listAdmin(page, size, parentCategoryId, categoryId, order, DirectionStatus.ValueOf(direction));
        return ResponseHelp.success(p, page, size, p.getTotal());
    }

    @RequestMapping(value = "/area/all", method = RequestMethod.GET)
    @ApiOperation(value = "所有区域层级", httpMethod = "GET")
    public Response<List<ReadyRoomArea>> allArea(HttpSession session) {
        List<ReadyRoomArea> p = readyRoomAreaService.all();
        return ResponseHelp.success(p);
    }

    @RequestMapping(value = "/room/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加考场", httpMethod = "POST")
    private Response<Boolean> addRoom(@RequestBody @Validated ReadyRoomDto dto){
        ReadyRoom entity = Transform.dtoToEntity(dto);
        readyRoomService.add(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/room/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改考场", httpMethod = "PUT")
    private Response<Boolean> editRoom(@RequestBody @Validated ReadyRoomDto dto){
        ReadyRoom entity = Transform.dtoToEntity(dto);
        readyRoomService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/room/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除考场", httpMethod = "DELETE")
    private Response<Boolean> deleteRoom(@RequestParam int id){
        readyRoomService.delete(id);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/room/detail", method = RequestMethod.GET)
    @ApiOperation(value = "考场详情", httpMethod = "GET")
    private Response<ReadyRoom> detailRoom(@RequestParam int id){
        ReadyRoom article = readyRoomService.get(id);
        return ResponseHelp.success(article);
    }

    @RequestMapping(value = "/room/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取考场列表", httpMethod = "GET")
    private Response<PageMessage<ReadyRoom>> listRoom(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) String position,
            @RequestParam(required = false) Integer areaId,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction
    ){
        Page<ReadyRoom> p = readyRoomService.listAdmin(page, size, position, areaId, order, DirectionStatus.ValueOf(direction));
        return ResponseHelp.success(p, page, size, p.getTotal());
    }

    @RequestMapping(value = "/read/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加文章", httpMethod = "POST")
    private Response<Boolean> addRead(@RequestBody @Validated ReadyReadDto dto){
        ReadyRead entity = Transform.dtoToEntity(dto);
        readyReadService.add(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/read/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改文章", httpMethod = "PUT")
    private Response<Boolean> editRead(@RequestBody @Validated ReadyReadDto dto){
        ReadyRead entity = Transform.dtoToEntity(dto);
        readyReadService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/read/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除文章", httpMethod = "DELETE")
    private Response<Boolean> deleteRead(@RequestParam int id){
        readyReadService.delete(id);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/read/detail", method = RequestMethod.GET)
    @ApiOperation(value = "文章详情", httpMethod = "GET")
    private Response<ReadyRead> detailRead(@RequestParam int id){
        ReadyRead article = readyReadService.get(id);
        return ResponseHelp.success(article);
    }

    @RequestMapping(value = "/read/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取文章列表", httpMethod = "GET")
    private Response<PageMessage<ReadyRead>> listRead(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) String plate,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction
    ){
        Page<ReadyRead> p = readyReadService.listAdmin(page, size, plate, order, DirectionStatus.ValueOf(direction));
        return ResponseHelp.success(p, page, size, p.getTotal());
    }

    @RequestMapping(value = "/data/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加资料", httpMethod = "POST")
    private Response<Boolean> addData(@RequestBody @Validated ReadyDataDto dto){
        ReadyData entity = Transform.dtoToEntity(dto);
        readyDataService.add(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/data/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改文章", httpMethod = "PUT")
    private Response<Boolean> editData(@RequestBody @Validated ReadyDataDto dto){
        ReadyData entity = Transform.dtoToEntity(dto);
        readyDataService.edit(entity);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/data/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除文章", httpMethod = "DELETE")
    private Response<Boolean> deleteData(@RequestParam int id){
        readyDataService.delete(id);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/data/detail", method = RequestMethod.GET)
    @ApiOperation(value = "文章详情", httpMethod = "GET")
    private Response<ReadyData> detailData(@RequestParam int id){
        ReadyData entity = readyDataService.get(id);
        return ResponseHelp.success(entity);
    }

    @RequestMapping(value = "/data/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取文章列表", httpMethod = "GET")
    private Response<PageMessage<ReadyData>> listData(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Boolean isOfficial,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction
    ){
        Page<ReadyData> p = readyDataService.listAdmin(page, size, isOfficial, order, DirectionStatus.ValueOf(direction));
        return ResponseHelp.success(p, page, size, p.getTotal());
    }
}