package com.ai.da.controller;

import com.ai.da.common.config.MyTaskScheduler;
import com.ai.da.common.response.PageBaseResponse;
import com.ai.da.common.response.Response;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.*;
import com.ai.da.service.ProjectService;
import com.ai.da.service.UserLikeGroupService;
import com.ai.da.service.WorkspaceService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.minio.errors.MinioException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@Api(tags = "Project模块")
@Slf4j
@RestController
@RequestMapping("/api/project")
public class ProjectController {

    @Resource
    private WorkspaceService workspaceService;

    @Resource
    private UserLikeGroupService userLikeGroupService;

    @Resource
    private ProjectService projectService;

    @PostMapping("/saveOrUpdate")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增或编辑", notes = "传入project")
    public Response<SaveOrUpdateProjectVO> saveOrUpdateProject(@Valid @RequestBody ProjectDTO projectDTO) {
        return Response.success(workspaceService.saveOrUpdateProject(projectDTO));
    }

    @PostMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页查询", notes = "传入project")
    public Response<PageBaseResponse<ProjectVO>> page(@Valid @RequestBody ProjectQueryDTO projectQueryDTO) {
        return Response.success(PageBaseResponse.success(userLikeGroupService.getPage(projectQueryDTO)));
    }

//    @PostMapping("/detail")
//    @ApiOperationSupport(order = 3)
//    @ApiOperation(value = "详情", notes = "传入project")
//    public Response saveOrUpdateProject(@Valid @RequestBody ProjectDTO projectDTO) {
//        return Response.success(workspaceService.saveOrUpdateProject(projectDTO));
//    }
//
    @PostMapping("/choose")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "选择", notes = "传入project")
    public Response<ProjectChooseVO> choose(@Valid @RequestBody ProjectDTO projectDTO) {
        return Response.success(userLikeGroupService.choose(projectDTO));
    }

    @PostMapping("/getModuleContent")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "获取模块内容", notes = "传入project")
    public Response<ModuleChooseVO> getModuleContent(@Valid @RequestBody ProjectDTO projectDTO) {
        return Response.success(userLikeGroupService.getModuleContent(projectDTO));
    }

    @PostMapping("/saveModuleContent")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "存储模块内容", notes = "传入project")
    public Response<ModuleChooseVO> saveModuleContent(@Valid @RequestBody ModuleSaveDTO moduleSaveDTO) {
        return Response.success(userLikeGroupService.saveModuleContent(moduleSaveDTO));
    }
//
//    @PostMapping("/delete")
//    @ApiOperationSupport(order = 5)
//    @ApiOperation(value = "删除", notes = "传入project")
//    public Response saveOrUpdateProject(@Valid @RequestBody ProjectDTO projectDTO) {
//        return Response.success(workspaceService.saveOrUpdateProject(projectDTO));
//    }

    @PostMapping("/getMannequinDetail")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "获取模特详情", notes = "传入mannequinId")
    public Response<QueryLibraryPageVO> getMannequinDetail(@Valid @RequestBody MannequinDTO mannequinDTO) {
        return Response.success(userLikeGroupService.getMannequinDetail(mannequinDTO));
    }

    @PostMapping("/threeDPage")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "获取3Dpage", notes = "传入ThreeDLayoutQueryDTO")
    public Response<PageBaseResponse<ThreeDLayoutVO>> threeDPage(@Valid @RequestBody ThreeDLayoutQueryDTO threeDLayoutQueryDTO) {
        return Response.success(PageBaseResponse.success(userLikeGroupService.getThreeDLayoutPage(threeDLayoutQueryDTO)));
    }

    @PostMapping("/getLayoutDetail")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取3D详情", notes = "传入project")
    public Response<ThreeDVO> getLayoutDetail(@RequestParam(value = "threeDSimpleId") Long threeDSimpleId) {
        return Response.success(userLikeGroupService.getLayoutDetail(threeDSimpleId));
    }

    @PostMapping("/getThreeDSize")

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "获取尺码", notes = "传入project")
    public Response<ThreeDSizeVO> getThreeDSize(@RequestParam(value = "threeDSimpleId") Long threeDSimpleId) {
        return Response.success(userLikeGroupService.getThreeDSize(threeDSimpleId));
    }

    @GetMapping("/getThreeDGlb")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "获取GLB", notes = "传入project")
    public void getThreeDGlb(@RequestParam(value = "threeDSimpleId") Long threeDSimpleId, HttpServletResponse response) throws MinioException, IOException {
        userLikeGroupService.getThreeDGlb(threeDSimpleId, response);
    }

    @GetMapping("/downloadZip")
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "下载", notes = "传入project")
    public Response<String> downloadZip(@RequestParam(value = "threeDSimpleId") Long threeDSimpleId, @RequestParam(value = "sizeType") String sizeType, @RequestParam(value = "size") String size, HttpServletResponse response) throws MinioException, IOException {
        return Response.success(userLikeGroupService.downloadZip(threeDSimpleId, sizeType, size, response));
    }

    @PostMapping("/delete")
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "删除", notes = "传入project")
    public Response<Boolean> delete(@RequestParam("projectId") Long projectId) {
        return Response.success(userLikeGroupService.delete(projectId));
    }


    @Resource
    private MyTaskScheduler myTaskScheduler;

    /*@GetMapping("/dataMigration")
    public String dataMigration(){
        myTaskScheduler.projectDataCreate();
        return "success";
    }*/

    @GetMapping("/getNextSequence")
    public Response<Integer> getNextSequence(){
        return Response.success(projectService.getOrCreateSequence());
    }


}
