package com.salpa.web.controller.subject;

import com.salpa.common.core.controller.BaseController;
import com.salpa.common.core.domain.AjaxResult;
import com.salpa.common.core.domain.model.LoginUser;
import com.salpa.common.utils.SecurityUtils;
import com.salpa.subject.service.DocumentService;
import com.salpa.subject.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;


/**
 * 数据库文档
 *
 * @author zhuff
 */
@RestController
@RequestMapping("/subject/document")
public class DocumentController extends BaseController {

    @Autowired
    private DocumentService documentService;

    @Autowired
    private ProjectService projectService;

    /**
     * 创建同步任务
     * @param projectId
     * @return
     */
    @GetMapping("createSyncTask")
    public AjaxResult createSyncTask(Integer projectId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int syncTask = documentService.createSyncTask(projectId, loginUser.getUserId(), false);
        if (syncTask > 0) {
            return AjaxResult.success(syncTask);
        } else {
            return AjaxResult.error("创建同步任务失败");
        }
    }

    /**
     * 获取版本号
     * @param projectId
     * @return
     */
    @GetMapping("getVersionsByProjectId")
    public AjaxResult getVersionsByProjectId(Integer projectId) {
        return AjaxResult.success(documentService.getVersionsByProjectId(projectId));
    }

    /**
     * 获取文档信息
     * @param projectId
     * @param version
     * @param search
     * @param originalVersion
     * @return
     */
    @GetMapping("getSimpleByProjectId")
    public AjaxResult getSimpleByProjectId(Integer projectId,Long version,String search,Long originalVersion) {
        return AjaxResult.success(documentService.getSimpleByProjectId(projectId,version,search,originalVersion));
    }

    /**
     * 获取表名列表
     * @param projectId
     * @param version
     * @param search
     * @return
     */
    @GetMapping("getTableDocumentByProjectId")
    public AjaxResult getTableDocumentByProjectId(Integer projectId,Long version,String search){
        return AjaxResult.success(documentService.getTableDocumentByProjectId(projectId,version,search));
    }

    /**
     * 获取每张表详细信息
     * @param id
     * @param projectId
     * @param version
     * @param originalVersion
     * @return
     */
    @GetMapping("getTableDetail")
    public AjaxResult getTableDetail(Integer id,Integer projectId,Long version,Long originalVersion) {
        return AjaxResult.success(documentService.getTableDetail(id,projectId,version,originalVersion));
    }

    /**
     * 获取同步任务列表
     * @param taskIds
     * @return
     */
    @GetMapping("getSyncTaskList")
    public AjaxResult getSyncTaskList(@RequestParam Integer[] taskIds) {
        return AjaxResult.success(documentService.getSyncTaskList(taskIds));
    }

    /**
     * 取消同步任务
     * @param taskId
     * @return
     */
    @GetMapping("cancelTask")
    public AjaxResult cancelTask(Integer taskId) {
        return toAjax(documentService.cancelTask(taskId));
    }

    /**
     * 获取同步任务状态
     * @param taskId
     * @return
     */
    @GetMapping("getSyncTaskStatus")
    public AjaxResult getSyncTaskStatus(Integer taskId) {
        return AjaxResult.success(documentService.getSyncTaskStatus(taskId));
    }

    /**
     * 导出文档
     * @param projectId
     * @param version
     * @param fileType
     * @return
     */
    @GetMapping("getDocumentFiles")
    public ResponseEntity<StreamingResponseBody> getDocumentFiles(Integer projectId, Long version, String fileType) {
        HttpHeaders headers = new HttpHeaders();
        String projectName = projectService.getProjectById(projectId).getProjectName();
        String fileName = projectName;
        if ("Markdown".equals(fileType)) {
            fileName += ".md";
        } else if ("UML SVG".equals(fileType)) {
            fileName += ".svg";
        } else if ("UML PNG".equals(fileType)) {
            fileName += ".png";
        } else if ("Word".equals(fileType)) {
            fileName += ".docx";
        }
        headers.setContentDisposition(ContentDisposition.attachment()
                .filename(fileName)
                .build());
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return ResponseEntity.ok()
                .headers(headers)
                .body(out -> documentService.export(projectId, version, fileType, out));
    }
}
