package admin

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"os"
	"server/dao"
	"server/global"
	"server/model/common/response"
	"strconv"
	"strings"
)

type FileApi struct{}

func (fa *FileApi) QueryFileGenre(c *gin.Context) {
	genre, err := fileService.QueryFileGenre()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryFileGenre ======= " + err.Error())
		return
	}
	response.OkWithData(genre, c)
}

func (fa *FileApi) CreateFileGenre(c *gin.Context) {
	var fileGenre dao.FileGenre
	err := c.ShouldBindJSON(&fileGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateFileGenre ====== " + err.Error())
		return
	}
	err = fileService.CreateFileGenre(fileGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateFileGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (fa *FileApi) QueryNecessaryDocuments(c *gin.Context) {
	necessaryDocument, err := fileService.QueryNecessaryDocuments()
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryNecessaryDocument ======= " + err.Error())
		return
	}
	response.OkWithData(necessaryDocument, c)
}

func (fa *FileApi) QueryNecessaryDocumentByGenreId(c *gin.Context) {
	genreId := c.Query("genre_id")
	id, _ := strconv.Atoi(genreId)
	necessaryDocument, err := fileService.QueryNecessaryDocumentByGenreId(id)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("QueryNecessaryDocumentByGenreId ======= " + err.Error())
		return
	}
	response.OkWithData(necessaryDocument, c)
}

func (fa *FileApi) CreateNecessaryDocument(c *gin.Context) {
	var necessaryDocument dao.NecessaryDocument
	err := c.ShouldBindJSON(&necessaryDocument)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateNecessaryDocument ====== " + err.Error())
		return
	}
	err = fileService.CreateNecessaryDocument(necessaryDocument)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("CreateNecessaryDocument ====== " + err.Error())
		return
	}
	response.OkWithMessage("新增成功", c)
}

func (fa *FileApi) UpdateFileGenre(c *gin.Context) {
	var fileGenre dao.FileGenre
	err := c.ShouldBindJSON(&fileGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateFileGenre ====== " + err.Error())
		return
	}
	err = fileService.UpdateFileGenre(fileGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateFileGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("修改成功", c)
}

func (fa *FileApi) UpdateNecessaryDocument(c *gin.Context) {
	var necessaryDocument dao.NecessaryDocument
	err := c.ShouldBindJSON(&necessaryDocument)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateNecessaryDocument ====== " + err.Error())
		return
	}
	err = fileService.UpdateNecessaryDocument(necessaryDocument)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("UpdateNecessaryDocument ====== " + err.Error())
		return
	}
	response.OkWithMessage("修改成功", c)
}

func (fa *FileApi) DeleteFileGenre(c *gin.Context) {
	var fileGenre dao.FileGenre
	err := c.ShouldBindJSON(&fileGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteFileGenre ====== " + err.Error())
		return
	}
	err = fileService.DeleteFileGenre(fileGenre)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteFileGenre ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (fa *FileApi) DeleteNecessaryDocument(c *gin.Context) {
	var necessaryDocument dao.NecessaryDocument
	err := c.ShouldBindJSON(&necessaryDocument)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteNecessaryDocument ====== " + err.Error())
		return
	}
	err = fileService.DeleteNecessaryDocument(necessaryDocument)
	if err != nil {
		response.FailWithMessage("失败", c)
		global.GVA_LOG.Error("DeleteNecessaryDocument ====== " + err.Error())
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (fa *FileApi) FileDownload(c *gin.Context) {
	path := c.Query("path")
	name := c.Query("name")

	// 验证或清理文件路径（这里只是一个简单的示例）
	if strings.Contains(path, "..") {
		response.FailWithMessage("无效的路径", c)
		log.Println("检测到可能的路径遍历攻击尝试")
		return
	}

	_, err := os.Stat(path)
	if err != nil {
		// 如果出现错误，可能是文件或目录不存在
		if os.IsNotExist(err) {
			c.JSON(http.StatusNotFound, gin.H{"error": "文件不存在"})
			return
		}
		// 其他错误处理
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.Header("Content-Type", "application/octet-stream")          // 表示是文件流，唤起浏览器下载，一般设置了这个，就要设置文件名
	c.Header("Content-Disposition", "attachment; filename="+name) // 用来指定下载下来的文件名
	c.Header("Content-Transfer-Encoding", "binary")               // 表示传输过程中的编码形式，乱码问题可能就是因为它
	c.File(path)
}
